This document explains how to run tests in the MIDAS project.
# Run all tests (starts database automatically)
./test.sh
# Or use Make
make testMIDAS has three types of tests:
Fast in-memory tests that don't require external dependencies.
# Via script
./test.sh unit
# Via Make
make test-unit
# Manually
go test ./internal/audit/... ./internal/envelope/...Packages with unit tests:
internal/audit- Audit event creation, hash chaininternal/envelope- Envelope state machine, five-section structureinternal/decision- Orchestrator logic (in-memory tests only)
Tests that validate behavior against a real PostgreSQL database.
# Via script (starts database automatically)
./test.sh db
# Via Make (starts database automatically)
make test-integration
# Manually (requires DATABASE_URL)
DATABASE_URL="postgresql://midas:midas@127.0.0.1:5432/midas?sslmode=disable" \
go test ./internal/decision/... ./internal/store/postgres/... -run PostgresPackages with integration tests:
internal/decision- Postgres orchestrator tests (atomicity, persistence)internal/store/postgres- Store transaction tests
# Via script (recommended)
./test.sh
# Via Make
make test
# Manually (requires DATABASE_URL)
DATABASE_URL="postgresql://midas:midas@127.0.0.1:5432/midas?sslmode=disable" \
go test ./...Integration tests require a PostgreSQL database. The test scripts handle this automatically, but you can also manage it manually:
The ./test.sh and make test commands automatically:
- Start Docker Postgres if not running
- Set the correct
DATABASE_URL - Run the tests
# Start database
docker compose up -d postgres
# Set environment variable
export DATABASE_URL="postgresql://midas:midas@127.0.0.1:5432/midas?sslmode=disable"
# Run tests
go test ./...
# Stop database when done
docker compose downThe Docker Postgres instance uses these credentials:
- User:
midas - Password:
midas - Database:
midas - Port:
5432
Important: Integration tests will skip gracefully if DATABASE_URL is not set. You'll see:
? github.com/accept-io/midas/internal/decision/... [no test files]
# Generate coverage report
./test.sh coverage
# View in browser
go tool cover -html=coverage.outFor CI environments, use:
# Start database
docker compose up -d postgres
sleep 5
# Run tests with proper credentials
DATABASE_URL="postgresql://midas:midas@127.0.0.1:5432/midas?sslmode=disable" \
go test ./... -v
# Cleanup
docker compose down -vinternal/
├── audit/ ← Hash chain, event creation
│ ├── *_test.go
├── decision/ ← Orchestrator (unit + integration)
│ ├── orchestrator_test.go # Unit tests
│ ├── orchestrator_lifecycle_test.go # Lifecycle tests
│ ├── orchestrator_postgres_test.go # Integration tests
│ └── orchestrator_postgres_atomicity_test.go # Atomicity tests
├── envelope/ ← State machine, sections
│ └── envelope_test.go
└── store/postgres/ ← Store transactions
└── store_test.go
This means DATABASE_URL is set but pointing to wrong credentials. Either:
- Use
./test.shormake testwhich set the correct URL - Unset
DATABASE_URLto skip Postgres tests:unset DATABASE_URL && go test ./... - Set it correctly:
export DATABASE_URL="postgresql://midas:midas@127.0.0.1:5432/midas?sslmode=disable"
Database isn't running. Start it with:
docker compose up -d postgres
sleep 5 # Wait for initializationDatabase might be initializing. Wait 10 seconds and try again, or check logs:
docker compose logs postgres- Use the test script:
./test.shhandles all setup automatically - Run unit tests frequently: They're fast and don't need infrastructure
- Run integration tests before commits: Ensures database behavior is correct
- Clean database between test runs:
docker compose down -vremoves old data - Check coverage periodically:
./test.sh coverageshows gaps
| Command | Description |
|---|---|
./test.sh |
Run all tests (starts DB automatically) |
./test.sh unit |
Unit tests only (no DB) |
./test.sh db |
Integration tests only (starts DB) |
./test.sh coverage |
Generate coverage report |
make test |
All tests via Makefile |
make test-unit |
Unit tests via Makefile |
make test-integration |
Integration tests via Makefile |
go test ./... |
Raw Go command (manual DB setup) |