diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 3fb25f3..2ab4c7d 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -6,6 +6,18 @@ jobs: test: name: Run Tests runs-on: ubuntu-latest + services: + postgres: + image: postgres:16 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_PORT: 5432 + POSTGRES_DB: postgres + options: >- + --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 + ports: + - 5432:5432 steps: - uses: actions/checkout@v4 - name: Set up Go @@ -15,6 +27,10 @@ jobs: cache: true - name: Install dependencies run: go mod download + - name: Install Goose + run: go install github.com/pressly/goose/v3/cmd/goose@v3.22.1 + - name: Run migrations + run: goose -dir .sqlc/migrations postgres "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" up - name: Run tests run: go test -v ./... # Comment test results on the PR diff --git a/Makefile b/Makefile index ad3ac81..3a8f7b3 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ # migration name NAME ?= name -TEST_DB_PASSWORD ?= password +# default database configuration +TEST_DB_USER ?= postgres +TEST_DB_PASSWORD ?= postgres +TEST_DB_NAME ?= postgres TEST_DB_HOST_PORT ?= 5432 -TEST_DB_URL ?= postgres://postgres:$(TEST_DB_PASSWORD)@localhost:$(TEST_DB_HOST_PORT)/postgres?sslmode=disable -POSTGRESQL_VERSION=16 +TEST_DB_URL ?= postgres://$(TEST_DB_USER):$(TEST_DB_PASSWORD)@localhost:$(TEST_DB_HOST_PORT)/$(TEST_DB_NAME)?sslmode=disable +POSTGRESQL_VERSION ?= 16 .PHONY: query @@ -54,14 +57,31 @@ test: start-testdb start-testdb: @echo "Setting up PostgreSQL database..." - @docker run --name test-postgres -e POSTGRES_PASSWORD=$(TEST_DB_PASSWORD) -d -p "$(TEST_DB_HOST_PORT):5432" postgres:$(POSTGRESQL_VERSION) + @if ! docker ps -a --filter "name=test-postgres" --format '{{.Names}}' | grep -q '^test-postgres$$'; then \ + docker run --name test-postgres \ + -e POSTGRES_USER=$(TEST_DB_USER) \ + -e POSTGRES_PASSWORD=$(TEST_DB_PASSWORD) \ + -e POSTGRES_DB=$(TEST_DB_NAME) \ + -d -p "$(TEST_DB_HOST_PORT):5432" \ + postgres:$(POSTGRESQL_VERSION); \ + elif ! docker ps --filter "name=test-postgres" --format '{{.Names}}' | grep -q '^test-postgres$$'; then \ + docker start test-postgres; \ + fi @echo "Waiting for PostgreSQL to be ready..." - @until docker exec test-postgres pg_isready -U postgres; do sleep 1; done - @goose -dir ./migrations postgres $(TEST_DB_URL) up + @timeout 30s bash -c 'until docker exec test-postgres pg_isready -U $(TEST_DB_USER); do sleep 1; done' + @echo "Running migrations..." + @goose -dir .sqlc/migrations postgres "$(TEST_DB_URL)" up run-tests: - APP_ENV=test VERSION=0.0.0-test PASS_ENCRYPT_ALGO=md5 DB_URL=$(TEST_DB_URL) go test -v ./router ./store + @go test -v ./... stop-testdb: - @docker container stop test-postgres - @docker container rm -f test-postgres + @echo "Stopping test db..." + @docker container stop test-postgres || true + @echo "Removing test db..." + @docker container rm -f test-postgres || true + +# helper target to check database connection +check-testdb: + @echo "Testing database connection..." + @docker exec test-postgres psql -U $(TEST_DB_USER) -d $(TEST_DB_NAME) -c "SELECT 1"