From f6f635218f0c4d82caadb8e60fffb755632529cd Mon Sep 17 00:00:00 2001 From: Hiroaki Sano Date: Thu, 21 Dec 2023 16:33:59 +0900 Subject: [PATCH] feat: Add docker-compose.yml to run test on Docker --- Makefile | 8 ++++++++ README.md | 8 +++++++- docker-compose.yml | 17 +++++++++++++++++ initdb/init.sql | 3 +++ planter_test.go | 9 ++++++++- 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 docker-compose.yml create mode 100644 initdb/init.sql diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4dcc59e --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.PHONY: test +test: + docker-compose up -d + DB_PORT=15432 go test -v ./... + +.PHONY: clean +clean: + docker-compose down -v diff --git a/README.md b/README.md index 58fd6ad..8e34bc2 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,13 @@ Args: ## Test -setup database. +Run on docker + +``` +make +``` + +or setup manually and run test ``` create database planter; diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1fdd9c1 --- /dev/null +++ b/docker-compose.yml @@ -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: diff --git a/initdb/init.sql b/initdb/init.sql new file mode 100644 index 0000000..dc1067c --- /dev/null +++ b/initdb/init.sql @@ -0,0 +1,3 @@ +CREATE DATABASE planter; +CREATE USER planter; +CREATE SCHEMA planter AUTHORIZATION planter; diff --git a/planter_test.go b/planter_test.go index 8b24227..09ffc88 100644 --- a/planter_test.go +++ b/planter_test.go @@ -2,7 +2,9 @@ package main import ( "database/sql" + "fmt" "io/ioutil" + "os" "reflect" "testing" ) @@ -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) }