33import unittest
44
55import six
6+
67if six .PY3 :
78 from unittest import mock
89else :
@@ -38,53 +39,61 @@ def setUp(self):
3839
3940 def test_connect (self ):
4041 self .bm .connect ('123' , '/foo' )
41- self .assertIn (None , self .bm .rooms ['/foo' ])
42- self .assertIn ('123' , self .bm .rooms ['/foo' ])
43- self .assertIn ('123' , self .bm .rooms ['/foo' ][None ])
44- self .assertIn ('123' , self .bm .rooms ['/foo' ]['123' ])
45- self .assertEqual (self .bm .rooms ['/foo' ], {None : {'123' : True },
46- '123' : {'123' : True }})
42+ assert None in self .bm .rooms ['/foo' ]
43+ assert '123' in self .bm .rooms ['/foo' ]
44+ assert '123' in self .bm .rooms ['/foo' ][None ]
45+ assert '123' in self .bm .rooms ['/foo' ]['123' ]
46+ assert self .bm .rooms ['/foo' ] == {
47+ None : {'123' : True },
48+ '123' : {'123' : True },
49+ }
4750
4851 def test_pre_disconnect (self ):
4952 self .bm .connect ('123' , '/foo' )
5053 self .bm .connect ('456' , '/foo' )
5154 self .bm .pre_disconnect ('123' , '/foo' )
52- self .assertEqual ( self . bm .pending_disconnect , {'/foo' : ['123' ]})
53- self .assertFalse ( self . bm .is_connected ('123' , '/foo' ) )
55+ assert self .bm .pending_disconnect == {'/foo' : ['123' ]}
56+ assert not self .bm .is_connected ('123' , '/foo' )
5457 self .bm .pre_disconnect ('456' , '/foo' )
55- self .assertEqual ( self . bm .pending_disconnect , {'/foo' : ['123' , '456' ]})
56- self .assertFalse ( self . bm .is_connected ('456' , '/foo' ) )
58+ assert self .bm .pending_disconnect == {'/foo' : ['123' , '456' ]}
59+ assert not self .bm .is_connected ('456' , '/foo' )
5760 self .bm .disconnect ('123' , '/foo' )
58- self .assertEqual ( self . bm .pending_disconnect , {'/foo' : ['456' ]})
61+ assert self .bm .pending_disconnect == {'/foo' : ['456' ]}
5962 self .bm .disconnect ('456' , '/foo' )
60- self .assertEqual ( self . bm .pending_disconnect , {})
63+ assert self .bm .pending_disconnect == {}
6164
6265 def test_disconnect (self ):
6366 self .bm .connect ('123' , '/foo' )
6467 self .bm .connect ('456' , '/foo' )
6568 self .bm .enter_room ('123' , '/foo' , 'bar' )
6669 self .bm .enter_room ('456' , '/foo' , 'baz' )
6770 self .bm .disconnect ('123' , '/foo' )
68- self .assertEqual (self .bm .rooms ['/foo' ], {None : {'456' : True },
69- '456' : {'456' : True },
70- 'baz' : {'456' : True }})
71+ assert self .bm .rooms ['/foo' ] == {
72+ None : {'456' : True },
73+ '456' : {'456' : True },
74+ 'baz' : {'456' : True },
75+ }
7176
7277 def test_disconnect_default_namespace (self ):
7378 self .bm .connect ('123' , '/' )
7479 self .bm .connect ('123' , '/foo' )
7580 self .bm .connect ('456' , '/' )
7681 self .bm .connect ('456' , '/foo' )
77- self .assertTrue ( self . bm .is_connected ('123' , '/' ) )
78- self .assertTrue ( self . bm .is_connected ('123' , '/foo' ) )
82+ assert self .bm .is_connected ('123' , '/' )
83+ assert self .bm .is_connected ('123' , '/foo' )
7984 self .bm .disconnect ('123' , '/' )
80- self .assertFalse ( self . bm .is_connected ('123' , '/' ) )
81- self .assertTrue ( self . bm .is_connected ('123' , '/foo' ) )
85+ assert not self .bm .is_connected ('123' , '/' )
86+ assert self .bm .is_connected ('123' , '/foo' )
8287 self .bm .disconnect ('123' , '/foo' )
83- self .assertFalse (self .bm .is_connected ('123' , '/foo' ))
84- self .assertEqual (self .bm .rooms ['/' ], {None : {'456' : True },
85- '456' : {'456' : True }})
86- self .assertEqual (self .bm .rooms ['/foo' ], {None : {'456' : True },
87- '456' : {'456' : True }})
88+ assert not self .bm .is_connected ('123' , '/foo' )
89+ assert self .bm .rooms ['/' ] == {
90+ None : {'456' : True },
91+ '456' : {'456' : True },
92+ }
93+ assert self .bm .rooms ['/foo' ] == {
94+ None : {'456' : True },
95+ '456' : {'456' : True },
96+ }
8897
8998 def test_disconnect_twice (self ):
9099 self .bm .connect ('123' , '/' )
@@ -95,10 +104,14 @@ def test_disconnect_twice(self):
95104 self .bm .disconnect ('123' , '/foo' )
96105 self .bm .disconnect ('123' , '/' )
97106 self .bm .disconnect ('123' , '/foo' )
98- self .assertEqual (self .bm .rooms ['/' ], {None : {'456' : True },
99- '456' : {'456' : True }})
100- self .assertEqual (self .bm .rooms ['/foo' ], {None : {'456' : True },
101- '456' : {'456' : True }})
107+ assert self .bm .rooms ['/' ] == {
108+ None : {'456' : True },
109+ '456' : {'456' : True },
110+ }
111+ assert self .bm .rooms ['/foo' ] == {
112+ None : {'456' : True },
113+ '456' : {'456' : True },
114+ }
102115
103116 def test_disconnect_all (self ):
104117 self .bm .connect ('123' , '/foo' )
@@ -107,17 +120,17 @@ def test_disconnect_all(self):
107120 self .bm .enter_room ('456' , '/foo' , 'baz' )
108121 self .bm .disconnect ('123' , '/foo' )
109122 self .bm .disconnect ('456' , '/foo' )
110- self .assertEqual ( self . bm .rooms , {})
123+ assert self .bm .rooms == {}
111124
112125 def test_disconnect_with_callbacks (self ):
113126 self .bm .connect ('123' , '/' )
114127 self .bm .connect ('123' , '/foo' )
115128 self .bm ._generate_ack_id ('123' , '/' , 'f' )
116129 self .bm ._generate_ack_id ('123' , '/foo' , 'g' )
117130 self .bm .disconnect ('123' , '/foo' )
118- self . assertNotIn ( '/foo' , self .bm .callbacks ['123' ])
131+ assert '/foo' not in self .bm .callbacks ['123' ]
119132 self .bm .disconnect ('123' , '/' )
120- self . assertNotIn ( '123' , self .bm .callbacks )
133+ assert '123' not in self .bm .callbacks
121134
122135 def test_trigger_sync_callback (self ):
123136 self .bm .connect ('123' , '/' )
@@ -127,7 +140,7 @@ def test_trigger_sync_callback(self):
127140 id2 = self .bm ._generate_ack_id ('123' , '/foo' , cb )
128141 _run (self .bm .trigger_callback ('123' , '/' , id1 , ['foo' ]))
129142 _run (self .bm .trigger_callback ('123' , '/foo' , id2 , ['bar' , 'baz' ]))
130- self . assertEqual ( cb .call_count , 2 )
143+ assert cb .call_count == 2
131144 cb .assert_any_call ('foo' )
132145 cb .assert_any_call ('bar' , 'baz' )
133146
@@ -139,7 +152,7 @@ def test_trigger_async_callback(self):
139152 id2 = self .bm ._generate_ack_id ('123' , '/foo' , cb )
140153 _run (self .bm .trigger_callback ('123' , '/' , id1 , ['foo' ]))
141154 _run (self .bm .trigger_callback ('123' , '/foo' , id2 , ['bar' , 'baz' ]))
142- self . assertEqual ( cb .mock .call_count , 2 )
155+ assert cb .mock .call_count == 2
143156 cb .mock .assert_any_call ('foo' )
144157 cb .mock .assert_any_call ('bar' , 'baz' )
145158
@@ -152,26 +165,26 @@ def test_invalid_callback(self):
152165 _run (self .bm .trigger_callback ('124' , '/' , id , ['foo' ]))
153166 _run (self .bm .trigger_callback ('123' , '/foo' , id , ['foo' ]))
154167 _run (self .bm .trigger_callback ('123' , '/' , id + 1 , ['foo' ]))
155- self . assertEqual ( cb .mock .call_count , 0 )
168+ assert cb .mock .call_count == 0
156169
157170 def test_get_namespaces (self ):
158- self . assertEqual ( list (self .bm .get_namespaces ()), [])
171+ assert list (self .bm .get_namespaces ()) == []
159172 self .bm .connect ('123' , '/' )
160173 self .bm .connect ('123' , '/foo' )
161174 namespaces = list (self .bm .get_namespaces ())
162- self . assertEqual ( len (namespaces ), 2 )
163- self . assertIn ( '/' , namespaces )
164- self . assertIn ( '/foo' , namespaces )
175+ assert len (namespaces ) == 2
176+ assert '/' in namespaces
177+ assert '/foo' in namespaces
165178
166179 def test_get_participants (self ):
167180 self .bm .connect ('123' , '/' )
168181 self .bm .connect ('456' , '/' )
169182 self .bm .connect ('789' , '/' )
170183 self .bm .disconnect ('789' , '/' )
171- self . assertNotIn ( '789' , self .bm .rooms ['/' ][None ])
184+ assert '789' not in self .bm .rooms ['/' ][None ]
172185 participants = list (self .bm .get_participants ('/' , None ))
173- self . assertEqual ( len (participants ), 2 )
174- self . assertNotIn ( '789' , participants )
186+ assert len (participants ) == 2
187+ assert '789' not in participants
175188
176189 def test_leave_invalid_room (self ):
177190 self .bm .connect ('123' , '/foo' )
@@ -180,7 +193,7 @@ def test_leave_invalid_room(self):
180193
181194 def test_no_room (self ):
182195 rooms = self .bm .get_rooms ('123' , '/foo' )
183- self . assertEqual ([], rooms )
196+ assert [] == rooms
184197
185198 def test_close_room (self ):
186199 self .bm .connect ('123' , '/foo' )
@@ -189,7 +202,7 @@ def test_close_room(self):
189202 self .bm .enter_room ('123' , '/foo' , 'bar' )
190203 self .bm .enter_room ('123' , '/foo' , 'bar' )
191204 _run (self .bm .close_room ('bar' , '/foo' ))
192- self . assertNotIn ( 'bar' , self .bm .rooms ['/foo' ])
205+ assert 'bar' not in self .bm .rooms ['/foo' ]
193206
194207 def test_close_invalid_room (self ):
195208 self .bm .close_room ('bar' , '/foo' )
@@ -198,31 +211,40 @@ def test_rooms(self):
198211 self .bm .connect ('123' , '/foo' )
199212 self .bm .enter_room ('123' , '/foo' , 'bar' )
200213 r = self .bm .get_rooms ('123' , '/foo' )
201- self . assertEqual ( len (r ), 2 )
202- self . assertIn ( '123' , r )
203- self . assertIn ( 'bar' , r )
214+ assert len (r ) == 2
215+ assert '123' in r
216+ assert 'bar' in r
204217
205218 def test_emit_to_sid (self ):
206219 self .bm .connect ('123' , '/foo' )
207220 self .bm .connect ('456' , '/foo' )
208- _run (self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/foo' ,
209- room = '123' ))
221+ _run (
222+ self .bm .emit (
223+ 'my event' , {'foo' : 'bar' }, namespace = '/foo' , room = '123'
224+ )
225+ )
210226 self .bm .server ._emit_internal .mock .assert_called_once_with (
211- '123' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
227+ '123' , 'my event' , {'foo' : 'bar' }, '/foo' , None
228+ )
212229
213230 def test_emit_to_room (self ):
214231 self .bm .connect ('123' , '/foo' )
215232 self .bm .enter_room ('123' , '/foo' , 'bar' )
216233 self .bm .connect ('456' , '/foo' )
217234 self .bm .enter_room ('456' , '/foo' , 'bar' )
218235 self .bm .connect ('789' , '/foo' )
219- _run (self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/foo' ,
220- room = 'bar' ))
221- self .assertEqual (self .bm .server ._emit_internal .mock .call_count , 2 )
236+ _run (
237+ self .bm .emit (
238+ 'my event' , {'foo' : 'bar' }, namespace = '/foo' , room = 'bar'
239+ )
240+ )
241+ assert self .bm .server ._emit_internal .mock .call_count == 2
222242 self .bm .server ._emit_internal .mock .assert_any_call (
223- '123' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
243+ '123' , 'my event' , {'foo' : 'bar' }, '/foo' , None
244+ )
224245 self .bm .server ._emit_internal .mock .assert_any_call (
225- '456' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
246+ '456' , 'my event' , {'foo' : 'bar' }, '/foo' , None
247+ )
226248
227249 def test_emit_to_all (self ):
228250 self .bm .connect ('123' , '/foo' )
@@ -232,13 +254,16 @@ def test_emit_to_all(self):
232254 self .bm .connect ('789' , '/foo' )
233255 self .bm .connect ('abc' , '/bar' )
234256 _run (self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/foo' ))
235- self .assertEqual ( self . bm .server ._emit_internal .mock .call_count , 3 )
257+ assert self .bm .server ._emit_internal .mock .call_count == 3
236258 self .bm .server ._emit_internal .mock .assert_any_call (
237- '123' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
259+ '123' , 'my event' , {'foo' : 'bar' }, '/foo' , None
260+ )
238261 self .bm .server ._emit_internal .mock .assert_any_call (
239- '456' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
262+ '456' , 'my event' , {'foo' : 'bar' }, '/foo' , None
263+ )
240264 self .bm .server ._emit_internal .mock .assert_any_call (
241- '789' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
265+ '789' , 'my event' , {'foo' : 'bar' }, '/foo' , None
266+ )
242267
243268 def test_emit_to_all_skip_one (self ):
244269 self .bm .connect ('123' , '/foo' )
@@ -247,13 +272,18 @@ def test_emit_to_all_skip_one(self):
247272 self .bm .enter_room ('456' , '/foo' , 'bar' )
248273 self .bm .connect ('789' , '/foo' )
249274 self .bm .connect ('abc' , '/bar' )
250- _run (self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/foo' ,
251- skip_sid = '456' ))
252- self .assertEqual (self .bm .server ._emit_internal .mock .call_count , 2 )
275+ _run (
276+ self .bm .emit (
277+ 'my event' , {'foo' : 'bar' }, namespace = '/foo' , skip_sid = '456'
278+ )
279+ )
280+ assert self .bm .server ._emit_internal .mock .call_count == 2
253281 self .bm .server ._emit_internal .mock .assert_any_call (
254- '123' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
282+ '123' , 'my event' , {'foo' : 'bar' }, '/foo' , None
283+ )
255284 self .bm .server ._emit_internal .mock .assert_any_call (
256- '789' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
285+ '789' , 'my event' , {'foo' : 'bar' }, '/foo' , None
286+ )
257287
258288 def test_emit_to_all_skip_two (self ):
259289 self .bm .connect ('123' , '/foo' )
@@ -262,25 +292,37 @@ def test_emit_to_all_skip_two(self):
262292 self .bm .enter_room ('456' , '/foo' , 'bar' )
263293 self .bm .connect ('789' , '/foo' )
264294 self .bm .connect ('abc' , '/bar' )
265- _run (self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/foo' ,
266- skip_sid = ['123' , '789' ]))
267- self .assertEqual (self .bm .server ._emit_internal .mock .call_count , 1 )
295+ _run (
296+ self .bm .emit (
297+ 'my event' ,
298+ {'foo' : 'bar' },
299+ namespace = '/foo' ,
300+ skip_sid = ['123' , '789' ],
301+ )
302+ )
303+ assert self .bm .server ._emit_internal .mock .call_count == 1
268304 self .bm .server ._emit_internal .mock .assert_any_call (
269- '456' , 'my event' , {'foo' : 'bar' }, '/foo' , None )
305+ '456' , 'my event' , {'foo' : 'bar' }, '/foo' , None
306+ )
270307
271308 def test_emit_with_callback (self ):
272309 self .bm .connect ('123' , '/foo' )
273310 self .bm ._generate_ack_id = mock .MagicMock ()
274311 self .bm ._generate_ack_id .return_value = 11
275- _run (self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/foo' ,
276- callback = 'cb' ))
312+ _run (
313+ self .bm .emit (
314+ 'my event' , {'foo' : 'bar' }, namespace = '/foo' , callback = 'cb'
315+ )
316+ )
277317 self .bm ._generate_ack_id .assert_called_once_with ('123' , '/foo' , 'cb' )
278318 self .bm .server ._emit_internal .mock .assert_called_once_with (
279- '123' , 'my event' , {'foo' : 'bar' }, '/foo' , 11 )
319+ '123' , 'my event' , {'foo' : 'bar' }, '/foo' , 11
320+ )
280321
281322 def test_emit_to_invalid_room (self ):
282- _run (self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/' ,
283- room = '123' ))
323+ _run (
324+ self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/' , room = '123' )
325+ )
284326
285327 def test_emit_to_invalid_namespace (self ):
286328 _run (self .bm .emit ('my event' , {'foo' : 'bar' }, namespace = '/foo' ))
0 commit comments