forked from sungkhum/MicroPyDatabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_test.py
642 lines (581 loc) · 23.4 KB
/
device_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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
"""
A file that contains tests of MicroPyDatabase features to ensure
nothing is broken when updates are written.
Run on device with:
with open("device_test.py") as f:
exec(f.read(), globals())
"""
import micropydatabase as mdb
import gc
import time
import sys
# Is device microcontroller
uC = True if sys.platform not in ("unix", "linux", "win32") else False
def test_database_open_exception():
try:
mdb.Database.open("dabatase_not_exist")
except Exception:
return 'Success.'
else:
return 'Error.'
def test_database_creation():
try:
gc.collect()
if uC:
before = gc.mem_free()
start_time = time.ticks_ms()
mdb.Database.create("testdb")
gc.collect()
if uC:
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Database creation took", end_time, "ms to run")
print("Database creation took up", before - after, "bytes")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_database_creation_exception():
try:
mdb.Database.create("testdb")
except Exception:
return 'Success.'
else:
return 'Error.'
def test_table_open_exception():
try:
db_object = mdb.Database.open("testdb")
db_object.open_table("testblahtable")
except Exception:
return 'Success.'
else:
return 'Error.'
def test_table_creation():
try:
db_object = mdb.Database.open("testdb")
db_object.create_table("testtable", ["name", "password"])
except Exception:
return 'Error.'
else:
return 'Success.'
def test_table_open():
try:
db_object = mdb.Database.open("testdb")
db_object.open_table("testtable")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_insert_row():
i = 1
try:
all_memory = []
all_time = []
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
for x in range(550):
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.insert({"name": "bob", "password": "coolpassword"})
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
all_memory.append(before - after)
all_time.append(end_time)
i += 1
except Exception:
return 'Error.'
else:
if uC:
print("Average memory used per insert was",
sum(all_memory) / len(all_memory), "bytes.")
print("Average time per insert was",
sum(all_time) / len(all_time), "ms.")
all_time.sort()
all_memory.sort()
print("Most memory used was", all_memory[-1], "bytes.")
print("Longest insert time was", all_time[-1], "ms.")
return 'Success.'
def test_insert_multiple_rows():
try:
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_object = mdb.Database.open("testdb")
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Database open took", end_time, "ms to run.")
print("Database open took up", before - after, "bytes.")
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table = db_object.open_table("testtable")
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Opening table with 550 rows took", end_time, "ms.")
print("Opening table with 550 rows took", before - after, "bytes.")
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.insert([{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"}])
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Multi-inserting 28 rows took", end_time, "ms to run.")
print("Multi-inserting 28 rows took up", before - after, "bytes.")
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.insert([{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"},
{"name": "whothere", "password": "ohyeah"}])
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Multi-inserting 30 rows took", end_time, "ms to run.")
print("Multi-inserting 30 rows took up", before - after, "bytes.")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_update_row_exception_row():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.update_row(1000, {"name": "john"})
except Exception:
return 'Success.'
else:
return 'Error.'
def test_update_row_exception_column():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.update_row(300, {"what": "john"})
except Exception:
return 'Success.'
else:
return 'Error.'
def test_update_row():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.update_row(300, {"name": "george", "password": "anotherone"})
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Updating by row id took", end_time, "ms to run.")
print("Updating by row id took", before - after, "bytes.")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_update():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.update({"name": "george", "password": "anotherone"},
{"name": "sally", "password": "whatisthis"})
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Updating by query (at row 300) took",
end_time, "ms to run.")
print("Updating by query (at row 300) took",
before - after, "bytes.")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_update_exception():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.update({"name": "whowhowho", "password": "anotherone"},
{"name": "sally", "password": "whatisthis"})
except Exception:
return 'Success.'
else:
return 'Error.'
def test_delete_row():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.delete_row(200)
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Delete row by row id took", end_time, "ms to run.")
print("Delete row by row id took", before - after, "bytes.")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_delete_row_exception():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.delete_row(3300)
except Exception:
return 'Success.'
else:
return 'Error.'
def test_delete():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.delete({"name": "sally", "password": "whatisthis"})
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Delete by query (at row 300) took", end_time, "ms to run.")
print("Delete by query (at row 300) took", before - after, "bytes.")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_delete_exception():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.delete({"name": "sallywho", "password": "whatisthis"})
except Exception:
return 'Success.'
else:
return 'Error.'
def test_find_row():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.find_row(150)
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Find by row id (row 150) took", end_time, "ms to run.")
print("Find by row id (row 150) took", before - after, "bytes.")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_find_row_exception():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.find_row(4500)
except Exception:
return 'Success.'
else:
return 'Error.'
def test_query():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.update_row(250, {"name": "blah", "password": "something"})
db_table.update_row(101, {"name": "blah", "password": "something"})
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
return_list = db_table.query({"name": "blah", "password": "something"})
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Query with two results (row 101 and 250) took",
end_time, "ms to run.")
print("Query with two results (row 101 and 250) took",
before - after, "bytes.")
except Exception:
return 'Error.'
if len(return_list) == 2:
return 'Success.'
else:
return 'Error.'
def test_find():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
db_table.find({"name": "blah", "password": "something"})
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Find by query (row 101) took", end_time, "ms to run.")
print("Find by query (row 101) took", before - after, "bytes.")
except Exception:
return 'Error.'
else:
return 'Success.'
def test_scan_no_query():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.update_row(1, {"name": "tom", "password": "alright"})
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
scan_return = db_table.scan()
the_scan = scan_return.__next__()
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Scan returning first row took", end_time, "ms to run.")
print("Scan returning first row took", before - after, "bytes.")
except Exception:
return 'Error.'
if the_scan['name'] == 'tom':
return 'Success.'
else:
return 'Error.'
def test_scan_with_query():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
if uC:
gc.collect()
before = gc.mem_free()
start_time = time.ticks_ms()
scan_return = db_table.scan({"name": "blah", "password": "something"})
the_scan = scan_return.__next__()
if uC:
gc.collect()
after = gc.mem_free()
end_time = time.ticks_diff(time.ticks_ms(), start_time)
print("Scan with query returning first result (row 101) took",
end_time, "ms to run.")
print("Scan with query returning first result (row 101) took",
before - after, "bytes.")
except Exception:
return 'Error.'
if the_scan['name'] == 'blah':
return 'Success.'
else:
return 'Error.'
# A test to be sure data row files were created correctly.
def check_data_file_name():
location = mdb.os.listdir('testdb/testtable')
# Remove non-data files from our list of dirs.
location = [element for element in location if 'data' in element]
# Check we have the correct number of data page files
if len(location) == 61:
pass
else:
raise Exception('Error.')
# Sort as integers so we get them in the right order.
location = sorted(location,
key=lambda x: int(x.split('.')[0].split('_')[1]),
reverse=True)
for f in location:
if f in ['data551_560.dat', 'data1_10.dat', 'data11_20.dat',
'data21_30.dat', 'data31_40.dat', 'data41_50.dat',
'data51_60.dat', 'data61_70.dat', 'data71_80.dat',
'data81_90.dat', 'data91_100.dat', 'data101_110.dat',
'data111_120.dat', 'data121_130.dat', 'data131_140.dat',
'data141_150.dat', 'data151_160.dat', 'data161_170.dat',
'data171_180.dat', 'data181_190.dat', 'data191_200.dat',
'data201_210.dat', 'data211_220.dat', 'data221_230.dat',
'data231_240.dat', 'data241_250.dat', 'data251_260.dat',
'data261_270.dat', 'data271_280.dat', 'data281_290.dat',
'data291_300.dat', 'data301_310.dat', 'data311_320.dat',
'data321_330.dat', 'data331_340.dat', 'data341_350.dat',
'data351_360.dat', 'data361_370.dat', 'data371_380.dat',
'data381_390.dat', 'data391_400.dat', 'data401_410.dat',
'data411_420.dat', 'data421_430.dat', 'data431_440.dat',
'data441_450.dat', 'data451_460.dat', 'data461_470.dat',
'data471_480.dat', 'data481_490.dat', 'data491_500.dat',
'data501_510.dat', 'data511_520.dat', 'data521_530.dat',
'data531_540.dat', 'data541_550.dat', 'data561_570.dat',
'data571_580.dat', 'data581_590.dat', 'data591_600.dat',
'data601_610.dat']:
continue
else:
raise Exception('Error.')
return 'Success.'
def check_current_row():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
current_row = db_table.__calculate_current_row()
except Exception:
return 'Error.'
if int(current_row) == 610:
return 'Success.'
else:
return 'Error.'
# Make sure the data files have the correct number of rows in the files
def test_data_files():
location = mdb.os.listdir('testdb/testtable')
# Remove non-data files from our list of dirs.
location = [element for element in location if 'data' in element]
# Sort as integers so we get them in the right order.
location = sorted(location,
key=lambda x: int(x.split('.')[0].split('_')[1]),
reverse=True)
for f in location:
if f != 'data601_610.dat':
with open('testdb/testtable/' + f, 'r') as output_file:
i = 0
for line in output_file:
i += 1
if i == 10:
pass
else:
raise Exception('Error.')
else:
with open('testdb/testtable/' + f, 'r') as output_file:
i = 0
for line in output_file:
i += 1
if i == 10:
pass
else:
raise Exception('Error.')
return 'Success.'
def test_truncate():
try:
db_object = mdb.Database.open("testdb")
db_table = db_object.open_table("testtable")
db_table.truncate()
except Exception:
return 'Error.'
for file_name in mdb.os.listdir('testdb/testtable'):
if file_name[0:4] == 'data':
return 'Error.'
else:
return 'Success.'
# Clean up all the test data
def remove_test_database_files():
try:
for file_name in mdb.os.listdir('testdb/testtable'):
mdb.os.remove('testdb/testtable/' + file_name)
mdb.os.rmdir('testdb/testtable')
for file_name in mdb.os.listdir('testdb'):
mdb.os.remove('testdb/' + file_name)
mdb.os.rmdir('testdb')
except Exception:
return 'Failed to delete test data.'
print("Testing")
print("------")
assert test_database_open_exception() == "Success.", "Open database that doesn't exist: Error."
assert test_database_creation() == "Success.", "Create database: Error."
assert test_database_creation_exception() == "Success.", "Create database with same name: Error."
assert test_table_open_exception() == "Success.", "Open table that doesn't exist: Error."
assert test_table_creation() == "Success.", "Create table: Error."
assert test_table_open() == "Success.", "Open table: Error."
assert test_insert_row() == "Success.", "Insert row: Error."
assert test_insert_multiple_rows() == "Success.", "Insert multiple rows: Error."
assert test_update_row_exception_row() == "Success.", "Update row that doesn't exist: Error."
assert test_update_row_exception_column() == "Success.", "Update row with column that doesn't exist: Error."
assert test_update_row() == "Success.", "Update row: Error."
assert test_update() == "Success.", "Update: Error."
assert test_update_exception() == "Success.", "Update with query that doesn't match: Error."
assert test_delete_row() == "Success.", "Delete row: Error."
assert test_delete_row_exception() == "Success.", "Delete row exception: Error."
assert test_find_row() == "Success.", "Find row: Error."
assert test_find_row_exception() == "Success.", "Find row exception: Error."
assert test_query() == "Success.", "Query exception: Error."
assert test_find() == "Success.", "Find exception: Error."
assert test_scan_no_query() == "Success.", "Scan without query: Error."
assert test_scan_with_query() == "Success.", "Scan with query: Error."
assert check_data_file_name() == "Success.", "Data row files: Error"
assert test_truncate() == "Success.", "Truncate: Error."
remove_test_database_files()
print("------")
print("All tests passed.")