Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Add basic unit tests for precipitation
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Martz committed Dec 6, 2016
1 parent 027ac56 commit 0ee7e6b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
85 changes: 44 additions & 41 deletions django/climate_change_api/indicators/tests/test_unit_converters.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
from django.test import TestCase
from indicators.unit_converters import TemperatureConverter
from indicators.tests.mixins import ConverterTestMixin
from indicators.unit_converters import TemperatureConverter, PrecipitationConverter


class TemperatureConverterTestCase(TestCase):
def test_conversions(self):
cases = {
('F', 'K'): [
(0, 255.372222),
(32, 273.15),
(73.45, 296.17778)
],
('K', 'F'): [
(0, -459.67),
(276.3, 37.67)
],
('K', 'C'): [
(0, -273.15),
(295.3, 22.15)
],
('C', 'F'): [
(-40, -40),
(0, 32),
(10, 50),
(100, 212),
(23.2, 73.76)
],
('F', 'C'): [
(80, 26.6667),
(73.2, 22.888889),
(-40, -40),
(0, -17.77778),
(212, 100),
(32, 0)
]
}
class TemperatureConverterTestCase(ConverterTestMixin, TestCase):
converter_class = TemperatureConverter
cases = {
('F', 'K'): [
(0, 255.372222),
(32, 273.15),
(73.45, 296.17778)
],
('K', 'F'): [
(0, -459.67),
(276.3, 37.67)
],
('K', 'C'): [
(0, -273.15),
(295.3, 22.15)
],
('C', 'F'): [
(-40, -40),
(0, 32),
(10, 50),
(100, 212),
(23.2, 73.76)
],
('F', 'C'): [
(80, 26.6667),
(73.2, 22.888889),
(-40, -40),
(0, -17.77778),
(212, 100),
(32, 0)
]
}

for units, tests in cases.iteritems():
converter = TemperatureConverter.get(*units)

for start, expected in tests:
value = converter(start)
self.assertAlmostEqual(value, expected,
places=3,
msg='Failed assertion that %f %s = %f %s, got %f %s' %
(start, units[0], expected, units[1], value, units[1]))
class PrecipitationConverterTestCase(ConverterTestMixin, TestCase):
converter_class = PrecipitationConverter
cases = {
('kg/m^2/s', 'kg/m^2/day'): [
(1, 86400)
],
('kg/m^2/day', 'kg/m^2/s'): [
(86400, 1)
]
}
2 changes: 1 addition & 1 deletion django/climate_change_api/indicators/unit_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LinearConverter(UnitConverter):
units = {}

def __init__(self, start, end):
self.ratio = self.units[end] / self.units[start]
self.ratio = 1.0 * self.units[end] / self.units[start]

def convert(self, value):
return value * self.ratio
Expand Down

0 comments on commit 0ee7e6b

Please sign in to comment.