-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
133 lines (95 loc) · 3.72 KB
/
test.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
import unittest, os, sys,sqlite3, random, string
from quick_tools import create_db, add_user, add_room, get_users, get_rooms,delete_user,delete_room, verify_room_type,verify_room_name,verify_user_password
db_path = 'quick_chat.db'
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
c = conn.cursor()
class QuickToolsTester(unittest.TestCase):
def test1_create_db(self):
c.execute('DROP TABLE IF EXISTS Rooms;')
c.execute('DROP TABLE IF EXISTS Users;')
create_db(db_path)
sql = "select * from Users;"
table_name = ''
for row in c.execute(sql).fetchall():
table_name = row[0]
self.assertEqual(table_name,'')
sql = "select * from Rooms;"
table_name = ''
for row in c.execute(sql).fetchall():
table_name = row[0]
self.assertEqual(table_name,'')
def test2_verify_user_password(self):
self.assertFalse(verify_room_name('qwer')) # not long enough
self.assertFalse(verify_room_name('qwer123456')) # no special character
random_str_len = random.randint(5,10)
correct_password = ''.join(random.choice(string.ascii_lowercase) for i in range(random_str_len))
correct_password += '123456,'
self.assertTrue(verify_user_password(correct_password))
def test3_add_user(self):
add_user(db_path,'yann.c',0,0,'qwer123456,') # add a correct user
sql = "select user_name from Users where user_name = 'yann.c';"
user_name = ''
for row in c.execute(sql):
user_name = row[0]
self.assertEqual(user_name,'yann.c')
add_user(db_path,'huiling.b',0,0,'pass') # add a user with wrong password format
sql = "select user_name from Users where user_name = 'huiling.b';"
name = ''
for row in c.execute(sql):
name = row[0]
self.assertEqual(name,'')
def test4_get_users(self):
self.assertEqual(get_users(db_path),['yann.c']) # Have to be able to get the user added
def test5_delete_user(self):
delete_user(db_path,'yann.c')
sql = "select user_name from Users where user_name = 'yann.c';"
none = ''
for row in c.execute(sql):
none = row[0]
self.assertEqual(none,'') # Successful delete the user
def test6_verify_room_name(self):
self.assertFalse(verify_room_name('false'))
self.assertFalse(verify_room_name('ROOM_'))
random_str_len = random.randint(3,20)
correct_room = 'ROOM_'
correct_room += ''.join(random.choice(string.ascii_lowercase) for i in range(random_str_len))
self.assertTrue(verify_room_name(correct_room))
def test7_verify_room_type(self):
self.assertFalse(verify_room_type('false_type'))
self.assertTrue(verify_room_type('public'))
self.assertTrue(verify_room_type('private'))
def test8_add_room(self):
add_room(db_path,'ROOM_dinningroom','public') # correct room
sql = "select room_name from Rooms where room_name = 'ROOM_dinningroom';"
for row in c.execute(sql):
room_name = row[0]
self.assertEqual(room_name,'ROOM_dinningroom')
add_room(db_path,'ROOM_bedroom','unknown') # wrong room type
sql = "select room_name from Rooms where room_name = 'ROOM_bedroom';"
res = ''
for row in c.execute(sql):
res = row[0]
self.assertEqual(res,'')
add_room(db_path,'ROOM_no','private') # wrong room name
sql = "select room_name from Rooms where room_name = 'ROOM_no';"
res = ''
for row in c.execute(sql):
res = row[0]
self.assertEqual(res,'')
def test9_get_rooms(self):
self.assertEqual(get_rooms(db_path),['ROOM_dinningroom'])
def test10_delete_room(self):
delete_room(db_path,'ROOM_dinningroom')
sql = "select room_name from Rooms where room_name = 'ROOM_dinningroom';"
none = ''
for row in c.execute(sql):
none = row[0]
self.assertEqual(none,'')
if __name__ == '__main__':
unittest.main()
conn.commit()
conn.close()
# add_room('quick_chat.db','room0','public')
# print(get_users(db_path))
# print(get_rooms(db_path))