Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Jul 14, 2024
1 parent 911f13c commit 695b1e3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
7 changes: 4 additions & 3 deletions backend/graph/schema.resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatalf("Failed to setup test database: %v", err)
}
defer cleanup()
defer cleanup(migrationFilePath)

// Run migrations
if err := pg.RunGooseMigrations(migrationFilePath); err != nil {
Expand All @@ -51,6 +51,7 @@ func TestMain(m *testing.M) {

// Run the tests
m.Run()

}

func setupEchoServer(db *gorm.DB) *echo.Echo {
Expand Down Expand Up @@ -168,7 +169,7 @@ func TestMutationResolver_CreateCard(t *testing.T) {
}
}`, cardgroup.Created.Format(time.RFC3339Nano), cardgroup.Created.Format(time.RFC3339Nano), cardgroup.Updated.Format(time.RFC3339Nano))

testGraphQLQuery(t, e, jsonInput, expected, "data.createCard.created", "data.createCard.updated")
testGraphQLQuery(t, e, jsonInput, expected, "data.createCard.created", "data.createCard.updated", "data.createCard.review_date")
}

func TestMutationResolver_UpdateCard(t *testing.T) {
Expand Down Expand Up @@ -307,7 +308,7 @@ func TestQueryResolver_Cards(t *testing.T) {
"id": %d,
"front": "Front",
"back": "Back",
"interval_days": 1,
"interval_days": 1
}]
}
}`, card.ID)
Expand Down
11 changes: 11 additions & 0 deletions backend/pkg/repository/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ func (pg *Postgres) RunGooseMigrations(path string) error {
}
return nil
}

func (pg *Postgres) RunGooseMigrationsDown(path string) error {
dsn := pg.DSN()
cmd := exec.Command("goose", "-dir", path, "postgres", dsn, "down")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("goose migration down failed: %w", err)
}
return nil
}
5 changes: 5 additions & 0 deletions backend/pkg/repository/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func setupTestDB(ctx context.Context, dbName string) (*Postgres, func(), error)
}

cleanup := func() {
// Run migrations
if err := pg.RunGooseMigrations(migrationFilePath); err != nil {
log.Fatalf("failed to run migrations: %v", err)
}

if err := pgContainer.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate postgres container: %s", err)
}
Expand Down
9 changes: 7 additions & 2 deletions backend/testutils/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// SetupTestDB sets up a Postgres test container and returns the connection and a cleanup function.
func SetupTestDB(ctx context.Context, user, password, dbName string) (*repository.Postgres, func(), error) {
func SetupTestDB(ctx context.Context, user, password, dbName string) (*repository.Postgres, func(migrationFilePath string), error) {
req := testcontainers.ContainerRequest{
Image: "postgres:16",
ExposedPorts: []string{"5432/tcp"},
Expand Down Expand Up @@ -60,7 +60,12 @@ func SetupTestDB(ctx context.Context, user, password, dbName string) (*repositor
return nil, nil, fmt.Errorf("failed to open database: %w", err)
}

cleanup := func() {
cleanup := func(migrationFilePath string) {
// Run migrations
if err := pg.RunGooseMigrations(migrationFilePath); err != nil {
log.Fatalf("failed to run migrations: %v", err)
}

if err := pgContainer.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate postgres container: %s", err)
}
Expand Down

0 comments on commit 695b1e3

Please sign in to comment.