aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_datamanager_spec.lua
blob: d46590e8637e0d5b8a8b094a9df0db8504050a98 (plain) (blame)
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
describe("util.datamanager", function()
	local dm;
	setup(function()
		dm = require "util.datamanager";
		dm.set_data_path("./data");
	end);

	describe("keyvalue", function()
		local data = {hello = "world"};

		do
			local ok, err = dm.store("keyval-user", "datamanager.test", "testdata", data);
			assert.truthy(ok, err);
		end

		do
			local read, err = dm.load("keyval-user", "datamanager.test", "testdata")
			assert.same(data, read, err);
		end

		do
			local ok, err = dm.store("keyval-user", "datamanager.test", "testdata", nil);
			assert.truthy(ok, err);
		end

		do
			local read, err = dm.load("keyval-user", "datamanager.test", "testdata")
			assert.is_nil(read, err);
		end
	end)

	describe("lists", function()
		do
			local ok, err = dm.list_store("list-user", "datamanager.test", "testdata", {});
			assert.truthy(ok, err);
		end

		do
			local nothing, err = dm.list_load("list-user", "datamanager.test", "testdata");
			assert.is_nil(nothing, err);
			assert.is_nil(err);
		end

		do
			local ok, err = dm.list_append("list-user", "datamanager.test", "testdata", {id = 1});
			assert.truthy(ok, err);
		end

		do
			local ok, err = dm.list_append("list-user", "datamanager.test", "testdata", {id = 2});
			assert.truthy(ok, err);
		end

		do
			local ok, err = dm.list_append("list-user", "datamanager.test", "testdata", {id = 3});
			assert.truthy(ok, err);
		end

		do
			local list, err = dm.list_load("list-user", "datamanager.test", "testdata");
			assert.same(list, {{id = 1}; {id = 2}; {id = 3}}, err);
		end

		do
			local ok, err = dm.list_store("list-user", "datamanager.test", "testdata", {});
			assert.truthy(ok, err);
		end

		do
			local nothing, err = dm.list_load("list-user", "datamanager.test", "testdata");
			assert.is_nil(nothing, err);
			assert.is_nil(err);
		end

	end)
end)