diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d97170f..9fb6a82 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,9 +16,9 @@ jobs: # - macOS-latest # - windows-latest go: + - "1.21" + - "1.20" - "1.19" - - "1.18" - - "1.17" services: postgres: image: postgres:12 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/go.mod b/go.mod index bf04295..b68a84e 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,14 @@ module github.com/achiku/planter -go 1.19 +go 1.21 require ( github.com/alecthomas/kingpin v2.2.6+incompatible - github.com/lib/pq v1.2.0 - github.com/pkg/errors v0.8.1 + github.com/lib/pq v1.10.9 + github.com/pkg/errors v0.9.1 ) require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect - github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect + github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect ) diff --git a/go.sum b/go.sum index deb2cb3..0f6a243 100644 --- a/go.sum +++ b/go.sum @@ -2,14 +2,14 @@ github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvr github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E= -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= +github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 h1:ez/4by2iGztzR4L0zgAOR8lTQK9VlyBVVd7G4omaOQs= +github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 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) }