-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_db.py
209 lines (189 loc) · 6.5 KB
/
test_db.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import shutil
import database
import pytest
TEST_DB = "example_dbs/reg.sqlite"
TEST_DB2 = "example_dbs/reg_test.sqlite" # Use this for anything that modifies db
class TestDatabase:
@pytest.fixture
def setup(self):
if not os.path.exists(TEST_DB2):
shutil.copy(TEST_DB, TEST_DB2)
yield
os.remove(TEST_DB2)
def test_get_tables(self):
tables = database.get_table_names(TEST_DB)
ref = ['classes', 'courses', 'coursesprofs', 'crosslistings', 'profs']
assert tables == ref
def test_get_columns(self):
columns = database.get_columns(TEST_DB, "classes")
ref = ['classid', 'courseid', 'days', 'starttime', 'endtime', 'bldg', 'roomnum']
assert columns == ref
def test_get_types(self):
columns = database.get_columns(TEST_DB, "classes")
ref = {
'classid': "INTEGER",
'courseid': "INTEGER",
'days': "TEXT",
'starttime': "TEXT",
'endtime': "TEXT",
'bldg': "TEXT",
'roomnum': "TEXT"}
types = database.get_column_data_types(TEST_DB, "classes")
assert len(types) == len(ref)
for key, val in ref.items():
assert types[key] == val
def test_get_all(self):
rows = database.get_all(TEST_DB, "classes")
ref_len = 1494
ref_row = {
'classid': 7838,
'courseid': 3457,
'days': 'M',
'starttime': '01:30 PM',
'endtime': '04:20 PM',
'bldg': 'CHANC',
'roomnum': '105'
}
assert len(rows) == ref_len
for key, val in rows[0].items():
assert ref_row[key] == val
def test_update(self, setup):
update_vals = {
'days': 'TEST!!!',
'roomnum': 'TEST'
}
id_vals = {
'classid': 7838,
'courseid': 3457
}
ref_len = 1494
ref_row = {
'classid': 7838,
'courseid': 3457,
'days': 'TEST!!!',
'starttime': '01:30 PM',
'endtime': '04:20 PM',
'bldg': 'CHANC',
'roomnum': 'TEST'
}
original_row = {
'classid': 7838,
'courseid': 3457,
'days': 'M',
'starttime': '01:30 PM',
'endtime': '04:20 PM',
'bldg': 'CHANC',
'roomnum': '105'
}
database.update(TEST_DB2, "classes", update_vals, id_vals)
rows = database.get_all(TEST_DB2, "classes")
assert len(rows) == ref_len
for key, val in rows[0].items():
assert ref_row[key] == val
database.update(TEST_DB2, "classes", original_row, rows[0])
rows = database.get_all(TEST_DB2, "classes")
assert len(rows) == ref_len
for key, val in rows[0].items():
assert original_row[key] == val
def test_delete(self, setup):
ref_len = 1493
del_row = {
'classid': 7838,
'courseid': 3457,
'days': 'M',
'starttime': '01:30 PM',
'endtime': '04:20 PM',
'bldg': 'CHANC',
'roomnum': '105'
}
ref_row = {
'classid': 7839,
'courseid': 3458,
'days': 'W',
'starttime': '07:30 PM',
'endtime': '10:20 PM',
'bldg': 'MARXH',
'roomnum': '101'
}
database.delete(TEST_DB2, "classes", del_row)
rows = database.get_all(TEST_DB2, "classes")
assert len(rows) == ref_len
for key, val in rows[0].items():
assert ref_row[key] == val
def test_create(self, setup):
ref_len = 1495
new_row = {
'classid': 0000,
'courseid': 0000,
'days': 'TEST',
'starttime': '00:00 PM',
'endtime': '00:00 PM',
'bldg': 'TEST',
'roomnum': '1234'
}
ref_row = {
'classid': 7838,
'courseid': 3457,
'days': 'M',
'starttime': '01:30 PM',
'endtime': '04:20 PM',
'bldg': 'CHANC',
'roomnum': '105'
}
database.insert(TEST_DB2, "classes", new_row)
rows = database.get_all(TEST_DB2, "classes")
assert len(rows) == ref_len
for key, val in rows[0].items():
assert ref_row[key] == val
for key, val in rows[-1].items():
assert new_row[key] == val
def test_join(self):
ref_len = 1648
ref_row = {
'courseid': 3457,
'area': "LA",
'title': 'Music from the Hispanophone Caribbean',
'prereqs': '',
'dept': 'AAS',
'coursenum': '310',
'profid': 481,
'profname': 'Alexandra T. Vazquez'
}
prim_table = 'courses'
tables = ['crosslistings', 'coursesprofs', 'profs']
identifiers = [[('courses', 'courseid', 'crosslistings', 'courseid')],
[('courses', 'courseid', 'coursesprofs', 'courseid')],
[('coursesprofs', 'profid', 'profs', 'profid')]]
rows = database.join(TEST_DB, prim_table, tables, identifiers)
assert len(rows) == ref_len
for key, val in ref_row.items():
assert rows[0][key] == val
@pytest.mark.stress
def test_stress1(self, setup):
iters = 1
ref_len = 1494
for i in range(iters):
rows = database.get_all(TEST_DB2, 'classes')
for row in rows:
database.delete(TEST_DB2, 'classes', row)
rows_deleted = database.get_all(TEST_DB2, 'classes')
assert len(rows_deleted) == 0
for row in rows:
database.insert(TEST_DB2, 'classes', row)
rows_restored = database.get_all(TEST_DB2, 'classes')
assert len(rows_restored) == ref_len
@pytest.mark.stress
def test_stress2(self, setup):
iters = 2
ref_len = 1494
for i in range(iters):
rows = database.get_all(TEST_DB2, 'classes')
for row in rows:
database.delete(TEST_DB2, 'classes', row)
rows_deleted = database.get_all(TEST_DB2, 'classes')
assert len(rows_deleted) == 0
for row in rows:
database.insert(TEST_DB2, 'classes', row)
rows_restored = database.get_all(TEST_DB2, 'classes')
assert len(rows_restored) == ref_len