-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
150 lines (119 loc) · 4.98 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import unittest
import json
from room import RoomContainers, RoomNotExistingException, Room, InvalidMessageException, InvalidStatusMessageException
class RoomContainersTest(unittest.TestCase):
def cleanup(self):
RoomContainers._instance = None
def test_singleton(self):
room_containers = RoomContainers()
self.assertEqual(id(room_containers), id(RoomContainers()))
self.cleanup()
def test_add_room_no_key(self):
room_containers = RoomContainers()
room = room_containers.add_room()
self.assertNotEqual(room.container_key, None)
self.cleanup()
def test_add_room_key_ok(self):
room_containers = RoomContainers()
room = room_containers.add_room("AAAA")
self.assertNotEqual(room, None)
self.assertEqual(room.container_key, "AAAA")
self.cleanup()
def test_get_room(self):
room_containers = RoomContainers()
rooms = room_containers.add_room(key="ABC")
room = room_containers.get_room("ABC")
self.assertEqual(room.container_key, "ABC")
self.cleanup()
def test_get_room_exception(self):
room_containers = RoomContainers()
rooms = room_containers.add_room(key="ABC")
with self.assertRaises(RoomNotExistingException):
room_containers.get_room("ABCD")
self.cleanup()
def test_remove_room(self):
room_containers = RoomContainers()
rooms = room_containers.add_room(key="ABC")
room = room_containers.remove_room("ABC")
self.assertEqual(room.container_key, "ABC")
self.assertEqual(len(room_containers.rooms), 0)
self.cleanup()
def test_get_room_exception(self):
room_containers = RoomContainers()
rooms = room_containers.add_room(key="ABC")
with self.assertRaises(RoomNotExistingException):
room_containers.remove_room("ABCD")
self.cleanup()
class RoomTest(unittest.TestCase):
def test_new_room(self):
room = Room("KEY")
assert room.container_key
assert room.votes == dict()
def test_handle_message_no_message(self):
room = Room("KEY")
with self.assertRaises(InvalidMessageException):
room.handle_message()
def test_handle_message_no_message_status(self):
room = Room("KEY")
with self.assertRaises(InvalidStatusMessageException):
room.handle_message(json.dumps({'aaa':1}))
def test_handle_message_connect(self):
room = Room("KEY")
data = dict(status="connect", user="franco", room_key="KEY")
data = room.handle_message(json.dumps(data))
assert len(data) == 1
data = json.loads(data[0])
assert data['status'] == 'users'
assert data['user'] == 'franco'
assert len(room.votes) == 1
def test_handle_message_reset(self):
room = Room("KEY")
data = dict(status="reset", user="franco", room_key="KEY")
room.votes = dict(franco=None)
data = room.handle_message(json.dumps(data))
assert len(data) == 1
data = json.loads(data[0])
assert data['status'] == 'reset'
assert len(room.votes) == 1
assert room.votes['franco'] is None
def test_handle_message_reset_all(self):
room = Room("KEY")
data = dict(status="reset_all", user="franco", room_key="KEY")
room.votes = dict(franco=None)
data = room.handle_message(json.dumps(data))
assert len(data) == 1
data = json.loads(data[0])
assert data['status'] == 'reset'
assert len(room.votes) == 0
def test_handle_message_vote(self):
room = Room("KEY")
data = dict(status="vote", user="franco", value='2', room_key="KEY")
room.votes = dict(franco=None)
data = room.handle_message(json.dumps(data))
assert room.votes['franco'] == '2'
assert len(data) == 0
def test_handle_message_disconnect(self):
room = Room("KEY")
data = dict(status="disconnect", user="franco", room_key="KEY")
room.votes = dict(franco=None)
data = room.handle_message(json.dumps(data))
assert room.votes == dict()
assert len(data) == 2
def test_handle_message_block(self):
room = Room("KEY")
data = dict(status="block", room_key="KEY")
room.votes = dict(franco=None)
data = room.handle_message(json.dumps(data))
assert len(room.votes) == 1
assert len(data) == 1
def test_handle_message_reveal(self):
room = Room("KEY")
data = dict(status="reveal", room_key="KEY")
room.votes = dict(franco=1, mike=2)
data = room.handle_message(json.dumps(data))
assert len(room.votes) == 2
assert len(data) == 1
print(json.loads(data[0])['value'])
assert json.loads(data[0])['value'] == [{'key': 1, 'value': ['franco']}, {'key':2, 'value':['mike']}]
if __name__ == '__main__':
unittest.main()