🧱 add tests & ci changes
All checks were successful
CI / Lint (push) Successful in 21s
CI / Helm Lint (push) Successful in 6s
CI / Build (push) Successful in 1m48s
CI / Test (push) Successful in 44s

This commit is contained in:
Christian van Dijk
2026-02-23 22:52:54 +01:00
parent dbbe991a3d
commit 59ddc66a8e
10 changed files with 309 additions and 72 deletions

5
devices-api/.busted Normal file
View File

@@ -0,0 +1,5 @@
return {
default = {
ROOT = { "spec" },
},
}

View File

@@ -1,39 +1,51 @@
.PHONY: init-db dev build clean logs down
.PHONY: run test lint deps init-db dev build logs logs-worker logs-all down shell-postgres shell-api shell-worker status
# Docker Compose: use parent dir's compose file (when run via make -C devices-api)
COMPOSE := docker-compose -f ../docker-compose.yml
run:
lua app-standalone.lua
test:
lua run_tests.lua
lint:
luacheck . --codes
deps:
luarocks install lua-cjson luasocket pgmoon redis-lua luaossl
# Docker Compose targets (run from project root: make -C devices-api <target>)
init-db:
docker-compose exec postgres psql -U devices_user -d handheld_devices -f /docker-entrypoint-initdb.d/001_create_devices.sql
$(COMPOSE) exec postgres psql -U devices_user -d handheld_devices \
-f /docker-entrypoint-initdb.d/001_create_devices.sql
dev up:
$(COMPOSE) up -d
build:
docker-compose build
dev:
docker-compose up -d
$(COMPOSE) build
logs:
docker-compose logs -f api
$(COMPOSE) logs -f api
logs-worker:
docker-compose logs -f worker
$(COMPOSE) logs -f worker
logs-all:
docker-compose logs -f
$(COMPOSE) logs -f
down:
docker-compose down
down-volumes:
docker-compose down -v
$(COMPOSE) down
shell-postgres:
docker-compose exec postgres psql -U devices_user -d handheld_devices
$(COMPOSE) exec postgres psql -U devices_user -d handheld_devices
shell-api:
docker-compose exec api sh
$(COMPOSE) exec api sh
shell-worker:
docker-compose exec worker sh
clean: down-volumes
$(COMPOSE) exec worker sh
status:
docker-compose ps
$(COMPOSE) ps

View File

@@ -0,0 +1,3 @@
#!/usr/bin/env lua
-- Busted test runner (uses .busted for ROOT = spec)
require("busted.runner")()

View File

@@ -0,0 +1,36 @@
describe("db", function()
local db
before_each(function()
db = require("db")
end)
describe("config", function()
it("has required connection fields", function()
assert.is_table(db.config)
assert.is_string(db.config.host)
assert.is_string(db.config.port)
assert.is_string(db.config.database)
assert.is_string(db.config.user)
assert.is_string(db.config.password)
end)
end)
describe("with_retry", function()
it("returns result when function succeeds immediately", function()
local result, err = db.with_retry(function()
return "ok"
end)
assert.are.equal("ok", result)
assert.is_nil(err)
end)
it("returns nil, err when function fails with non-retryable error", function()
local result, err = db.with_retry(function()
return nil, "validation failed"
end)
assert.is_nil(result)
assert.are.equal("validation failed", err)
end)
end)
end)

View File

@@ -0,0 +1,19 @@
describe("log", function()
local log
before_each(function()
log = require("log")
end)
it("exposes info, warn, error functions", function()
assert.is_function(log.info)
assert.is_function(log.warn)
assert.is_function(log.error)
end)
it("calls info without error", function()
assert.has_no.errors(function()
log.info("test message", { component = "test" })
end)
end)
end)