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+
2121from unittest import mock
2222
2323from 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+
160174def 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+
183211def 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
254282def 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' )}
0 commit comments