Comparison

spec/core_storagemanager_spec.lua @ 10677:0054aec3e8c5

mod_storage_sql: Add map_store:find_key() and map_store:delete_key() (+ tests)
author Matthew Wild <mwild1@gmail.com>
date Wed, 11 Mar 2020 15:57:53 +0000
parent 10676:33c7e4920591
child 10678:c9251d74a112
comparison
equal deleted inserted replaced
10676:33c7e4920591 10677:0054aec3e8c5
60 end 60 end
61 assert(hm.activate(test_host, {})); 61 assert(hm.activate(test_host, {}));
62 sm.initialize_host(test_host); 62 sm.initialize_host(test_host);
63 assert(mm.load(test_host, "storage_"..backend_config.storage)); 63 assert(mm.load(test_host, "storage_"..backend_config.storage));
64 64
65 local sql_it = backend_config.sql and it or pending;
66
65 describe("key-value stores", function () 67 describe("key-value stores", function ()
66 -- These tests rely on being executed in order, disable any order 68 -- These tests rely on being executed in order, disable any order
67 -- randomization for this block 69 -- randomization for this block
68 randomize(false); 70 randomize(false);
69 71
111 113
112 it("may get a specific key for a user", function () 114 it("may get a specific key for a user", function ()
113 assert.equal("bar", store:get("user9999", "foo")); 115 assert.equal("bar", store:get("user9999", "foo"));
114 end); 116 end);
115 117
116 it("may remove data for a user", function () 118 sql_it("may find all users with a specific key", function ()
119 assert.is_function(store.find_key);
120 assert(store:set("user9999b", "bar", "bar"));
121 assert(store:set("user9999c", "foo", "blah"));
122 local ret, err = store:find_key("foo");
123 assert.is_nil(err);
124 assert.same({ user9999 = "bar", user9999c = "blah" }, ret);
125 end);
126
127 sql_it("rejects empty or non-string keys to find_key", function ()
128 assert.is_function(store.find_key);
129 do
130 local ret, err = store:find_key("");
131 assert.is_nil(ret);
132 assert.is_not_nil(err);
133 end
134 do
135 local ret, err = store:find_key(true);
136 assert.is_nil(ret);
137 assert.is_not_nil(err);
138 end
139 end);
140
141 sql_it("rejects empty or non-string keys to delete_key", function ()
142 assert.is_function(store.delete_key);
143 do
144 local ret, err = store:delete_key("");
145 assert.is_nil(ret);
146 assert.is_not_nil(err);
147 end
148 do
149 local ret, err = store:delete_key(true);
150 assert.is_nil(ret);
151 assert.is_not_nil(err);
152 end
153 end);
154
155 sql_it("may delete all instances of a specific key", function ()
156 assert.is_function(store.delete_key);
157 assert(store:set("user9999b", "foo", "hello"));
158
159 local ret, err = store:delete_key("bar");
160 -- Ensure key was deleted
161 do
162 local ret, err = store:get("user9999b", "bar");
163 assert.is_nil(ret);
164 assert.is_nil(err);
165 end
166 -- Ensure other users/keys are intact
167 do
168 local ret, err = store:get("user9999", "foo");
169 assert.equal("bar", ret);
170 assert.is_nil(err);
171 end
172 do
173 local ret, err = store:get("user9999b", "foo");
174 assert.equal("hello", ret);
175 assert.is_nil(err);
176 end
177 do
178 local ret, err = store:get("user9999c", "foo");
179 assert.equal("blah", ret);
180 assert.is_nil(err);
181 end
182 end);
183
184 it("may remove data for a specific key for a user", function ()
117 assert(store:set("user9999", "foo", nil)); 185 assert(store:set("user9999", "foo", nil));
118 do 186 do
119 local ret, err = store:get("user9999", "foo"); 187 local ret, err = store:get("user9999", "foo");
120 assert.is_nil(ret); 188 assert.is_nil(ret);
121 assert.is_nil(err); 189 assert.is_nil(err);
122 end 190 end
123 do 191
124 local ret, err = kv_store:get("user9999"); 192 assert(store:set("user9999b", "foo", nil));
193 do
194 local ret, err = store:get("user9999b", "foo");
125 assert.is_nil(ret); 195 assert.is_nil(ret);
126 assert.is_nil(err); 196 assert.is_nil(err);
127 end 197 end
128 end); 198 end);
129 end); 199 end);