Skip to content

Commit

Permalink
feat: Add docker-compose.yml to run test on Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroakis committed Dec 21, 2023
1 parent ea529f9 commit f6f6352
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: test
test:
docker-compose up -d
DB_PORT=15432 go test -v ./...

.PHONY: clean
clean:
docker-compose down -v
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ Args:

## Test

setup database.
Run on docker

```
make
```

or setup manually and run test

```
create database planter;
Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
db:
image: postgres:12.11
ports:
- "15432:5432"
environment:
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
- db:/var/lib/postgresql/data
- ./initdb:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 1s
timeout: 1s
retries: 10
volumes:
db:
3 changes: 3 additions & 0 deletions initdb/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE DATABASE planter;
CREATE USER planter;
CREATE SCHEMA planter AUTHORIZATION planter;
9 changes: 8 additions & 1 deletion planter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
)
Expand All @@ -12,7 +14,12 @@ import (
// CREATE DATABASE planter OWNER planter;

func testPgSetup(t *testing.T) (*sql.DB, func()) {
conn, err := sql.Open("postgres", "user=planter dbname=planter sslmode=disable")
port := os.Getenv("DB_PORT")
if port == "" {
port = "5432"
}
dsn := fmt.Sprintf("user=planter port=%s dbname=planter sslmode=disable", port)
conn, err := sql.Open("postgres", dsn)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f6f6352

Please sign in to comment.