-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.lua
299 lines (265 loc) · 7.22 KB
/
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
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
require "redis"
require "lohm"
require "debug"
function debug.dump(tbl)
local function tcopy(t) local nt={}; for i,v in pairs(t) do nt[i]=v end; return nt end
local function printy(thing, prefix, tablestack)
local t = type(thing)
if t == "nil" then return "nil"
elseif t == "string" then return string.format('%q', thing)
elseif t == "number" then return tostring(thing)
elseif t == "function" then
local info = debug.getinfo(thing)
local source = info.source
if info.linedefined then
source = source .. ":" .. info.linedefined .. " - " .. info.lastlinedefined
end
if info.name then
return ("%s %s: %s"):format(info.namewhat, info.name, source)
else
return "function " .. source
end
elseif t == "table" then
if tablestack and tablestack[thing] then return string.format("%s (recursion)", tostring(thing)) end
local kids, pre, substack = {}, " " .. prefix, (tablestack and tcopy(tablestack) or {})
substack[thing]=true
for k, v in pairs(thing) do
table.insert(kids, string.format('%s%s= %s,',pre,printy(k, ''),printy(v, pre, substack)))
end
return string.format("%s{\n%s\n%s}", tostring(thing), table.concat(kids, "\n"), prefix)
else
return tostring(thing)
end
end
local ret = printy(tbl, "", {})
return ret
end
function debug.print(...)
local buffer = {}
for i, v in pairs{...} do
table.insert(buffer, debug.dump(v))
end
local res = table.concat(buffer, " ")
print(res)
return ...
end
local function newr()
local redis = Redis.connect()
redis:select(14)
redis:flushdb()
return redis
end
if not telescope then
function assert_type(var, typ)
return assert(type(var)==typ, "wrong type")
end
function assert_true(...)
for i, v in pairs{...} do
assert(v and v)
end
end
function context(lbl, tests)
return tests()
end
function test(lbl, test)
return test()
end
function assert_equal(a, ...)
for i, b in pairs{...} do
assert(a==b)
end
return true
end
function assert_false(...)
for i,v in pairs{...} do
assert(not v)
end
end
function assert_nil(...)
for i,v in pairs{...} do
assert(type(v)=='nil')
end
end
end
context("Initialization", function()
test("Can use open redis connection", function()
local redis = newr()
assert(redis:ping())
local res = assert(lohm.new({key="foo:%s"}, redis))
assert_type(res,'table')
end)
test("Model / Data prototype init", function()
local M = lohm.new({
key="foobar:%s",
object={
getFoo = function(self)
return self.foo
end
},
model={
getFirst = function(self)
return self:findOne(1)
end
}
}, newr())
assert_equal(M:new({foo=11}):getFoo(), 11)
--assert_true(lohm.isModel(M))
M:getFirst()
end)
end)
context("Basic manipulation", function()
test("insertion / autoincrement id counter / lookup by id / deletion", function()
local redis = newr()
local Model = lohm.new({key="foo:%s"}, redis)
local m = Model:new{foo='barbar'}
m:save()
local k, id = m:getKey(), m:getId()
m:set('barbar','baz'):save()
assert(m:getId()==id)
local checkM = assert(Model:findOne(id))
assert_true( rawget(checkM,'barbar')=="baz" )
assert_true( "barbar"==checkM.foo )
assert(checkM:delete())
local notfound = Model[tonumber(id)]
assert_true(not notfound)
end)
end)
context("Sets", function()
test("Rudimentary set manipulation", function()
local r = newr()
local Set = lohm.set({key="setty:%s"}, r)
s=Set:new():add('foo', "bar","baz")
s:save()
local setId = s:getId()
local sprime = Set[setId]
assert_true(r:sismember(sprime:getKey(), 'foo'))
assert_false(r:sismember(sprime:getKey(), 'bax'))
s:remove("foo", "bax")
s:save()
assert_equal(#s, 2)
assert_equal(#Set[setId], 2)
end)
test("Sets as references", function()
local r = newr()
local Foo = lohm.new({key="foohash:%s"}, r)
local Set = lohm.new({key="refset:%s", type='set', reference = Foo}, r)
local s = Set:new()
table.insert(s, (Foo:new({bar='baz'})))
s:save()
local fooId = s[1]:getId()
assert_true(Foo[fooId].bar=='baz')
end)
end)
context("Indexing", function()
test("Storage and Retrieval with index", function()
local M = lohm.new({
key="testindexing:%s",
index = {"i1","i2", "i3"}
}, newr())
local findme=math.random(1,300)
local find_id, findey
for i=1, 300 do
local m=assert(M:new{i1=math.random(1000), i2=math.random(1000), i3=math.random(1,2)}:save())
if i==findme then
findey, find_id=m, m:getId()
end
end
assert_equal(#assert(M:findByAttr{i3=1})+#assert(M:findByAttr{i3=2}), 300)
local res = M:findByAttr(findey)
assert_equal(res[1].i3, tostring(findey.i3))
assert_equal(#res, 1)
assert_equal(res[1].i3, tostring(findey.i3))
for i, v in pairs(res[1]) do
assert_equal(tostring(v), tostring(findey[i]))
end
local experiment_one = res[1]
experiment_one.i3="baconated grapefruit"
experiment_one:save()
experiment_one.i3=nil
experiment_one:save()
end)
end)
context("References", function()
test("direct reference manipulation", function()
local r = newr()
local Moo = lohm.hash({key="moo:%s"}, r)
local Thing = lohm.hash({
key="thing:%s",
attributes = {
moo = Moo
}
}, r)
local t = Thing:new{ foo="bar" }
local m = Moo:new({ bar="baz" }):save()
t.moo = m
t:save()
local t1 = Thing:findOne(t:getId())
assert_equal(t1.moo:getId(), '1')
t1.moo.bar="17"
t1:save()
local t1prime = Thing:findOne(t1:getId())
assert_equal(t1prime.moo.bar, t1.moo.bar)
t1:delete()
assert_nil(Thing[tonumber(t:getId())])
local t2 = Thing:new({z="9", moo=Moo:new({aux='bax'})}):save()
end)
--[[ test("Deletion", function()
local r = newr()
local Foo = lohm.new({key="foo:%s"}, r)
local Bar = lohm.new({key="bar:%s",
attributes={
foo = Foo
}
}, r)
local HardBar = lohm.new({key="hardBar:%s",
attributes = {
foo = Foo
}
}, r)
local foo1 = Foo:new({attr="foo1"}):save()
local foo2 = Foo:new({attr="foo2"}):save()
local bar = Bar:new({foo=foo1, bar=11}):save()
foo2.test="test"
local hardBar = HardBar:new({foo=foo2, bar=9}):save()
local ids = {}
for i, v in pairs{foo1=foo1, foo2=foo2, hardBar=hardBar, bar = bar} do
ids[i]=v:getId()
end
assert_equal(Bar:findOne(ids.bar):getId(), bar:getId())
bar:delete()
assert_true(not Bar:findOne(ids.bar))
assert(Foo:findOne(ids.foo1))
assert_equal(HardBar:findOne(ids.hardBar).foo.attr, foo2.attr)
hardBar:delete()
assert_true(not HardBar:findOne(ids.hardBar))
assert_true(not Foo:findOne(ids.foo2))
assert(Foo:findOne(ids.foo1):getId())
end)
]]
test("One-to-many references (sets)", function()
local r = newr()
local Bar = lohm({key="barbar:%s"}, r)
local Foo = lohm({
key="foo:%s",
attributes= {
manyBar = lohm.new({key="bars:%s", reference = Bar, type='set'}, r),
oneBar = Bar
}
}, r)
local bars = {}
for i=1, 20 do
local bar = Bar:new{woo=i}:save()
table.insert(bars, bar)
end
local foo = Foo:new{oneBar = Bar:new({yes="no"}):save()}
for i,v in pairs(bars) do
table.insert(foo.manyBar, v)
end
assert_equal(foo:save(), foo)
local foo_take2 = Foo:findOne(foo:getId())
assert_equal(foo_take2:getId(), foo:getId())
for i,v in pairs(foo_take2.manyBar) do
assert_equal(tostring(v.woo), tostring(bars[tonumber(v.woo)].woo))
end
end)
end)