From 2b289f34f929a69424a22bb0de3b668a58ba80cd Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 8 Dec 2018 17:09:55 +0100 Subject: various: Don't rely on _G.unpack existing --- spec/core_storagemanager_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index a0a8b5ef..fd2f8742 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -1,4 +1,4 @@ -local unpack = table.unpack or unpack; +local unpack = table.unpack or unpack; -- luacheck: ignore 113 local server = require "net.server_select"; package.loaded["net.server"] = server; -- cgit v1.2.3 From e0bcb4d7d43745ce677edabc1cebe6a53644723d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Dec 2019 21:33:10 +0100 Subject: tests: Silence [luacheck] warnings --- spec/core_storagemanager_spec.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index fd2f8742..c3534cd5 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -119,6 +119,7 @@ describe("storagemanager", function () describe("can be queried", function () it("for all items", function () + -- luacheck: ignore 211/err local data, err = archive:find("user", {}); assert.truthy(data); local count = 0; @@ -135,6 +136,7 @@ describe("storagemanager", function () end); it("by JID", function () + -- luacheck: ignore 211/err local data, err = archive:find("user", { with = "contact@example.com"; }); @@ -153,6 +155,7 @@ describe("storagemanager", function () end); it("by time (end)", function () + -- luacheck: ignore 211/err local data, err = archive:find("user", { ["end"] = test_time; }); @@ -171,6 +174,7 @@ describe("storagemanager", function () end); it("by time (start)", function () + -- luacheck: ignore 211/err local data, err = archive:find("user", { ["start"] = test_time; }); @@ -189,6 +193,7 @@ describe("storagemanager", function () end); it("by time (start+end)", function () + -- luacheck: ignore 211/err local data, err = archive:find("user", { ["start"] = test_time; ["end"] = test_time+1; @@ -239,6 +244,7 @@ describe("storagemanager", function () end); it("can be purged", function () + -- luacheck: ignore 211/err local ok, err = archive:delete("user"); assert.truthy(ok); local data, err = archive:find("user", { -- cgit v1.2.3 From 1d5af5f0a385ffbe4c46569d8fd8696c8c989a60 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 11 Mar 2020 14:36:56 +0000 Subject: storagemanager: Add tests for map stores --- spec/core_storagemanager_spec.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index c3534cd5..9ecb0cb4 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -90,6 +90,44 @@ describe("storagemanager", function () end); end); + describe("map stores", function () + -- These tests rely on being executed in order, disable any order + -- randomization for this block + randomize(false); + + local store, kv_store; + it("may be opened", function () + store = assert(sm.open(test_host, "test-map", "map")); + end); + + it("may be opened as a keyval store", function () + kv_store = assert(sm.open(test_host, "test-map", "keyval")); + end); + + it("may set a specific key for a user", function () + assert(store:set("user9999", "foo", "bar")); + assert.same(kv_store:get("user9999"), { foo = "bar" }); + end); + + it("may get a specific key for a user", function () + assert.equal("bar", store:get("user9999", "foo")); + end); + + it("may remove data for a user", function () + assert(store:set("user9999", "foo", nil)); + do + local ret, err = store:get("user9999", "foo"); + assert.is_nil(ret); + assert.is_nil(err); + end + do + local ret, err = kv_store:get("user9999"); + assert.is_nil(ret); + assert.is_nil(err); + end + end); + end); + describe("archive stores", function () randomize(false); -- cgit v1.2.3 From 33b9b2b91ec31f328dcd080832c3caf7a24cb93a Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 11 Mar 2020 15:57:53 +0000 Subject: mod_storage_sql: Add map_store:find_key() and map_store:delete_key() (+ tests) --- spec/core_storagemanager_spec.lua | 74 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index 9ecb0cb4..69afd87a 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -62,6 +62,8 @@ describe("storagemanager", function () sm.initialize_host(test_host); assert(mm.load(test_host, "storage_"..backend_config.storage)); + local sql_it = backend_config.sql and it or pending; + describe("key-value stores", function () -- These tests rely on being executed in order, disable any order -- randomization for this block @@ -113,15 +115,83 @@ describe("storagemanager", function () assert.equal("bar", store:get("user9999", "foo")); end); - it("may remove data for a user", function () + sql_it("may find all users with a specific key", function () + assert.is_function(store.find_key); + assert(store:set("user9999b", "bar", "bar")); + assert(store:set("user9999c", "foo", "blah")); + local ret, err = store:find_key("foo"); + assert.is_nil(err); + assert.same({ user9999 = "bar", user9999c = "blah" }, ret); + end); + + sql_it("rejects empty or non-string keys to find_key", function () + assert.is_function(store.find_key); + do + local ret, err = store:find_key(""); + assert.is_nil(ret); + assert.is_not_nil(err); + end + do + local ret, err = store:find_key(true); + assert.is_nil(ret); + assert.is_not_nil(err); + end + end); + + sql_it("rejects empty or non-string keys to delete_key", function () + assert.is_function(store.delete_key); + do + local ret, err = store:delete_key(""); + assert.is_nil(ret); + assert.is_not_nil(err); + end + do + local ret, err = store:delete_key(true); + assert.is_nil(ret); + assert.is_not_nil(err); + end + end); + + sql_it("may delete all instances of a specific key", function () + assert.is_function(store.delete_key); + assert(store:set("user9999b", "foo", "hello")); + + local ret, err = store:delete_key("bar"); + -- Ensure key was deleted + do + local ret, err = store:get("user9999b", "bar"); + assert.is_nil(ret); + assert.is_nil(err); + end + -- Ensure other users/keys are intact + do + local ret, err = store:get("user9999", "foo"); + assert.equal("bar", ret); + assert.is_nil(err); + end + do + local ret, err = store:get("user9999b", "foo"); + assert.equal("hello", ret); + assert.is_nil(err); + end + do + local ret, err = store:get("user9999c", "foo"); + assert.equal("blah", ret); + assert.is_nil(err); + end + end); + + it("may remove data for a specific key for a user", function () assert(store:set("user9999", "foo", nil)); do local ret, err = store:get("user9999", "foo"); assert.is_nil(ret); assert.is_nil(err); end + + assert(store:set("user9999b", "foo", nil)); do - local ret, err = kv_store:get("user9999"); + local ret, err = store:get("user9999b", "foo"); assert.is_nil(ret); assert.is_nil(err); end -- cgit v1.2.3 From e35a4db4984013d82c90bd99fa1cbb15396fa415 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 11 Mar 2020 16:07:36 +0000 Subject: storagemanager: Fix unused variable in tests [luacheck] --- spec/core_storagemanager_spec.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index 69afd87a..dd80f4b8 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -64,6 +64,7 @@ describe("storagemanager", function () local sql_it = backend_config.sql and it or pending; + describe("key-value stores", function () -- These tests rely on being executed in order, disable any order -- randomization for this block @@ -156,7 +157,7 @@ describe("storagemanager", function () assert.is_function(store.delete_key); assert(store:set("user9999b", "foo", "hello")); - local ret, err = store:delete_key("bar"); + assert(store:delete_key("bar")); -- Ensure key was deleted do local ret, err = store:get("user9999b", "bar"); -- cgit v1.2.3 From 81f5c3e319792bec5d53c30334f3f54537aeb32b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 11 Mar 2020 16:29:57 +0000 Subject: storagemanager: Add support for :find_key() and :delete_key() to map store shim --- spec/core_storagemanager_spec.lua | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index dd80f4b8..848f7910 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -62,9 +62,6 @@ describe("storagemanager", function () sm.initialize_host(test_host); assert(mm.load(test_host, "storage_"..backend_config.storage)); - local sql_it = backend_config.sql and it or pending; - - describe("key-value stores", function () -- These tests rely on being executed in order, disable any order -- randomization for this block @@ -116,7 +113,7 @@ describe("storagemanager", function () assert.equal("bar", store:get("user9999", "foo")); end); - sql_it("may find all users with a specific key", function () + it("may find all users with a specific key", function () assert.is_function(store.find_key); assert(store:set("user9999b", "bar", "bar")); assert(store:set("user9999c", "foo", "blah")); @@ -125,7 +122,7 @@ describe("storagemanager", function () assert.same({ user9999 = "bar", user9999c = "blah" }, ret); end); - sql_it("rejects empty or non-string keys to find_key", function () + it("rejects empty or non-string keys to find_key", function () assert.is_function(store.find_key); do local ret, err = store:find_key(""); @@ -139,7 +136,7 @@ describe("storagemanager", function () end end); - sql_it("rejects empty or non-string keys to delete_key", function () + it("rejects empty or non-string keys to delete_key", function () assert.is_function(store.delete_key); do local ret, err = store:delete_key(""); @@ -153,7 +150,7 @@ describe("storagemanager", function () end end); - sql_it("may delete all instances of a specific key", function () + it("may delete all instances of a specific key", function () assert.is_function(store.delete_key); assert(store:set("user9999b", "foo", "hello")); -- cgit v1.2.3 From cb6148d155ea02a68e40b8afb5861451750499ad Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 11 Mar 2020 16:32:41 +0000 Subject: storagemanager, mod_storage_sql: Rename methods to :get_all() and :delete_all() --- spec/core_storagemanager_spec.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index 848f7910..b228c5fd 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -114,47 +114,47 @@ describe("storagemanager", function () end); it("may find all users with a specific key", function () - assert.is_function(store.find_key); + assert.is_function(store.get_all); assert(store:set("user9999b", "bar", "bar")); assert(store:set("user9999c", "foo", "blah")); - local ret, err = store:find_key("foo"); + local ret, err = store:get_all("foo"); assert.is_nil(err); assert.same({ user9999 = "bar", user9999c = "blah" }, ret); end); - it("rejects empty or non-string keys to find_key", function () - assert.is_function(store.find_key); + it("rejects empty or non-string keys to get_all", function () + assert.is_function(store.get_all); do - local ret, err = store:find_key(""); + local ret, err = store:get_all(""); assert.is_nil(ret); assert.is_not_nil(err); end do - local ret, err = store:find_key(true); + local ret, err = store:get_all(true); assert.is_nil(ret); assert.is_not_nil(err); end end); - it("rejects empty or non-string keys to delete_key", function () - assert.is_function(store.delete_key); + it("rejects empty or non-string keys to delete_all", function () + assert.is_function(store.delete_all); do - local ret, err = store:delete_key(""); + local ret, err = store:delete_all(""); assert.is_nil(ret); assert.is_not_nil(err); end do - local ret, err = store:delete_key(true); + local ret, err = store:delete_all(true); assert.is_nil(ret); assert.is_not_nil(err); end end); it("may delete all instances of a specific key", function () - assert.is_function(store.delete_key); + assert.is_function(store.delete_all); assert(store:set("user9999b", "foo", "hello")); - assert(store:delete_key("bar")); + assert(store:delete_all("bar")); -- Ensure key was deleted do local ret, err = store:get("user9999b", "bar"); -- cgit v1.2.3 From 0cf5e523843ae039f2aa005995cd320d7fe2ff1f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 11 May 2020 21:41:02 +0200 Subject: spec/storage: Reset build context of test stanza make comparisons easier While building a stanza there's a .last_add field keeping track of where in the XML tree tags are being added. This field does not survive a roundtrip through preserialize / deserialize. :reset() removes this, which simplifes comparisons after such a roundtrip. --- spec/core_storagemanager_spec.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index b228c5fd..29ab014f 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -206,7 +206,8 @@ describe("storagemanager", function () local test_stanza = st.stanza("test", { xmlns = "urn:example:foo" }) :tag("foo"):up() - :tag("foo"):up(); + :tag("foo"):up() + :reset(); local test_time = 1539204123; local test_data = { -- cgit v1.2.3 From 82714e54a8dca7ff60365758dd497c3c9c270633 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 11 May 2020 21:56:19 +0200 Subject: mod_storage_internal: Implement key-value API --- spec/core_storagemanager_spec.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index 29ab014f..96ccceb6 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -439,6 +439,21 @@ describe("storagemanager", function () assert.equal(2, count); assert(archive:delete("user-issue1073")); end); + + it("can be treated as a map store", function () + assert.falsy(archive:get("mapuser", "no-such-id")); + assert.falsy(archive:set("mapuser", "no-such-id", test_stanza)); + + local id = archive:append("mapuser", nil, test_stanza, test_time, "contact@example.com"); + assert.same(test_stanza, archive:get("mapuser", id)); + + local replacement_stanza = st.stanza("test", { xmlns = "urn:example:foo" }) + :tag("bar"):up() + :reset(); + assert(archive:set("mapuser", id, replacement_stanza)); + assert.same(replacement_stanza, archive:get("mapuser", id)); + end); + end); end); end -- cgit v1.2.3 From d916ce38f6b434552a5ebd2a1751286da9cf2d69 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 15 May 2020 20:55:22 +0200 Subject: mod_storage_internal: Fix keeping old timestamp in archive map API This led to a missing 'when' field on changed items, which would cause a traceack. --- spec/core_storagemanager_spec.lua | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index 96ccceb6..a19edbab 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -445,13 +445,24 @@ describe("storagemanager", function () assert.falsy(archive:set("mapuser", "no-such-id", test_stanza)); local id = archive:append("mapuser", nil, test_stanza, test_time, "contact@example.com"); - assert.same(test_stanza, archive:get("mapuser", id)); + do + local stanza_roundtrip, when, with = archive:get("mapuser", id); + assert.same(test_stanza, stanza_roundtrip, "same stanza is returned"); + assert.equal(test_time, when, "same 'when' is returned"); + assert.equal("contact@example.com", with, "same 'with' is returned"); + end local replacement_stanza = st.stanza("test", { xmlns = "urn:example:foo" }) :tag("bar"):up() :reset(); - assert(archive:set("mapuser", id, replacement_stanza)); - assert.same(replacement_stanza, archive:get("mapuser", id)); + assert(archive:set("mapuser", id, replacement_stanza, test_time+1)); + + do + local replaced, when, with = archive:get("mapuser", id); + assert.same(replacement_stanza, replaced, "replaced stanza is returned"); + assert.equal(test_time+1, when, "modified 'when' is returned"); + assert.equal("contact@example.com", with, "original 'with' is returned"); + end end); end); -- cgit v1.2.3 From f2dbf366426578b2ba233168f8e09cbca0ceec59 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 12 Jun 2020 16:59:06 +0100 Subject: 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. --- spec/core_storagemanager_spec.lua | 52 +++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'spec/core_storagemanager_spec.lua') diff --git a/spec/core_storagemanager_spec.lua b/spec/core_storagemanager_spec.lua index a19edbab..d07dc4ec 100644 --- a/spec/core_storagemanager_spec.lua +++ b/spec/core_storagemanager_spec.lua @@ -215,12 +215,16 @@ describe("storagemanager", function () { nil, test_stanza, test_time+1, "contact2@example.com" }; { nil, test_stanza, test_time+2, "contact2@example.com" }; { nil, test_stanza, test_time-1, "contact2@example.com" }; + { nil, test_stanza, test_time-1, "contact3@example.com" }; + { nil, test_stanza, test_time+0, "contact3@example.com" }; + { nil, test_stanza, test_time+1, "contact3@example.com" }; }; it("can be added to", function () for _, data_item in ipairs(test_data) do - local ok = archive:append("user", unpack(data_item, 1, 4)); - assert.truthy(ok); + local id = archive:append("user", unpack(data_item, 1, 4)); + assert.truthy(id); + data_item[1] = id; end end); @@ -277,7 +281,7 @@ describe("storagemanager", function () assert.equal(2, #item.tags); assert(test_time >= when); end - assert.equal(2, count); + assert.equal(4, count); end); it("by time (start)", function () @@ -296,7 +300,7 @@ describe("storagemanager", function () assert.equal(2, #item.tags); assert(test_time <= when); end - assert.equal(#test_data -1, count); + assert.equal(#test_data - 2, count); end); it("by time (start+end)", function () @@ -317,7 +321,45 @@ describe("storagemanager", function () assert(when >= test_time, ("%d >= %d"):format(when, test_time)); assert(when <= test_time+1, ("%d <= %d"):format(when, test_time+1)); end - assert.equal(2, count); + assert.equal(4, count); + end); + + it("by id (after)", function () + -- luacheck: ignore 211/err + local data, err = archive:find("user", { + ["after"] = test_data[2][1]; + }); + assert.truthy(data); + local count = 0; + for id, item in data do + count = count + 1; + assert.truthy(id); + assert.equal(test_data[2+count][1], id); + assert(st.is_stanza(item)); + assert.equal("test", item.name); + assert.equal("urn:example:foo", item.attr.xmlns); + assert.equal(2, #item.tags); + end + assert.equal(5, count); + end); + + it("by id (before)", function () + -- luacheck: ignore 211/err + local data, err = archive:find("user", { + ["before"] = test_data[4][1]; + }); + assert.truthy(data); + local count = 0; + for id, item in data do + count = count + 1; + assert.truthy(id); + assert.equal(test_data[count][1], id); + assert(st.is_stanza(item)); + assert.equal("test", item.name); + assert.equal("urn:example:foo", item.attr.xmlns); + assert.equal(2, #item.tags); + end + assert.equal(3, count); end); end); -- cgit v1.2.3