Comparison

spec/core_storagemanager_spec.lua @ 10927:470602a8b633

storage tests: Add tests for archive queries before/after specific ids Also increased the size of the test data for easier debugging with more complex tests.
author Matthew Wild <mwild1@gmail.com>
date Fri, 12 Jun 2020 16:59:06 +0100
parent 10842:5a6ba2f38e2b
child 11273:9f1355689648
comparison
equal deleted inserted replaced
10926:c55bd98a54f8 10927:470602a8b633
213 local test_data = { 213 local test_data = {
214 { nil, test_stanza, test_time, "contact@example.com" }; 214 { nil, test_stanza, test_time, "contact@example.com" };
215 { nil, test_stanza, test_time+1, "contact2@example.com" }; 215 { nil, test_stanza, test_time+1, "contact2@example.com" };
216 { nil, test_stanza, test_time+2, "contact2@example.com" }; 216 { nil, test_stanza, test_time+2, "contact2@example.com" };
217 { nil, test_stanza, test_time-1, "contact2@example.com" }; 217 { nil, test_stanza, test_time-1, "contact2@example.com" };
218 { nil, test_stanza, test_time-1, "contact3@example.com" };
219 { nil, test_stanza, test_time+0, "contact3@example.com" };
220 { nil, test_stanza, test_time+1, "contact3@example.com" };
218 }; 221 };
219 222
220 it("can be added to", function () 223 it("can be added to", function ()
221 for _, data_item in ipairs(test_data) do 224 for _, data_item in ipairs(test_data) do
222 local ok = archive:append("user", unpack(data_item, 1, 4)); 225 local id = archive:append("user", unpack(data_item, 1, 4));
223 assert.truthy(ok); 226 assert.truthy(id);
227 data_item[1] = id;
224 end 228 end
225 end); 229 end);
226 230
227 describe("can be queried", function () 231 describe("can be queried", function ()
228 it("for all items", function () 232 it("for all items", function ()
275 assert.equal("test", item.name); 279 assert.equal("test", item.name);
276 assert.equal("urn:example:foo", item.attr.xmlns); 280 assert.equal("urn:example:foo", item.attr.xmlns);
277 assert.equal(2, #item.tags); 281 assert.equal(2, #item.tags);
278 assert(test_time >= when); 282 assert(test_time >= when);
279 end 283 end
280 assert.equal(2, count); 284 assert.equal(4, count);
281 end); 285 end);
282 286
283 it("by time (start)", function () 287 it("by time (start)", function ()
284 -- luacheck: ignore 211/err 288 -- luacheck: ignore 211/err
285 local data, err = archive:find("user", { 289 local data, err = archive:find("user", {
294 assert.equal("test", item.name); 298 assert.equal("test", item.name);
295 assert.equal("urn:example:foo", item.attr.xmlns); 299 assert.equal("urn:example:foo", item.attr.xmlns);
296 assert.equal(2, #item.tags); 300 assert.equal(2, #item.tags);
297 assert(test_time <= when); 301 assert(test_time <= when);
298 end 302 end
299 assert.equal(#test_data -1, count); 303 assert.equal(#test_data - 2, count);
300 end); 304 end);
301 305
302 it("by time (start+end)", function () 306 it("by time (start+end)", function ()
303 -- luacheck: ignore 211/err 307 -- luacheck: ignore 211/err
304 local data, err = archive:find("user", { 308 local data, err = archive:find("user", {
315 assert.equal("urn:example:foo", item.attr.xmlns); 319 assert.equal("urn:example:foo", item.attr.xmlns);
316 assert.equal(2, #item.tags); 320 assert.equal(2, #item.tags);
317 assert(when >= test_time, ("%d >= %d"):format(when, test_time)); 321 assert(when >= test_time, ("%d >= %d"):format(when, test_time));
318 assert(when <= test_time+1, ("%d <= %d"):format(when, test_time+1)); 322 assert(when <= test_time+1, ("%d <= %d"):format(when, test_time+1));
319 end 323 end
320 assert.equal(2, count); 324 assert.equal(4, count);
325 end);
326
327 it("by id (after)", function ()
328 -- luacheck: ignore 211/err
329 local data, err = archive:find("user", {
330 ["after"] = test_data[2][1];
331 });
332 assert.truthy(data);
333 local count = 0;
334 for id, item in data do
335 count = count + 1;
336 assert.truthy(id);
337 assert.equal(test_data[2+count][1], id);
338 assert(st.is_stanza(item));
339 assert.equal("test", item.name);
340 assert.equal("urn:example:foo", item.attr.xmlns);
341 assert.equal(2, #item.tags);
342 end
343 assert.equal(5, count);
344 end);
345
346 it("by id (before)", function ()
347 -- luacheck: ignore 211/err
348 local data, err = archive:find("user", {
349 ["before"] = test_data[4][1];
350 });
351 assert.truthy(data);
352 local count = 0;
353 for id, item in data do
354 count = count + 1;
355 assert.truthy(id);
356 assert.equal(test_data[count][1], id);
357 assert(st.is_stanza(item));
358 assert.equal("test", item.name);
359 assert.equal("urn:example:foo", item.attr.xmlns);
360 assert.equal(2, #item.tags);
361 end
362 assert.equal(3, count);
321 end); 363 end);
322 end); 364 end);
323 365
324 it("can selectively delete items", function () 366 it("can selectively delete items", function ()
325 local delete_id; 367 local delete_id;