forked from asb/lua-tinycdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
48 lines (41 loc) · 1.05 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
require("lunit")
local cdb = require("cdb")
local db_name = "test.cdb"
local db = assert(cdb.make(db_name, db_name..".tmp"))
db:add("one", "1")
db:add("two", "2")
db:add("three", "4") -- oops
db:add("three", "3", "replace")
db:add("three", "III")
assert(db:finish())
module("querying a cdb", lunit.testcase, package.seeall)
do
function setup()
db = assert(cdb.open(db_name))
end
function test_get()
assert_equal("1", db:get("one"))
assert_equal("3", db:get("three"))
assert_nil(db:get("four"))
end
function test_pairs()
local expected_keys = { "one", "two", "three", "three" }
local expected_values = { "1", "2", "3", "III" }
local i = 1
for k, v in db:pairs() do
assert_equal(expected_keys[i], k)
assert_equal(expected_values[i], v)
i = i+1
end
end
function test_findall()
local t = db:find_all("three")
assert_equal(2, #t)
assert_equal("3", t[1])
assert_equal("III", t[2])
end
function test_closed_cdb()
db:close()
assert_error(nil, function() db:get("one") end)
end
end