Skip to content

Commit 48cd396

Browse files
committed
Add tests for Command and Message
1 parent 554ed97 commit 48cd396

6 files changed

Lines changed: 132 additions & 18 deletions

File tree

animatedledstrip/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from .animation_data import AnimationData
22
from .animation_info import AnimationInfo
33
from .animation_sender import AnimationSender
4+
from .command import Command
45
from .color_container import ColorContainer
56
from .direction import Direction
67
from .end_animation import EndAnimation
78
from .global_vars import ANIMATION_DATA_PREFIX, ANIMATION_INFO_PREFIX, DELIMITER, \
89
END_ANIMATION_PREFIX, SECTION_PREFIX, STRICT_TYPE_CHECKING, STRIP_INFO_PREFIX
10+
from .message import Message
911
from .param_usage import ParamUsage
1012
from .section import Section
1113
from .strip_info import StripInfo

animatedledstrip/animation_sender.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def parse_data(self):
207207
self.on_new_strip_info_callback(info)
208208

209209
else:
210-
logging.warning('Unrecognized data type: {}'.format(split_input[:4]))
210+
logging.warning('Unrecognized data type: {}'.format(str(split_input[:4], 'utf-8')))
211211

212212
except socket.timeout:
213213
pass

test/animatedledstrip/test_animation_sender.py

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
# THE SOFTWARE.
20-
import socket
20+
2121
from unittest import mock
2222

2323
from animatedledstrip import AnimationSender, AnimationData, Direction, ParamUsage
@@ -37,6 +37,7 @@ def test_constructor():
3737
assert sender.on_new_animation_data_callback is None
3838
assert sender.on_new_animation_info_callback is None
3939
assert sender.on_new_end_animation_callback is None
40+
assert sender.on_new_message_callback is None
4041
assert sender.on_new_section_callback is None
4142
assert sender.on_new_strip_info_callback is None
4243

@@ -54,11 +55,11 @@ def test_parse_data_animation_data():
5455
sender = AnimationSender('10.0.0.254', 5)
5556

5657
with mock.patch.object(sender, 'connection') as mock_socket:
57-
with mock.patch.object(sender, 'newAnimationDataCallback') as mock_callback:
58+
with mock.patch.object(sender, 'on_new_animation_data_callback') as mock_callback:
5859
mock_socket.recv.return_value = bytes('DATA:{"animation":"Meteor","baseDelay":10,"center":120,'
5960
'"colors":[{"colors":[16711680]}],"continuous":null,'
6061
'"delayMod":1.0,"direction":"FORWARD","distance":240,'
61-
'"id":"52782797","section":"","spacing":3}', 'utf-8')
62+
'"id":"52782797","section":"","spacing":3};;;', 'utf-8')
6263

6364
sender.parse_data()
6465

@@ -87,14 +88,14 @@ def test_parse_data_animation_info():
8788
sender = AnimationSender('10.0.0.254', 5)
8889

8990
with mock.patch.object(sender, 'connection') as mock_socket:
90-
with mock.patch.object(sender, 'newAnimationInfoCallback') as mock_callback:
91+
with mock.patch.object(sender, 'on_new_animation_info_callback') as mock_callback:
9192
mock_socket.recv.return_value = bytes('AINF:{"name":"Multi Pixel Run","abbr":"MPR",'
9293
'"description":"Similar to [Pixel Run](Pixel-Run) but with multiple '
9394
'LEDs at a specified spacing.","signatureFile":"multi_pixel_run.png",'
9495
'"repetitive":true,"minimumColors":1,"unlimitedColors":false,'
9596
'"center":"NOTUSED","delay":"USED","direction":"USED",'
9697
'"distance":"NOTUSED","spacing":"USED","delayDefault":100,'
97-
'"distanceDefault":-1,"spacingDefault":3}', 'utf-8')
98+
'"distanceDefault":-1,"spacingDefault":3};;;', 'utf-8')
9899

99100
sender.parse_data()
100101

@@ -128,7 +129,7 @@ def test_parse_data_animation_info():
128129
'"repetitive":false,"minimumColors":1,"unlimitedColors":true,'
129130
'"center":"USED","delay":"USED","direction":"USED",'
130131
'"distance":"USED","spacing":"USED","delayDefault":1000,'
131-
'"distanceDefault":10,"spacingDefault":30}', 'utf-8')
132+
'"distanceDefault":10,"spacingDefault":30};;;', 'utf-8')
132133

133134
sender.parse_data()
134135

@@ -157,21 +158,34 @@ def test_parse_data_animation_info():
157158
assert mock_callback.called_with(info2)
158159

159160

161+
def test_parse_data_command(caplog):
162+
sender = AnimationSender('10.0.0.254', 5)
163+
164+
with mock.patch.object(sender, 'connection') as mock_socket:
165+
mock_socket.recv.return_value = bytes('CMD :{"command":"test_command"};;;', 'utf-8')
166+
167+
sender.parse_data()
168+
169+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
170+
171+
assert log_messages == {('Receiving Command is not supported by client', 'WARNING')}
172+
173+
160174
def test_parse_data_end_animation():
161175
sender = AnimationSender('10.0.0.254', 5)
162176

