Newer
Older
from __future__ import division, with_statement, absolute_import
import pytest # noqa
import sys # noqa
from six.moves import range
from six.moves import zip
def test_persistent_dict():
from pytools.persistent_dict import PersistentDict
pdict = PersistentDict("pytools-test")
pdict.clear()
from random import randrange
def rand_str(n=20):
return "".join(
chr(65+randrange(26))
for i in range(n))
keys = [(randrange(2000), rand_str(), None) for i in range(20)]
values = [randrange(2000) for i in range(20)]
for k, v in zip(keys, values):
pdict[k] = v
pdict.store(k, v, info_files={"hey": str(v)})
assert d[k] == pdict[k]
for k, v in zip(keys, values):
pdict.store(k, v+1, info_files={"hey": str(v)})
assert d[k] + 1 == pdict[k]
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
class PDictTestValue(object):
def __init__(self, val):
self.val = val
def __getstate__(self):
return {"val": self.val}
def update_persistent_hash(self, key_hash, key_builder):
key_builder.rec(key_hash, self.val)
def test_persistent_dict_in_memory_cache():
from pytools.persistent_dict import PersistentDict
pdict = PersistentDict("pytools-in-memory-cache-test", in_mem_cache_size=3)
pdict.clear()
pdict[1] = PDictTestValue(1)
pdict[2] = PDictTestValue(2)
pdict[3] = PDictTestValue(3)
pdict[4] = PDictTestValue(4)
# {{{ test LRU policy
val1 = pdict[1]
val1.val = 0
assert pdict[1].val == 0
pdict[2]
assert pdict[1].val == 0
pdict[3]
assert pdict[1].val == 0
pdict[2]
assert pdict[1].val == 0
pdict[4]
assert pdict[1].val == 1
# }}}
# {{{ test cache invalidation by versioning
assert pdict[1].val == 1
pdict2 = PersistentDict("pytools-in-memory-cache-test")
pdict2[1] = PDictTestValue(5)
assert pdict[1].val == 5
# }}}
# {{{ test cache invalidation by deletion
del pdict2[1]
pdict2[1] = PDictTestValue(10)
assert pdict[1].val == 10
# }}}
if __name__ == "__main__":
if len(sys.argv) > 1:
else:
from py.test.cmdline import main
main([__file__])