-
Notifications
You must be signed in to change notification settings - Fork 10
/
tests.py
90 lines (70 loc) · 2.65 KB
/
tests.py
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
import unittest
from sse import Publisher
class TestPublisher(unittest.TestCase):
def read(self, generator):
return ''.join(generator).strip()
def test_single(self):
p = Publisher()
s = p.subscribe()
p.publish('test')
p.close()
self.assertEqual(self.read(s), 'data: test')
def test_multiple(self):
p = Publisher()
s1 = p.subscribe()
s2 = p.subscribe()
s3 = p.subscribe()
p.publish('test')
p.close()
self.assertEqual(self.read(s1), 'data: test')
self.assertEqual(self.read(s2), 'data: test')
self.assertEqual(self.read(s3), 'data: test')
def test_channel(self):
p = Publisher()
s1 = p.subscribe('channel 1')
s2 = p.subscribe('channel 2')
s3 = p.subscribe(['channel 1', 'channel 2'])
p.publish('test1', 'channel 1')
p.publish('test2', 'channel 2')
p.close()
self.assertEqual(self.read(s1), 'data: test1')
self.assertEqual(self.read(s2), 'data: test2')
self.assertEqual(self.read(s3), 'data: test1\n\ndata: test2')
def test_custom(self):
p = Publisher()
s1 = p.subscribe(properties=1)
s2 = p.subscribe(properties=2)
p.publish(lambda properties: properties)
p.close()
self.assertEqual(self.read(s1), 'data: 1')
self.assertEqual(self.read(s2), 'data: 2')
def test_unsubscribe(self):
p = Publisher()
self.assertEqual(len(p.subscribers_by_channel['default channel']), 0)
p.subscribe(properties=1)
p.subscribe(properties=2)
self.assertEqual(len(p.subscribers_by_channel['default channel']), 2)
p.unsubscribe(properties=2)
self.assertEqual(len(p.subscribers_by_channel['default channel']), 1)
p.subscribe(properties={'id': 3, 'eggs': 'spam'})
self.assertEqual(len(p.subscribers_by_channel['default channel']), 2)
p.unsubscribe(properties={'id': 3, 'eggs': 'spam'})
self.assertEqual(len(p.subscribers_by_channel['default channel']), 1)
p.close()
def test_initial_data(self):
p = Publisher()
s = p.subscribe(initial_data=['start 1', 'start 2'])
p.publish('test')
p.close()
self.assertEqual(self.read(s),
'data: start 1\n\n'
'data: start 2\n\n'
'data: test')
def test_multiline(self):
p = Publisher()
s = p.subscribe()
p.publish('line 1\nline 2')
p.close()
self.assertEqual(self.read(s), 'data: line 1\ndata: line 2')
if __name__ == '__main__':
unittest.main()