From cb31e75bb1f7fd484965f9fc759a2b08a1c2e5d7 Mon Sep 17 00:00:00 2001 From: Graham Mather Date: Thu, 28 Sep 2023 20:29:39 -0400 Subject: [PATCH] Seperated testdb and api, tests can now be run independenlty of one another --- api/docker-compose.test.yml | 7 ------- api/tests/api_test.go | 4 ++-- api/tests/db_test.go | 14 ++++++-------- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/api/docker-compose.test.yml b/api/docker-compose.test.yml index 3e58954..fd074b4 100644 --- a/api/docker-compose.test.yml +++ b/api/docker-compose.test.yml @@ -14,10 +14,3 @@ services: POSTGRES_DB: testdb POSTGRES_USER: testuser POSTGRES_PASSWORD: testpwd - test-api: - build: . - environment: - TEST_DATABASE_URL: postgres://testuser:testpwd@test-db:5432/testdb - depends_on: - - test-db - command: [ "go", "test", "-v", "./..." ] \ No newline at end of file diff --git a/api/tests/api_test.go b/api/tests/api_test.go index 0241ab7..c2d65cf 100644 --- a/api/tests/api_test.go +++ b/api/tests/api_test.go @@ -20,7 +20,7 @@ import ( func TestAddExampleGift(t *testing.T) { // Database setup - dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable" + dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable" if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists { dsn = dbURL } @@ -86,7 +86,7 @@ func TestAddExampleGift(t *testing.T) { } func TestGetExampleGift(t *testing.T) { // Database setup - dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable" + dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable" if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists { dsn = dbURL } diff --git a/api/tests/db_test.go b/api/tests/db_test.go index b36b14b..2633820 100644 --- a/api/tests/db_test.go +++ b/api/tests/db_test.go @@ -12,7 +12,7 @@ import ( ) func TestDBConnection(t *testing.T) { - dsn := "user=testuser password=testpwd host=test-db port=5432 dbname=testdb sslmode=disable" + dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable" db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { @@ -34,7 +34,7 @@ func TestDBConnection(t *testing.T) { func TestExampleGiftModel(t *testing.T) { // This code should be the same for each test - dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable" + dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable" if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists { dsn = dbURL } @@ -88,10 +88,9 @@ func TestExampleGiftModel(t *testing.T) { } - func TestUserModel(t *testing.T) { // This code should be the same for each test - dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable" + dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable" if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists { dsn = dbURL } @@ -147,7 +146,7 @@ func TestUserModel(t *testing.T) { func TestAdminModel(t *testing.T) { // This code should be the same for each test - dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable" + dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable" if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists { dsn = dbURL } @@ -200,7 +199,7 @@ func TestAdminModel(t *testing.T) { func TestCustomerModel(t *testing.T) { // This code should be the same for each test - dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable" + dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable" if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists { dsn = dbURL } @@ -229,7 +228,7 @@ func TestCustomerModel(t *testing.T) { assert.Equal(t, customer.ID, fetchedCustomer.ID) assert.Equal(t, customer.UserID, fetchedCustomer.UserID) assert.Equal(t, customer.CreatedAt.In(time.UTC).Round(time.Millisecond), - fetchedCustomer.CreatedAt.In(time.UTC).Round(time.Millisecond)) + fetchedCustomer.CreatedAt.In(time.UTC).Round(time.Millisecond)) // Update customer err = db.Model(&fetchedCustomer).Update("UserID", uint(4)).Error @@ -250,4 +249,3 @@ func TestCustomerModel(t *testing.T) { db.Model(&model.Customer{}).Where("id = ?", updatedCustomer.ID).Count(&count) assert.Equal(t, int64(0), count) } -