✅🧱 add tests & ci changes
This commit is contained in:
5
devices-api/.busted
Normal file
5
devices-api/.busted
Normal file
@@ -0,0 +1,5 @@
|
||||
return {
|
||||
default = {
|
||||
ROOT = { "spec" },
|
||||
},
|
||||
}
|
||||
@@ -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
|
||||
|
||||
3
devices-api/run_tests.lua
Normal file
3
devices-api/run_tests.lua
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env lua
|
||||
-- Busted test runner (uses .busted for ROOT = spec)
|
||||
require("busted.runner")()
|
||||
36
devices-api/spec/db_spec.lua
Normal file
36
devices-api/spec/db_spec.lua
Normal 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)
|
||||
19
devices-api/spec/log_spec.lua
Normal file
19
devices-api/spec/log_spec.lua
Normal 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)
|
||||
Reference in New Issue
Block a user