Skip to content

Commit

Permalink
Seperated testdb and api, tests can now be run independenlty of one a…
Browse files Browse the repository at this point in the history
…nother
  • Loading branch information
matherg committed Sep 29, 2023
1 parent 52216ae commit cb31e75
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
7 changes: 0 additions & 7 deletions api/docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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", "./..." ]
4 changes: 2 additions & 2 deletions api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 6 additions & 8 deletions api/tests/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit cb31e75

Please sign in to comment.