-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathm1_tester.py
65 lines (57 loc) · 2.22 KB
/
m1_tester.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
from template.db import Database
from template.query import Query
from template.config import init
from random import choice, randint, sample, seed
from colorama import Fore, Back, Style
# Student Id and 4 grades
init()
db = Database()
grades_table = db.create_table('Grades', 5, 0)
query = Query(grades_table)
records = {}
seed(3562901)
for i in range(0, 1000):
key = 92106429 + randint(0, 9000)
while key in records:
key = 92106429 + randint(0, 9000)
records[key] = [key, randint(0, 20), randint(0, 20), randint(0, 20), randint(0, 20)]
query.insert(*records[key])
print('inserted', records[key])
for key in records:
record = query.select(key, 0, [1, 1, 1, 1, 1])[0]
error = False
for i, column in enumerate(record.columns):
if column != records[key][i]:
error = True
if error:
print('select error on', key, ':', record, ', correct:', records[key])
else:
print('select on', key, ':', record)
for key in records:
updated_columns = [None, None, None, None, None]
for i in range(1, grades_table.num_columns):
value = randint(0, 20)
updated_columns[i] = value
original = records[key].copy()
records[key][i] = value
query.update(key, *updated_columns)
record = query.select(key, 0, [1, 1, 1, 1, 1])[0]
error = False
for j, column in enumerate(record.columns):
if column != records[key][j]:
error = True
if error:
print('update error on', original, 'and', updated_columns, ':', record, ', correct:', records[key])
else:
print('update on', original, 'and', updated_columns, ':', record)
updated_columns[i] = None
keys = sorted(list(records.keys()))
for c in range(0, grades_table.num_columns):
for i in range(0, 20):
r = sorted(sample(range(0, len(keys)), 2))
column_sum = sum(map(lambda key: records[key][c], keys[r[0]: r[1] + 1]))
result = query.sum(keys[r[0]], keys[r[1]], c)
if column_sum != result:
print('sum error on [', keys[r[0]], ',', keys[r[1]], ']: ', result, ', correct: ', column_sum)
else:
print('sum on [', keys[r[0]], ',', keys[r[1]], ']: ', column_sum)