forked from miguelgrinberg/python-socketio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_namespace.py
More file actions
129 lines (108 loc) · 4.71 KB
/
Copy pathtest_namespace.py
File metadata and controls
129 lines (108 loc) · 4.71 KB
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
import unittest
import six
if six.PY3:
from unittest import mock
else:
import mock
from socketio import namespace
class TestNamespace(unittest.TestCase):
def test_connect_event(self):
result = {}
class MyNamespace(namespace.Namespace):
def on_connect(self, sid, environ):
result['result'] = (sid, environ)
ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock())
ns.trigger_event('connect', 'sid', {'foo': 'bar'})
self.assertEqual(result['result'], ('sid', {'foo': 'bar'}))
def test_disconnect_event(self):
result = {}
class MyNamespace(namespace.Namespace):
def on_disconnect(self, sid):
result['result'] = sid
ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock())
ns.trigger_event('disconnect', 'sid')
self.assertEqual(result['result'], 'sid')
def test_event(self):
result = {}
class MyNamespace(namespace.Namespace):
def on_custom_message(self, sid, data):
result['result'] = (sid, data)
ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock())
ns.trigger_event('custom_message', 'sid', {'data': 'data'})
self.assertEqual(result['result'], ('sid', {'data': 'data'}))
def test_event_not_found(self):
result = {}
class MyNamespace(namespace.Namespace):
def on_custom_message(self, sid, data):
result['result'] = (sid, data)
ns = MyNamespace('/foo')
ns._set_server(mock.MagicMock())
ns.trigger_event('another_custom_message', 'sid', {'data': 'data'})
self.assertEqual(result, {})
def test_emit(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.emit('ev', data='data', room='room', skip_sid='skip',
callback='cb')
ns.server.emit.assert_called_with(
'ev', data='data', room='room', skip_sid='skip', namespace='/foo',
callback='cb')
ns.emit('ev', data='data', room='room', skip_sid='skip',
namespace='/bar', callback='cb')
ns.server.emit.assert_called_with(
'ev', data='data', room='room', skip_sid='skip', namespace='/bar',
callback='cb')
def test_send(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.send(data='data', room='room', skip_sid='skip', callback='cb')
ns.server.send.assert_called_with(
'data', room='room', skip_sid='skip', namespace='/foo',
callback='cb')
ns.send(data='data', room='room', skip_sid='skip', namespace='/bar',
callback='cb')
ns.server.send.assert_called_with(
'data', room='room', skip_sid='skip', namespace='/bar',
callback='cb')
def test_enter_room(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.enter_room('sid', 'room')
ns.server.enter_room.assert_called_with('sid', 'room',
namespace='/foo')
ns.enter_room('sid', 'room', namespace='/bar')
ns.server.enter_room.assert_called_with('sid', 'room',
namespace='/bar')
def test_leave_room(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.leave_room('sid', 'room')
ns.server.leave_room.assert_called_with('sid', 'room',
namespace='/foo')
ns.leave_room('sid', 'room', namespace='/bar')
ns.server.leave_room.assert_called_with('sid', 'room',
namespace='/bar')
def test_close_room(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.close_room('room')
ns.server.close_room.assert_called_with('room', namespace='/foo')
ns.close_room('room', namespace='/bar')
ns.server.close_room.assert_called_with('room', namespace='/bar')
def test_rooms(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.rooms('sid')
ns.server.rooms.assert_called_with('sid', namespace='/foo')
ns.rooms('sid', namespace='/bar')
ns.server.rooms.assert_called_with('sid', namespace='/bar')
def test_disconnect(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.disconnect('sid')
ns.server.disconnect.assert_called_with('sid', namespace='/foo')
ns.disconnect('sid', namespace='/bar')
ns.server.disconnect.assert_called_with('sid', namespace='/bar')