-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.py
151 lines (107 loc) · 3.57 KB
/
utils_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Tests for the utils module."""
import unittest
from utils import exists_and_not_none
from utils import power_to_temperature_rate
from utils import rgetattr
from utils import rsetattr
class TestRsetattr(unittest.TestCase):
"""Tests for rsetattr."""
def test_set_on_same(self):
class Foo(object):
def __init__(self):
self.foo_var = 11
foo = Foo()
rsetattr(foo, "foo_var", 12)
got = foo.foo_var
self.assertEquals(got, 12)
def test_set_on_related(self):
class Foo(object):
def __init__(self):
self.foo_var = 11
class Bar(object):
def __init__(self):
self.bar_var = Foo()
bar = Bar()
rsetattr(bar, "bar_var__foo_var", 12)
got = bar.bar_var.foo_var
self.assertEquals(got, 12)
def test_set_on_nested_related(self):
class Foo(object):
def __init__(self):
self.foo_var = 11
class Bar(object):
def __init__(self):
self.bar_var = Foo()
class Baz(object):
def __init__(self):
self.baz_var = Bar()
baz = Baz()
rsetattr(baz, "baz_var__bar_var__foo_var", 12)
got = baz.baz_var.bar_var.foo_var
self.assertEquals(got, 12)
def test_set_doesnt_exist(self):
class Foo(object):
pass
foo = Foo()
with self.assertRaises(AttributeError):
rsetattr(foo, "bar_var", 12)
class TestRgetattr(unittest.TestCase):
"""Tests for rgetattr."""
def test_get_on_same(self):
class Foo(object):
def __init__(self):
self.foo_var = 11
foo = Foo()
rgetattr(foo, "foo_var")
got = foo.foo_var
self.assertEquals(got, 11)
def test_get_on_related(self):
class Foo(object):
def __init__(self):
self.foo_var = 11
class Bar(object):
def __init__(self):
self.bar_var = Foo()
bar = Bar()
rgetattr(bar, "bar_var__foo_var")
got = bar.bar_var.foo_var
self.assertEquals(got, 11)
def test_get_on_nested_related(self):
class Foo(object):
def __init__(self):
self.foo_var = 11
class Bar(object):
def __init__(self):
self.bar_var = Foo()
class Baz(object):
def __init__(self):
self.baz_var = Bar()
baz = Baz()
rgetattr(baz, "baz_var__bar_var__foo_var")
got = baz.baz_var.bar_var.foo_var
self.assertEquals(got, 11)
def test_get_doesnt_exist(self):
class Foo(object):
pass
foo = Foo()
with self.assertRaises(AttributeError):
rgetattr(foo, "bar_var")
class TestExistsAndNotNone(unittest.TestCase):
"""Tests for exists_and_not_none."""
def test_does_not_exist(self):
foo = {}
self.assertFalse(exists_and_not_none(foo, "foo"))
def test_exists_and_none(self):
foo = {"foo": None}
self.assertFalse(exists_and_not_none(foo, "foo"))
def test_exists_and_not_none(self):
foo = {"foo": 1}
self.assertTrue(exists_and_not_none(foo, "foo"))
class TestPowerToTemperatureRate(unittest.TestCase):
"""Tests for power_to_temperature_rate."""
def test(self):
power = 100.0 # Watts
volume = 1.0 # Gallons
got = power_to_temperature_rate(power, volume)
want = 0.0113 # degF/second
self.assertAlmostEquals(got, want, 2)