Changeset

9471:6798fcd25e9c

storagemanager tests: Add many more archive tests, including (failing) cases for #1200 and #1073
author Matthew Wild <mwild1@gmail.com>
date Thu, 11 Oct 2018 14:10:55 +0100
parents 9470:0d491bc98b9f
children 9472:ea40fe484c38
files spec/core_storagemanager_spec.lua
diffstat 1 files changed, 105 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/spec/core_storagemanager_spec.lua	Thu Oct 11 13:29:14 2018 +0100
+++ b/spec/core_storagemanager_spec.lua	Thu Oct 11 14:10:55 2018 +0100
@@ -205,6 +205,35 @@
 					end);
 				end);
 
+				it("can selectively delete items", function ()
+					local delete_id;
+					do
+						local data = assert(archive:find("user", {}));
+						local count = 0;
+						for id, item, when in data do --luacheck: ignore 213/item 213/when
+							count = count + 1;
+							if count == 2 then
+								delete_id = id;
+							end
+							assert.truthy(id);
+						end
+						assert.equal(#test_data, count);
+					end
+
+					assert(archive:delete("user", { key = delete_id }));
+
+					do
+						local data = assert(archive:find("user", {}));
+						local count = 0;
+						for id, item, when in data do --luacheck: ignore 213/item 213/when
+							count = count + 1;
+							assert.truthy(id);
+							assert.not_equal(delete_id, id);
+						end
+						assert.equal(#test_data-1, count);
+					end
+				end);
+
 				it("can be purged", function ()
 					local ok, err = archive:delete("user");
 					assert.truthy(ok);
@@ -218,6 +247,82 @@
 					end
 					assert.equal(0, count);
 				end);
+
+				it("can truncate the oldest items", function ()
+					local username = "user-truncate";
+					for i = 1, 10 do
+						assert(archive:append(username, nil, test_stanza, i, "contact@example.com"));
+					end
+					assert(archive:delete(username, { truncate = 3 }));
+
+					do
+						local data = assert(archive:find(username, {}));
+						local count = 0;
+						for id, item, when in data do --luacheck: ignore 213/when
+							count = count + 1;
+							assert.truthy(id);
+							assert(st.is_stanza(item));
+							assert(when > 7, ("%d > 7"):format(when));
+						end
+						assert.equal(3, count);
+					end
+				end);
+
+				it("overwrites existing keys with new data", function ()
+					local prefix = ("a"):rep(50);
+					local username = "user-overwrite";
+					assert(archive:append(username, prefix.."-1", test_stanza, test_time, "contact@example.com"));
+					assert(archive:append(username, prefix.."-2", test_stanza, test_time, "contact@example.com"));
+
+					do
+						local data = assert(archive:find(username, {}));
+						local count = 0;
+						for id, item, when in data do --luacheck: ignore 213/when
+							count = count + 1;
+							assert.truthy(id);
+							assert.equals(("%s-%d"):format(prefix, count), id);
+							assert(st.is_stanza(item));
+						end
+						assert.equal(2, count);
+					end
+
+					local new_stanza = st.clone(test_stanza);
+					new_stanza.attr.foo = "bar";
+					assert(archive:append(username, prefix.."-2", new_stanza, test_time+1, "contact2@example.com"));
+
+					do
+						local data = assert(archive:find(username, {}));
+						local count = 0;
+						for id, item, when in data do
+							count = count + 1;
+							assert.truthy(id);
+							assert.equals(("%s-%d"):format(prefix, count), id);
+							assert(st.is_stanza(item));
+							if count == 2 then
+								assert.equals(test_time+1, when);
+								assert.equals("bar", item.attr.foo);
+							end
+						end
+						assert.equal(2, count);
+					end
+				end);
+
+				it("can contain multiple long unique keys #issue1073", function ()
+					local prefix = ("a"):rep(50);
+					assert(archive:append("user-issue1073", prefix.."-1", test_stanza, test_time, "contact@example.com"));
+					assert(archive:append("user-issue1073", prefix.."-2", test_stanza, test_time, "contact@example.com"));
+
+					local data = assert(archive:find("user-issue1073", {}));
+					local count = 0;
+					for id, item, when in data do --luacheck: ignore 213/when
+						print(id)
+						count = count + 1;
+						assert.truthy(id);
+						assert(st.is_stanza(item));
+					end
+					assert.equal(2, count);
+					assert(archive:delete("user-issue1073"));
+				end);
 			end);
 		end);
 	end