File

spec/util_datamanager_spec.lua @ 13114:025c38ee885d

util.dependencies: Print tables itself to reduce number of imports Rationale: See diffstat When this module is imported, it ends up calling stty via term_width() in util.human.io.table(). When this happens outside of a terminal, the following message is sent to stdout: stty: 'standard input': Inappropriate ioctl for device Not importing this module avoids that. Furthermore three is value in this module having minimal dependencies as they might not be available when it does the checks. Ref a1fed82c44b9
author Kim Alvefur <zash@zash.se>
date Fri, 26 May 2023 21:18:27 +0200
parent 11370:7c2ef5a1ec9c
line wrap: on
line source

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)