test: add comprehensive test suite for backend

Add 56 tests covering the core backend packages:

Unit tests (no database required):
- internal/partnum: 7 tests for part number generation logic
  (sequence, format templates, enum validation, constants)
- internal/schema: 8 tests for YAML schema loading, property
  merging, validation, and default application

Integration tests (require TEST_DATABASE_URL):
- internal/db/items: 10 tests for item CRUD, archive/unarchive,
  revisions, and thumbnail operations
- internal/db/relationships: 10 tests for BOM CRUD, cycle detection,
  self-reference blocking, where-used, expanded/flat BOM
- internal/db/projects: 5 tests for project CRUD and item association
- internal/api/bom_handlers: 6 HTTP handler tests for BOM endpoints
  including flat BOM, cost calculation, add/delete entries
- internal/api/items: 5 HTTP handler tests for item CRUD endpoints

Infrastructure:
- internal/testutil: shared helpers for test DB pool setup,
  migration runner, and table truncation
- internal/db/helpers_test.go: DB wrapper for integration tests
- internal/db/db.go: add NewFromPool constructor
- Makefile: add test-integration target with default DSN

Integration tests skip gracefully when TEST_DATABASE_URL is unset.
Dev-mode auth (nil authConfig) used for API handler tests.

Fixes: fmt.Errorf Go vet warning in partnum/generator.go

Closes #2
This commit is contained in:
Forbes
2026-02-07 01:57:10 -06:00
parent 3704adb584
commit d08b178466
12 changed files with 1616 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
.PHONY: build run test clean migrate fmt lint \
.PHONY: build run test test-integration clean migrate fmt lint \
docker-build docker-up docker-down docker-logs docker-ps \
docker-clean docker-rebuild \
web-install web-dev web-build
@@ -7,8 +7,8 @@
# Local Development
# =============================================================================
# Build all binaries
build:
# Build all binaries (frontend + backend)
build: web-build
go build -o silo ./cmd/silo
go build -o silod ./cmd/silod
@@ -20,14 +20,19 @@ run:
cli:
go run ./cmd/silo $(ARGS)
# Run tests
# Run unit tests (integration tests skipped without TEST_DATABASE_URL)
test:
go test -v ./...
# Run all tests including integration tests (requires PostgreSQL)
test-integration:
TEST_DATABASE_URL="postgres://silo:silodev@localhost:5432/silo_test?sslmode=disable" go test -v -count=1 ./...
# Clean build artifacts
clean:
rm -f silo silod
rm -f *.out
rm -rf web/dist
# Format code
fmt:
@@ -153,7 +158,8 @@ help:
@echo " build - Build CLI and server binaries"
@echo " run - Run API server locally"
@echo " cli ARGS=... - Run CLI with arguments"
@echo " test - Run tests"
@echo " test - Run unit tests (integration tests skip without DB)"
@echo " test-integration - Run all tests including integration (needs PostgreSQL)"
@echo " fmt - Format code"
@echo " lint - Run linter"
@echo " tidy - Tidy go.mod"