-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaketestdb.py
170 lines (127 loc) · 4.16 KB
/
maketestdb.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
#!/usr/bin/python3
#from club_database import *
from tinydb import TinyDB, Query
from tinydb.operations import delete
#
# Make a few test databases for tinymanger.py
#
#
####################################################################
#
# Test 1: a "uniform" db
#
dbfname = 'test1.json'
#dbfname = 'testdb.json'
db = TinyDB(dbfname)
q = Query()
for r in db:
db.remove(q.key1.exists()) # clear old copy
rec1 = {'key1': 5, 'key2':4, 'key3':3, 'key4':2,'key5':1}
rec2 = {'key1': 5, 'key5':4, 'key3':3, 'key4':2,'key2':1}
for i in range(30):
if i%10==0:
r = rec2
else:
r = rec1
db.insert(r)
####################################################################
#
# Test 2: every 10th rec has an extra key
#
dbfname = 'test2.json'
#dbfname = 'testdb.json'
db = TinyDB(dbfname)
for r in db:
db.remove(q.key1.exists()) # clear old copy
rec1 = {'key1':5, 'key2':4, 'key3':3, 'key4':2, 'key5':1}
rec2 = {'key0':0, 'key1':5, 'key5':4, 'key3':3, 'key4':2, 'key2':1}
for i in range(30):
if i==0:
r = rec1
elif i%10==0:
r = rec2
else:
r = rec1
db.insert(r)
####################################################################
#
# Test 3: two records have wrong types for key1 (str->int)
#
dbfname = 'test3.json'
#dbfname = 'testdb.json'
db = TinyDB(dbfname)
for r in db:
db.remove(q.key1.exists()) # clear old copy
rec = {'key1':'John Smith', 'key2':'127', 'key3':560, 'key4': ['a', 'b', 'c'], 'key5':4.2379}
ids = []
for i in range(100):
ids.append(db.insert(rec))
print ('Length of test3.json: ', len(db))
#make non uniform key
modids = [ids[3], ids[12]]
print ('modifying ids: ', modids)
db.update({'key1':5},doc_ids=modids) # key1 is now not uniform
####################################################################
#
# Test 4: some records have missing keys and others wrong types
#
dbfname = 'test4.json'
#dbfname = 'testdb.json'
db = TinyDB(dbfname)
for r in db:
db.remove(q.key1.exists()) # clear old copy if any
rec = {'key1':'John Smith', 'key2':'127', 'key3':560, 'key4': ['a', 'b', 'c'], 'key5':4.2379}
rec2 = {'key1':'John Smith', 'key3':560, 'key4': ['a', 'b', 'c'], 'key5':4.2379} # key2 missing
ids = []
for i in range(100):
ids.append(db.insert(rec))
print ('Length of test3.json: ', len(db))
#make non uniform key
modids = [ids[3], ids[12]] # 'randomly' pick 3rd, and 12th doc_ids to mess with
print ('modifying ids: ', modids)
db.update({'key1':5},doc_ids=modids) # key1 is now not uniform type
print('deleting key2 from ', ids[14], ids[18])
db.update(delete('key2'), doc_ids=[ids[14],ids[18]])
####################################################################
#
# Test 5: some records have both missing keys and extra keys
#
dbfname = 'test5.json'
#dbfname = 'testdb.json'
db = TinyDB(dbfname)
for r in db:
db.remove(q.key1.exists()) # clear old copy if any
rec = {'key1':'John Smith', 'key2':'127', 'key3':560, 'key4': ['a', 'b', 'c'], 'key5':4.2379}
rec2 = {'key1':'John Smith', 'key3':560, 'key4': ['a', 'b', 'c'], 'key5':4.2379, 'keyxx': 25} #
ids = []
for i in range(100):
ids.append(db.insert(rec))
print ('Length of test3.json: ', len(db))
#make non uniform key
modids = [ids[5], ids[27]] # 'randomly' pick 5th , 27th doc_ids to mess with
print ('modifying ids: ', modids)
for id in modids:
db.insert(rec2) # add some wierd records
####################################################################
#
# Test 6: Test db with multiple tables
# (table 2 has flaws)
#
dbfname = 'test6.json'
#dbfname = 'testdb.json'
db = TinyDB(dbfname)
q = Query()
tables = ['test_table1','test_table2','test_table3']
for t in tables:
dbt = db.table(t)
dbt.truncate() #clear it out
rec1 = {'key1': 5, 'key2':4, 'key3':3, 'key4':2,'key5':1}
rec2 = {'keyZ': 5, 'key5':'777', 'key3':3, 'key4':2,'key2':1}
r = rec1
for i in range(30):
if t == 'test_table2':
if i%10==0:
r = rec2
else:
r = rec1
dbt.insert(r)