-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
95 lines (73 loc) · 2.03 KB
/
tests.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
import dbdict
import sqlite3
#
# Perform some tests using the dbdict module
#
d = dbdict.dbdict(':memory:')
d[1] = 'test'
assert d[1] == 'test'
d[1] += '1'
assert d[1] == 'test1'
try:
assert d[2], 'Lookup did not fail on non-existent key'
except KeyError:
pass
# test len
assert len(d) == 1, 'Failed to count number of items'
# test clear
d.clear()
assert len(d) == 0, 'Database not cleared as expected'
# test with list of items as (key, value) pairs
range10 = list(range(10))
items = [(i, i) for i in range10]
d.update(items)
assert list(d.items()) == items, 'Failed to update using list'
d.clear()
# test with tuple of items as (key, value) pairs
d.update(tuple(items))
assert list(d.items()) == items, 'Failed to update using tuple'
d.clear()
# test with dict
d.update(dict(items))
assert list(d.items()) == items
d.clear()
# test with generator
d.update((i, i) for i in range10)
assert list(d.items()) == items, 'Failed to update using generator'
# check the std. dict methods
assert list(d.keys()) == range10
assert list(d.values()) == range10
assert list(d.items()) == items
# test get
assert d.get(range(8, 12)) == items[-2:]
# test remove
d.remove(range(8, 10))
assert len(d.get(range(8, 10))) == 0, 'Items not removed successfully'
d.clear()
# test with key,value pairs as parameters
d.update(foo=1, bar=2)
assert list(d.items()) == [('foo', 1), ('bar', 2)], \
'keyword assignment not successful'
# test popitem
while True:
try:
value = d.popitem()
assert value in [('foo', 1), ('bar', 2)], \
'Popitem not in expected result set'
except StopIteration:
break
# test setdefault
d.setdefault(10, 10)
assert d[10] == 10, 'Failed to set default value'
# test vacuum call (no assert)
d.reindex()
# test vacuum call (no assert, and call has no effect on an in memory db)
d.vacuum()
# test close call (assert is given reading from closed database)
d.close()
# try reading from a closed database
try:
d[1] = 1
raise AssertionError('Database not closed')
except sqlite3.ProgrammingError:
pass