-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_conversions.py
71 lines (58 loc) · 2.72 KB
/
test_conversions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import unittest
from assertpy import assert_that
from conversions import *
class ConversionsTest(unittest.TestCase):
def test_length_conversions(self):
# Test 1: non base unit conversions with prefixes
result1 = convert_units(24000.0, 'in', 'ft', length_conversions, 'c', 'k')
true_value = 0.02
assert_that(result1).is_between(0.99999*true_value, 1.00001*true_value)
def test_pressure_conversions(self):
# Test 1: non base unit conversions with prefixes
result1 = convert_units(2.0, 'atm', 'Pa', pressure_conversions, 'c', 'k')
true_value = 2.0265
assert_that(result1).is_between(0.99999*true_value, 1.00001*true_value)
def test_temperature_conversions(self):
# Test 1: non base unit conversions with prefixes
result1 = convert_units(100, 'deg C', 'R', temp_conversions, 'c', 'k')
true_value = 0.49347
assert_that(result1).is_between(0.99999*true_value, 1.00001*true_value)
def test_energy_conversions(self):
# Test 1: non base unit conversions with prefixes
result1 = convert_units(200000, 'BTU', 'gram cal', energy_conversions, 'c', 'k')
true_value = 504.3288
assert_that(result1).is_between(0.99999*true_value, 1.00001*true_value)
def test_mass_conversions(self):
# Test 1: non base unit conversions with prefixes
result1 = convert_units(200.0, 'ton', 'oz', mass_conversions, 'c', 'k')
true_value = 64
assert_that(result1).is_between(0.99999*true_value, 1.00001*true_value)
def test_same_to_same_behavior(self):
for unit_type, conv_map in conversion_maps.items():
for unit in conv_map.keys():
for prefix in prefix_modifiers:
for i in range(0, 1000):
assert_that(
convert_units(float(i), unit, unit, conv_map, unit1_prefix=prefix, unit2_prefix=prefix)
).is_between((0.9999*float(i)) - 1e-4, (1.0001*float(i) + 1e-4))
def test_exceptions(self):
self.assertRaises(
InvalidInputException,
convert_units, "foo", "m", "m", length_conversions
)
self.assertRaises(
InvalidUnitException,
convert_units, 1.0, "asd", "m", length_conversions
)
self.assertRaises(
InvalidUnitException,
convert_units, 1.0, "m", "asd", length_conversions
)
self.assertRaises(
InvalidPrefixException,
convert_units, 1.0, "m", "m", length_conversions, unit1_prefix="foo"
)
self.assertRaises(
InvalidPrefixException,
convert_units, 1.0, "m", "m", length_conversions, unit2_prefix="foo"
)