Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added basic postgres service for unit test action #38

Merged
merged 4 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/[email protected]
- 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
Expand Down
38 changes: 29 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"
Loading