From 3c919f04467fdf3a2605200ddc294dffb25b1265 Mon Sep 17 00:00:00 2001 From: Richard Ward Date: Tue, 30 Dec 2025 12:53:33 +0000 Subject: [PATCH] config: packages,operations,demo (3 files) --- .../scripts/tests/cache_operations.test.lua | 0 .../scripts/tests/kv_operations.cases.json | 47 ++++++++++ .../seed/scripts/tests/kv_operations.test.lua | 91 +++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 packages/dbal_demo/seed/scripts/tests/cache_operations.test.lua create mode 100644 packages/dbal_demo/seed/scripts/tests/kv_operations.cases.json create mode 100644 packages/dbal_demo/seed/scripts/tests/kv_operations.test.lua diff --git a/packages/dbal_demo/seed/scripts/tests/cache_operations.test.lua b/packages/dbal_demo/seed/scripts/tests/cache_operations.test.lua new file mode 100644 index 000000000..e69de29bb diff --git a/packages/dbal_demo/seed/scripts/tests/kv_operations.cases.json b/packages/dbal_demo/seed/scripts/tests/kv_operations.cases.json new file mode 100644 index 000000000..babfe8bb5 --- /dev/null +++ b/packages/dbal_demo/seed/scripts/tests/kv_operations.cases.json @@ -0,0 +1,47 @@ +{ + "set": { + "valid": [ + { "key": "user:123", "value": "John Doe", "ttl": null }, + { "key": "session:abc", "value": "token_data", "ttl": 3600 }, + { "key": "config:app", "value": { "debug": true }, "ttl": null }, + { "key": "counter", "value": 42, "ttl": 86400 }, + { "key": "flags:feature", "value": true, "ttl": null }, + { "key": "cache:list", "value": [1, 2, 3], "ttl": 1800 } + ], + "invalid": [ + { "key": "", "value": "test", "reason": "empty_key" }, + { "key": null, "value": "test", "reason": "null_key" } + ] + }, + "get": { + "found": [ + { "key": "user:123", "stored_value": "John Doe" }, + { "key": "config:app", "stored_value": { "debug": true } }, + { "key": "counter", "stored_value": 42 } + ], + "not_found": [ + { "key": "nonexistent:key" }, + { "key": "expired:session" }, + { "key": "deleted:item" } + ], + "invalid": [ + { "key": "", "reason": "empty_key" }, + { "key": null, "reason": "null_key" } + ] + }, + "delete": { + "success": [ + { "key": "user:123" }, + { "key": "session:abc" }, + { "key": "temp:data" } + ], + "not_found": [ + { "key": "nonexistent:key" }, + { "key": "already:deleted" } + ], + "invalid": [ + { "key": "", "reason": "empty_key" }, + { "key": null, "reason": "null_key" } + ] + } +} diff --git a/packages/dbal_demo/seed/scripts/tests/kv_operations.test.lua b/packages/dbal_demo/seed/scripts/tests/kv_operations.test.lua new file mode 100644 index 000000000..216dc3948 --- /dev/null +++ b/packages/dbal_demo/seed/scripts/tests/kv_operations.test.lua @@ -0,0 +1,91 @@ +-- KV Operations Tests +-- Uses lua_test framework with parameterized test cases + +local describe = require("lua_test.describe") +local it = require("lua_test.it") +local it_each = require("lua_test.it_each") +local expect = require("lua_test.expect") +local beforeEach = require("lua_test.beforeEach") +local mock = require("lua_test.mock") + +local cases = require("tests.kv_operations.cases") +local kv = require("kv_operations") + +describe("kv_operations", function() + -- Mock the global DBAL functions before each test + beforeEach(function() + _G.kv_set = mock.fn(function() return true end) + _G.kv_get = mock.fn(function(key) + if key == "existing_key" then return "stored_value" end + return nil + end) + _G.kv_delete = mock.fn(function(key) + return key == "existing_key" + end) + end) + + describe("set", function() + it_each(cases.set.valid, "should store $key with $value", function(case) + local result = kv.set(case.key, case.value, case.ttl) + expect(result.success).toBe(true) + expect(result.message).toContain(case.key) + if case.ttl then + expect(result.ttl).toBe(case.ttl) + end + end) + + it_each(cases.set.invalid, "should reject invalid input: $reason", function(case) + local result = kv.set(case.key, case.value) + expect(result.success).toBe(false) + expect(result.error).toBeTruthy() + end) + end) + + describe("get", function() + it_each(cases.get.found, "should retrieve $key successfully", function(case) + _G.kv_get = mock.fn(function() return case.stored_value end) + local result = kv.get(case.key) + expect(result.success).toBe(true) + expect(result.value).toBe(case.stored_value) + end) + + it_each(cases.get.not_found, "should handle missing key: $key", function(case) + _G.kv_get = mock.fn(function() return nil end) + local result = kv.get(case.key) + expect(result.success).toBe(false) + expect(result.message).toContain("not found") + end) + + it_each(cases.get.invalid, "should reject invalid key: $reason", function(case) + local result = kv.get(case.key) + expect(result.success).toBe(false) + expect(result.error).toBeTruthy() + end) + end) + + describe("delete", function() + it_each(cases.delete.success, "should delete $key", function(case) + _G.kv_delete = mock.fn(function() return true end) + local result = kv.delete(case.key) + expect(result.success).toBe(true) + expect(result.message).toContain(case.key) + end) + + it_each(cases.delete.not_found, "should handle missing key: $key", function(case) + _G.kv_delete = mock.fn(function() return false end) + local result = kv.delete(case.key) + expect(result.success).toBe(false) + end) + + it_each(cases.delete.invalid, "should reject invalid key: $reason", function(case) + local result = kv.delete(case.key) + expect(result.success).toBe(false) + expect(result.error).toBeTruthy() + end) + end) +end) + +return { + name = "kv_operations.test", + description = "Tests for KV store operations" +}