Skip to content

Commit 95910fd

Browse files
committed
solve docker tesing on test db
1 parent c876301 commit 95910fd

6 files changed

Lines changed: 34 additions & 84 deletions

File tree

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ DB_MIN_POOL_SIZE=2
1313
DB_MAX_POOL_SIZE=5
1414
DB_QUERY_TIMEOUT_SEC=60
1515

16+
# To run migration on test database since
17+
# docker load only from .env file
18+
TEST_DB_NAME=goserver_test_db
19+
TEST_DB_USER=goserver_test_db_user
20+
TEST_DB_USER_PWD=changeit
21+
1622
REDIS_HOST=redis
1723
REDIS_PORT=6379
1824
REDIS_PASSWORD=changeit

.extra/setup/init-test-db.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Create test user
2+
CREATE USER goserver_test_db_user WITH PASSWORD 'changeit';
3+
4+
-- Create test database
5+
CREATE DATABASE goserver_test_db OWNER goserver_test_db_user;
6+
7+
GRANT ALL PRIVILEGES ON DATABASE goserver_test_db TO goserver_test_db_user;

.test.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# debug, release, test
22
GO_MODE=debug
33

4-
SERVER_HOST=localhost
4+
SERVER_HOST=0.0.0.0
55
SERVER_PORT=8081
66

7-
DB_HOST=postgres-test
7+
DB_HOST=postgres
88
DB_PORT=5432
99
DB_NAME=goserver_test_db
1010
DB_USER=goserver_test_db_user
@@ -13,7 +13,7 @@ DB_MIN_POOL_SIZE=2
1313
DB_MAX_POOL_SIZE=5
1414
DB_QUERY_TIMEOUT_SEC=60
1515

16-
REDIS_HOST=redis-test
16+
REDIS_HOST=redis
1717
REDIS_PORT=6379
1818
REDIS_PASSWORD=changeit
1919

docker-compose.test.yml

Lines changed: 0 additions & 79 deletions
This file was deleted.

docker-compose.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ services:
33
build:
44
context: .
55
dockerfile: Dockerfile
6+
container_name: goserve_example_api_server_postgres
67
restart: unless-stopped
78
env_file: .env
89
ports:
@@ -18,14 +19,15 @@ services:
1819
restart: unless-stopped
1920
env_file: .env
2021
environment:
22+
POSTGRES_DB: ${DB_NAME}
2123
POSTGRES_USER: ${DB_USER}
2224
POSTGRES_PASSWORD: ${DB_USER_PWD}
23-
POSTGRES_DB: ${DB_NAME}
2425
ports:
2526
- '${DB_PORT}:5432'
2627
volumes:
2728
- dbdata:/data/db
2829
# optional pg seed scripts
30+
- ./.extra/setup/init-test-db.sql:/docker-entrypoint-initdb.d/init-test-db.sql:ro
2931
- ./.extra/setup/pgseed.sql:/docker-entrypoint-initdb.d/pgseed.sql:ro
3032
healthcheck:
3133
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$DB_NAME"]
@@ -48,6 +50,20 @@ services:
4850
timeout: 3s
4951
retries: 5
5052

53+
migrate:
54+
image: migrate/migrate
55+
env_file: .env
56+
volumes:
57+
- ./migrations:/migrations
58+
depends_on:
59+
postgres:
60+
condition: service_healthy
61+
command:
62+
[
63+
"-path", "/migrations",
64+
"-database", "postgres://${TEST_DB_USER}:${TEST_DB_USER_PWD}@postgres:5432/${TEST_DB_NAME}?sslmode=disable",
65+
"up"
66+
]
5167
volumes:
5268
dbdata:
5369
cache:

tests/auth_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestIntegrationAuthController_SignupSuccess(t *testing.T) {
2121
t.Fatalf("could not create apikey: %v", err)
2222
}
2323

24-
body := `{"email":"test@abc.com","password":123456,"name":"test name"}`
24+
body := `{"email":"test@abc.com","password":"123456","name":"test name"}`
2525

2626
req, err := http.NewRequest("POST", "/auth/signup/basic", bytes.NewBuffer([]byte(body)))
2727
if err != nil {

0 commit comments

Comments
 (0)