163177
with mock.patch.object(sender, 'connection') as mock_socket:
164-
with mock.patch.object(sender, 'newEndAnimationCallback') as mock_callback:
178+
with mock.patch.object(sender, 'on_new_end_animation_callback') as mock_callback:
165179
mock_socket.recv.return_value = bytes('DATA:{"animation":"Meteor","baseDelay":10,"center":120,'
166180
'"colors":[{"colors":[16711680]}],"continuous":null,'
167181
'"delayMod":1.0,"direction":"FORWARD","distance":240,'
168-
'"id":"52782797","section":"","spacing":3}', 'utf-8')
182+
'"id":"52782797","section":"","spacing":3};;;', 'utf-8')
169183

170184
sender.parse_data()
171185

172186
assert '52782797' in sender.running_animations.keys()
173187

174-
mock_socket.recv.return_value = bytes('END :{"id":"52782797"}', 'utf-8')
188+
mock_socket.recv.return_value = bytes('END :{"id":"52782797"};;;', 'utf-8')
175189

176190
sender.parse_data()
177191

@@ -180,13 +194,27 @@ def test_parse_data_end_animation():
180194
assert mock_callback.called
181195

182196

197+
def test_parse_data_message():
198+
sender = AnimationSender('10.0.0.254', 5)
199+
200+
with mock.patch.object(sender, 'connection') as mock_socket:
201+
with mock.patch.object(sender, 'on_new_message_callback') as mock_callback:
202+
mock_socket.recv.return_value = bytes('MSG :{"message":"a message"};;;',
203+
'utf-8')
204+
205+
sender.parse_data()
206+
207+
assert mock_callback.called
208+
assert mock_callback.called_with('a message')
209+
210+
183211
def test_parse_data_section():
184212
sender = AnimationSender('10.0.0.254', 5)
185213

186214
with mock.patch.object(sender, 'connection') as mock_socket:
187-
with mock.patch.object(sender, 'newSectionCallback') as mock_callback:
215+
with mock.patch.object(sender, 'on_new_section_callback') as mock_callback:
188216
mock_socket.recv.return_value = bytes('SECT:{"physicalStart":0,"numLEDs":240,"name":"section",'
189-
'"startPixel":0,"endPixel":239}',
217+
'"startPixel":0,"endPixel":239};;;',
190218
'utf-8')
191219

192220
sender.parse_data()
@@ -209,9 +237,9 @@ def test_parse_data_strip_info():
209237
sender = AnimationSender('10.0.0.254', 5)
210238

211239
with mock.patch.object(sender, 'connection') as mock_socket:
212-
with mock.patch.object(sender, 'newStripInfoCallback') as mock_callback:
240+
with mock.patch.object(sender, 'on_new_strip_info_callback') as mock_callback:
213241
mock_socket.recv.return_value = bytes('SINF:{"numLEDs":240,"pin":12,"imageDebugging":false,'
214-
'"fileName":"test.csv","rendersBeforeSave":1000,"threadCount":100}',
242+
'"fileName":"test.csv","rendersBeforeSave":1000,"threadCount":100};;;',
215243
'utf-8')
216244

217245
sender.parse_data()
@@ -231,7 +259,7 @@ def test_parse_data_strip_info():
231259
assert mock_callback.called_with(info)
232260

233261
mock_socket.recv.return_value = bytes('SINF:{"numLEDs":20,"pin":null,"imageDebugging":true,'
234-
'"fileName":null,"rendersBeforeSave":null,"threadCount":20}',
262+
'"fileName":null,"rendersBeforeSave":null,"threadCount":20};;;',
235263
'utf-8')
236264

237265
sender.parse_data()
@@ -252,4 +280,13 @@ def test_parse_data_strip_info():
252280

253281

254282
def test_parse_data_bad_data_type(caplog):
255-
pass
283+
sender = AnimationSender('10.0.0.254', 5)
284+
285+
with mock.patch.object(sender, 'connection') as mock_socket:
286+
mock_socket.recv.return_value = bytes('BAD :{};;;', 'utf-8')
287+
288+
sender.parse_data()
289+
290+
log_messages = {(log.msg, log.levelname) for log in caplog.records}
291+
292+
assert log_messages == {('Unrecognized data type: BAD ', 'WARNING')}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2019-2020 AnimatedLEDStrip
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
from animatedledstrip import Command
22+
23+
24+
def test_constructor():
25+
cmd = Command()
26+
27+
assert cmd.command == ''
28+
29+
30+
def test_command_json():
31+
cmd = Command()
32+
cmd.command = 'test_command'
33+
34+
assert cmd.json() == 'CMD :{"command":"test_command"}'

test/animatedledstrip/test_end_animation.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ def test_end_animation_json():
3131
end = EndAnimation()
3232
end.id = '123456'
3333

34-
json = end.json()
34+
assert end.json() == 'END :{"id":"123456"}'
3535

36-
assert json == 'END :{"id":"123456"}'
36+
37+
def test_end_animation_from_json():
38+
end_json = 'END :{"id":"testId"}'
39+
40+
end = EndAnimation.from_json(end_json)
41+
assert end.id == 'testId'
42+
43+
assert end.json() == 'END :{"id":"testId"}'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2019-2020 AnimatedLEDStrip
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
from animatedledstrip import Message
22+
23+
24+
def test_constructor():
25+
msg = Message()
26+
27+
assert msg.message == ''
28+
29+
30+
def test_message_from_json():
31+
message_json = 'MSG :{"message":"This is a message"}'
32+
33+
msg = Message.from_json(message_json)
34+
assert msg.message == 'This is a message'

0 commit comments

Comments
 (0)