From f5a652c13b41cb47b364725b735a466ab30c8769 Mon Sep 17 00:00:00 2001 From: Shuhrat Tultaganov Date: Tue, 3 Jan 2023 19:27:10 +0300 Subject: [PATCH] review test database and fix config restserver --- build/postgres/Dockerfile | 4 +++- build/postgres/docker-compose_test_db_init.sql | 4 ++-- cmd/rest/main.go | 2 ++ docker-compose.yaml | 2 +- go.mod | 1 + go.sum | 2 ++ internal/bd/bd_test.go | 2 +- internal/bd/config.go | 4 +++- internal/bd/filmrepo_test.go | 2 +- internal/bd/testhelper.go | 2 +- 10 files changed, 17 insertions(+), 8 deletions(-) diff --git a/build/postgres/Dockerfile b/build/postgres/Dockerfile index 84f6652..cbadbc6 100644 --- a/build/postgres/Dockerfile +++ b/build/postgres/Dockerfile @@ -1,3 +1,5 @@ FROM postgres -ADD ./docker-compose_test_db_init.sql /docker-entrypoint-initdb.d/ \ No newline at end of file +ADD docker-compose_test_db_init.sql /docker-entrypoint-initdb.d/docker-compose_test_db_init.sql +RUN chown -R postgres /docker-entrypoint-initdb.d/docker-compose_test_db_init.sql +# ENTRYPOINT ["docker-entrypoint.sh"] \ No newline at end of file diff --git a/build/postgres/docker-compose_test_db_init.sql b/build/postgres/docker-compose_test_db_init.sql index 242d39a..21dc110 100644 --- a/build/postgres/docker-compose_test_db_init.sql +++ b/build/postgres/docker-compose_test_db_init.sql @@ -1,2 +1,2 @@ -CREATE User test with password 'test' createdb; -CREATE DATABASE test with owner = test ENCODING = 'UTF8' LC_COLLATE = 'en_US.utf8' LC_CTYPE = 'en_US.utf8' TABLESPACE = pg_default CONNECTION LIMIT = -1; +CREATE USER test11 WITH PASSWORD 'test' CREATEDB; +CREATE DATABASE test11 with owner = test11 ENCODING = 'UTF8' LC_COLLATE = 'en_US.utf8' LC_CTYPE = 'en_US.utf8' TABLESPACE = pg_default CONNECTION LIMIT = -1; diff --git a/cmd/rest/main.go b/cmd/rest/main.go index 3667156..db90a71 100644 --- a/cmd/rest/main.go +++ b/cmd/rest/main.go @@ -5,6 +5,7 @@ import ( "log" "git.ukamnya.ru/stulyaganov/RestApi/internal/restserver" + "github.com/joho/godotenv" ) var ( @@ -18,6 +19,7 @@ func init() { func main() { flag.Parse() + godotenv.Load(".env") config := restserver.NewConfig() if err := config.SetConfig(configPath); err != nil { diff --git a/docker-compose.yaml b/docker-compose.yaml index 29481d9..d82dc6d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,4 @@ -version: '3.5' +version: '3.7' services: database: diff --git a/go.mod b/go.mod index 4fc08a6..633c781 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/gorilla/mux v1.8.0 // indirect github.com/jackc/pgx v3.6.2+incompatible // indirect github.com/jackc/pgx/v5 v5.2.0 + github.com/joho/godotenv v1.4.0 github.com/lib/pq v1.10.7 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index aab2632..ac02857 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yI github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I= github.com/jackc/pgx/v5 v5.2.0 h1:NdPpngX0Y6z6XDFKqmFQaE+bCtkqzvQIOt1wvBlAqs8= github.com/jackc/pgx/v5 v5.2.0/go.mod h1:Ptn7zmohNsWEsdxRawMzk3gaKma2obW+NWTnKa0S4nk= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/internal/bd/bd_test.go b/internal/bd/bd_test.go index 6e15cc8..69c09cb 100644 --- a/internal/bd/bd_test.go +++ b/internal/bd/bd_test.go @@ -12,7 +12,7 @@ var ( func TestMain(m *testing.M) { database := os.Getenv("DATABASE_URL") if database == "" { - database = "host=localhost user=admin password=root dbname=postgres sslmode=disable" + database = "host=localhost user=test password=test dbname=test sslmode=disable" } os.Exit(m.Run()) diff --git a/internal/bd/config.go b/internal/bd/config.go index 2eb8916..caa307a 100644 --- a/internal/bd/config.go +++ b/internal/bd/config.go @@ -5,5 +5,7 @@ type ConfigBD struct { } func NewConfig() *ConfigBD { - return &ConfigBD{} + return &ConfigBD{ + BaseUrlBd: "host=localhost user=admin password=root dbname=postgres sslmode=disable", + } } diff --git a/internal/bd/filmrepo_test.go b/internal/bd/filmrepo_test.go index f0f13c1..6fe9493 100644 --- a/internal/bd/filmrepo_test.go +++ b/internal/bd/filmrepo_test.go @@ -10,7 +10,7 @@ import ( func TestFilmRepo_Create(t *testing.T) { - s, teardown := bd.Testdb(t, "host=localhost user=admin password=root dbname=postgres sslmode=disable") + s, teardown := bd.Testdb(t, "host=localhost user=test password=test dbname=test sslmode=disable") defer teardown("films") u, err := s.Films().Create(&model.Films{ Id: 11, diff --git a/internal/bd/testhelper.go b/internal/bd/testhelper.go index b8cb27c..bc38b4f 100644 --- a/internal/bd/testhelper.go +++ b/internal/bd/testhelper.go @@ -12,7 +12,7 @@ func Testdb(t *testing.T, databaseurl string) (*Bd, func(...string)) { config := NewConfig() config.BaseUrlBd = databaseurl s := New(config) - + fmt.Println(config.BaseUrlBd) if err := s.Open(); err != nil { t.Fatal(err) }