-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathCacheFile_test.lua
98 lines (84 loc) · 2.75 KB
/
CacheFile_test.lua
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
-- CacheFile_test.lua
-- unit test
require 'CacheFile'
require 'makeVp'
local verbose = 0
local vp = makeVp(verbose, 'tester')
local filePath = '/tmp/CacheFile_test.csv'
local cf = CacheFile{keyNames={'key1', 'key2', 'key3'},
valueNames={'string1', 'number2'},
filePath=filePath}
local found = cf:fetch{keys={123, 'abc', .789}}
vp(1, 'found', found)
assert(not found)
local already = cf:store{keys={123, 'abc', .789},values={'x',27}}
assert(not already)
local already = cf:store{keys={123, 'abc', .789},values={'x',27}}
assert(already)
local ok, msg = pcall(function(x) cf:store(x) end,
{keys={123, 'abc', .789},values={'new', 'value'}})
vp(1, 'ok', ok, 'msg', msg)
assert(not ok)
vp(2, 'match',string.match(msg, 'keys in table with different value'))
assert(string.match(msg, 'keys in table with different value'))
local values = cf:fetch{keys={123, 'abc', .789}}
assert(values)
assert(values[1] == 'x')
assert(values[2] == 27)
assert(#values == 2)
-- test large number of entries
local c = CacheFile{keyNames={'k1', 'k2', 'k3'},
valueNames={'x'},
filePath=filePath}
local key1s = {1, 2, 3}
local key2s = {10, 11, 12}
local key3s = {100, 101, 102}
for _, key1 in ipairs(key1s) do
for _, key2 in ipairs(key2s) do
for _, key3 in ipairs(key3s) do
local value = key1 + key2 + key3
c:store{keys={key1, key2, key3}, values={value}}
end
end
end
-- test retrieval
for _, key1 in ipairs(key1s) do
for _, key2 in ipairs(key2s) do
for _, key3 in ipairs(key3s) do
local expected = key1 + key2 + key3
local actual = c:fetch{keys={key1, key2, key3}}
assert(#actual == 1)
vp(1, 'expected', expected, 'actual', actual)
assert(actual[1] == expected)
end
end
end
-- test writing and reading
c : write()
-- create new cache file with one entry, not in old c
local c = CacheFile{keyNames={'k1', 'k2', 'k3'},
valueNames={'x'},
filePath=filePath}
c:store{keys={0,0,0},values={0}}
c:merge() -- add in all the key-value pairs written from old c
-- make sure the old values are now there
for _, key1 in ipairs(key1s) do
for _, key2 in ipairs(key2s) do
for _, key3 in ipairs(key3s) do
local expected = key1 + key2 + key3
local actual = c:fetch{keys={key1, key2, key3}}
assert(#actual == 1)
vp(1, 'expected', expected, 'actual', actual)
assert(actual[1] == expected)
end
end
end
-- make sure the new value is also there
local expected = 0
local actual = c:fetch{keys={0,0,0}}
vp(1, 'actual', actual)
vp(1, 'c.table', c.table)
assert(actual ~= nil)
assert(#actual == 1)
assert(actual[1] == expected)
print('ok CacheFile')