- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
Latest commit
95 lines (78 loc) · 3.59 KB
/
Copy pathtest.py
File metadata and controls
95 lines (78 loc) · 3.59 KB
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
importmultiindex
classTestIdTime(object):
def__init__(self, id, time):
self.time=time
self.id=id
defto_dict(obj, classkey=None):
ifisinstance(obj, dict):
data= {}
for (k, v) inobj.items():
data[k] =to_dict(v, classkey)
returndata
elifhasattr(obj, "_ast"):
returnto_dict(obj._ast())
elifhasattr(obj, "__iter__"):
return [to_dict(v, classkey) forvinobj]
elifhasattr(obj, "__dict__"):
data=dict([(key, to_dict(value, classkey))
forkey, valueinobj.__dict__.iteritems()
ifnotcallable(value) andnotkey.startswith('_')])
ifclasskeyisnotNoneandhasattr(obj, "__class__"):
data[classkey] =obj.__class__.__name__
returndata
else:
returnobj
deftest():
transactions=IndexedList()
transactions.add_unique_index('id')
transactions.add_index('time')
transactions.append(TestIdTime(0, 553456670000007)) # Removed. will be removed by pk
transactions.append(TestIdTime(1, 123456670000008)) # Stays. less than slice start
transactions.append(TestIdTime(2, 123456670000009)) # Stays. will be replaced
transactions.append(TestIdTime(7, 123456670000009)) # Removed. removed by slice
transactions.append(TestIdTime(3, 123456680000000)) # Removed. same as 7th
transactions.append(TestIdTime(4, 123456680000001)) # Stays. larger than slice end
transactions.append(TestIdTime(5, 123456680000002)) # Stays. same as 4th
sixth=TestIdTime(6, 123456680000003) # Removed. by remove_item
transactions.append(sixth)
transactions.append(TestIdTime(8, 123456680000003)) # Removed. by remove_unique
errors= []
transactions.replace('id', 2, TestIdTime(2, 123456680000009))
print('removed:')
print(to_dict(transactions.remove_slice('time', 123456670000009, 123456680000000, True)))
print(to_dict(transactions.pop(0)))
print(to_dict(transactions.pop_unique('id', 8)))
print(to_dict(transactions.pop_item(sixth)))
foriinrange(transactions.get_max_pk() +1):
try:
fail=transactions.get_by('id', i)
exceptKeyError:
ifiin [1, 2, 4, 5]:
errors.append("id({}) should not be removed".format(i))
else:
ifiin [0, 3, 6, 7, 8]:
errors.append("{} should be removed".format(to_dict(fail)))
iflen(errors) >0:
print('\n'.join(errors))
returnlen(errors)
transactions.append(TestIdTime(10, 553456670000007)) # Removed. will be removed by pk
transactions.append(TestIdTime(11, 123456670000008)) # Stays. less than slice start
transactions.append(TestIdTime(12, 123456670000009)) # Stays. will be replaced
transactions.append(TestIdTime(17, 123456670000009)) # Removed. removed by slice
transactions.append(TestIdTime(13, 123456680000000)) # Removed. same as 7th
transactions.append(TestIdTime(14, 123456680000001)) # Stays. larger than slice end
transactions.append(TestIdTime(15, 123456680000002)) # Stays. same as 4th
assert11==len(transactions)
transactions.pop_front('time')
assert10==len(transactions)
transactions.pop_front('time')
assert9==len(transactions)
outnumbers=map(to_dict, transactions.pop_outnumbers('time', 3))
assert6==len(outnumbers)
print(outnumbers)
assert3==len(transactions)
outnumbers=map(to_dict, transactions.pop_outnumbers('time', 4))
assert0==len(outnumbers)
assert3==len(transactions)
if__name__=='__main__':
test()