Skip to content

Commit df590bf

Browse files
committed
Improve tests
1 parent df46b00 commit df590bf

9 files changed

Lines changed: 486 additions & 29 deletions

File tree

animatedledstrip/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .animation_data import AnimationData
2+
from .animation_info import AnimationInfo
23
from .animation_sender import AnimationSender
34
from .color_container import ColorContainer
45
from .direction import Direction

animatedledstrip/animation_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def check_data_types(self) -> bool:
109109
good_types = True
110110

111111
good_types = good_types and check_data_type('animation', self.animation, str)
112+
check_data_type('colors', self.colors, list, force_strict=True)
112113
for color_container in self.colors:
113114
good_types = good_types and check_data_type('color', color_container, ColorContainer)
114115
good_types = good_types and check_data_type('center', self.center, int)

animatedledstrip/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def nullable_str(value: Optional[Any]) -> str:
3232
return str(value)
3333

3434

35-
def check_data_type(name: str, param: Any, correct_type: TypeVar, allow_none: bool = False) -> bool:
35+
def check_data_type(name: str, param: Any, correct_type: TypeVar, *,
36+
allow_none: bool = False, force_strict: bool = False) -> bool:
3637
"""Check that a parameter has the correct data type
3738
3839
If it does not, then when STRICT_TYPE_CHECKING is:
@@ -49,7 +50,7 @@ def check_data_type(name: str, param: Any, correct_type: TypeVar, allow_none: bo
4950
cor_type=str(correct_type),
5051
none_allowed=" or None" if allow_none else "")
5152

52-
if global_vars.STRICT_TYPE_CHECKING:
53+
if force_strict or global_vars.STRICT_TYPE_CHECKING:
5354
# Type was incorrect and STRICT_TYPE_CHECKING is True, raise TypeError with message
5455
raise TypeError(msg)
5556
else:

test/animatedledstrip/test_animation_data.py

Lines changed: 100 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ def test_constructor():
3838
assert data.spacing == -1
3939

4040

41-
def test_animation():
41+
def test_animation(caplog):
4242
data = AnimationData()
4343

4444
data.animation = 'Sparkle'
4545
assert data.check_data_types() is True
4646

4747
data.animation = 3
48-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
48+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
4949
assert data.check_data_types() is False
50+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
51+
assert log_messages == {("Bad data type for animation: <class 'int'> (should be <class 'str'>)", 'ERROR')}
5052

5153
try:
5254
data.check_data_types()
@@ -55,15 +57,17 @@ def test_animation():
5557
pass
5658

5759

58-
def test_center():
60+
def test_center(caplog):
5961
data = AnimationData()
6062

6163
data.center = 5
6264
assert data.check_data_types() is True
6365

6466
data.center = Direction.FORWARD
65-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
67+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
6668
assert data.check_data_types() is False
69+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
70+
assert log_messages == {("Bad data type for center: <enum 'Direction'> (should be <class 'int'>)", 'ERROR')}
6771

6872
try:
6973
data.check_data_types()
@@ -72,7 +76,65 @@ def test_center():
7276
pass
7377

7478

75-
def test_continuous():
79+
def test_colors(caplog):
80+
data = AnimationData()
81+
82+
data.colors = []
83+
assert data.check_data_types() is True
84+
85+
data.colors = [ColorContainer()]
86+
assert data.check_data_types() is True
87+
88+
data.colors = [ColorContainer(), ColorContainer()]
89+
assert data.check_data_types() is True
90+
91+
try:
92+
data.colors = Direction.FORWARD
93+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
94+
data.check_data_types()
95+
raise AssertionError
96+
except TypeError:
97+
pass
98+
99+
try:
100+
data.check_data_types()
101+
raise AssertionError
102+
except TypeError:
103+
pass
104+
105+
data.colors = [Direction.FORWARD]
106+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
107+
assert data.check_data_types() is False
108+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
109+
assert log_messages == {
110+
("Bad data type for color: <enum 'Direction'> "
111+
"(should be <class 'animatedledstrip.color_container.ColorContainer'>)", 'ERROR')
112+
}
113+
caplog.clear()
114+
115+
try:
116+
data.check_data_types()
117+
raise AssertionError
118+
except TypeError:
119+
pass
120+
121+
data.colors = [ColorContainer(), Direction.FORWARD]
122+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
123+
assert data.check_data_types() is False
124+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
125+
assert log_messages == {
126+
("Bad data type for color: <enum 'Direction'> "
127+
"(should be <class 'animatedledstrip.color_container.ColorContainer'>)", 'ERROR')
128+
}
129+
130+
try:
131+
data.check_data_types()
132+
raise AssertionError
133+
except TypeError:
134+
pass
135+
136+
137+
def test_continuous(caplog):
76138
data = AnimationData()
77139

78140
data.continuous = True
@@ -82,8 +144,11 @@ def test_continuous():
82144
assert data.check_data_types() is True
83145

84146
data.continuous = 5
85-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
147+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
86148
assert data.check_data_types() is False
149+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
150+
assert log_messages == {
151+
("Bad data type for continuous: <class 'int'> (should be <class 'bool'> or None)", 'ERROR')}
87152

88153
try:
89154
data.check_data_types()
@@ -92,15 +157,17 @@ def test_continuous():
92157
pass
93158

94159

95-
def test_delay():
160+
def test_delay(caplog):
96161
data = AnimationData()
97162

98163
data.delay = 10
99164
assert data.check_data_types() is True
100165

101166
data.delay = Direction.BACKWARD
102-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
167+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
103168
assert data.check_data_types() is False
169+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
170+
assert log_messages == {("Bad data type for delay: <enum 'Direction'> (should be <class 'int'>)", 'ERROR')}
104171

105172
try:
106173
data.check_data_types()
@@ -109,15 +176,17 @@ def test_delay():
109176
pass
110177

111178

112-
def test_delay_mod():
179+
def test_delay_mod(caplog):
113180
data = AnimationData()
114181

115182
data.delay_mod = 0.5
116183
assert data.check_data_types() is True
117184

118185
data.delay_mod = 3
119-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
186+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
120187
assert data.check_data_types() is False
188+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
189+
assert log_messages == {("Bad data type for delay_mod: <class 'int'> (should be <class 'float'>)", 'ERROR')}
121190

122191
try:
123192
data.check_data_types()
@@ -126,15 +195,17 @@ def test_delay_mod():
126195
pass
127196

128197

129-
def test_direction():
198+
def test_direction(caplog):
130199
data = AnimationData()
131200

132201
data.direction = Direction.BACKWARD
133202
assert data.check_data_types() is True
134203

135204
data.direction = 1
136-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
205+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
137206
assert data.check_data_types() is False
207+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
208+
assert log_messages == {("Bad data type for direction: <class 'int'> (should be <enum 'Direction'>)", 'ERROR')}
138209

139210
try:
140211
data.check_data_types()
@@ -143,15 +214,17 @@ def test_direction():
143214
pass
144215

145216

146-
def test_distance():
217+
def test_distance(caplog):
147218
data = AnimationData()
148219

149220
data.distance = 5
150221
assert data.check_data_types() is True
151222

152223
data.distance = 5.0
153-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
224+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
154225
assert data.check_data_types() is False
226+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
227+
assert log_messages == {("Bad data type for distance: <class 'float'> (should be <class 'int'>)", 'ERROR')}
155228

156229
try:
157230
data.check_data_types()
@@ -160,15 +233,17 @@ def test_distance():
160233
pass
161234

162235

163-
def test_id():
236+
def test_id(caplog):
164237
data = AnimationData()
165238

166239
data.id = 'TEST'
167240
assert data.check_data_types() is True
168241

169242
data.id = 5
170-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
243+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
171244
assert data.check_data_types() is False
245+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
246+
assert log_messages == {("Bad data type for id: <class 'int'> (should be <class 'str'>)", 'ERROR')}
172247

173248
try:
174249
data.check_data_types()
@@ -177,15 +252,17 @@ def test_id():
177252
pass
178253

179254

180-
def test_section():
255+
def test_section(caplog):
181256
data = AnimationData()
182257

183258
data.section = 'SECT'
184259
assert data.check_data_types() is True
185260

186261
data.section = 5
187-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
262+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
188263
assert data.check_data_types() is False
264+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
265+
assert log_messages == {("Bad data type for section: <class 'int'> (should be <class 'str'>)", 'ERROR')}
189266

190267
try:
191268
data.check_data_types()
@@ -194,15 +271,17 @@ def test_section():
194271
pass
195272

196273

197-
def test_spacing():
274+
def test_spacing(caplog):
198275
data = AnimationData()
199276

200277
data.spacing = 10
201278
assert data.check_data_types() is True
202279

203280
data.spacing = 3.0
204-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
281+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
205282
assert data.check_data_types() is False
283+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
284+
assert log_messages == {("Bad data type for spacing: <class 'float'> (should be <class 'int'>)", 'ERROR')}
206285

207286
try:
208287
data.check_data_types()
@@ -262,7 +341,7 @@ def test_json_bad_type():
262341
data = AnimationData()
263342
data.spacing = 1.5
264343

265-
with mock.patch('led_client.global_vars.STRICT_TYPE_CHECKING', False):
344+
with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False):
266345
assert data.json() == ''
267346

268347
try:

0 commit comments

Comments
 (0)