Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Jul 14, 2024
1 parent 7332b14 commit df7ce23
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
49 changes: 30 additions & 19 deletions backend/graph/schema.resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestMain(m *testing.M) {
defer cleanup(migrationFilePath)

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

Expand All @@ -51,7 +51,6 @@ func TestMain(m *testing.M) {

// Run the tests
m.Run()

}

func setupEchoServer(db *gorm.DB) *echo.Echo {
Expand Down Expand Up @@ -126,6 +125,8 @@ func removeField(data map[string]interface{}, field string) {
}

func TestRemoveField(t *testing.T) {
t.Parallel()

// Sample data
actual := map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -157,19 +158,21 @@ func TestRemoveField(t *testing.T) {
}

func TestMutationResolver_CreateCard(t *testing.T) {
t.Parallel()

// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Name: "TestMutationResolver_CreateCard Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Step 2: Create a new card with the CardgroupID
input := model.NewCard{
Front: "Front of card",
Back: "Back of card",
Front: "CreateCard Front of card",
Back: "CreateCard Back of card",
ReviewDate: now,
IntervalDays: new(int),
CardgroupID: cardgroup.ID,
Expand Down Expand Up @@ -208,19 +211,21 @@ func TestMutationResolver_CreateCard(t *testing.T) {
}

func TestMutationResolver_UpdateCard(t *testing.T) {
t.Parallel()

// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Name: "TestMutationResolver_UpdateCard Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Create a card to update
card := repository.Card{
Front: "Old Front",
Back: "Old Back",
Front: "UpdateCard Old Front",
Back: "UpdateCard Old Back",
ReviewDate: time.Now(),
IntervalDays: 1,
CardGroupID: cardgroup.ID,
Expand All @@ -230,8 +235,8 @@ func TestMutationResolver_UpdateCard(t *testing.T) {
db.Create(&card)

input := model.NewCard{
Front: "New Front",
Back: "New Back",
Front: "UpdateCard New Front",
Back: "UpdateCard New Back",
ReviewDate: time.Now(),
}
jsonInput, _ := json.Marshal(map[string]interface{}{
Expand Down Expand Up @@ -264,19 +269,21 @@ func TestMutationResolver_UpdateCard(t *testing.T) {
}

func TestMutationResolver_DeleteCard(t *testing.T) {
t.Parallel()

// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Name: "TestMutationResolver_DeleteCard Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Step 2: Create a card to delete
card := repository.Card{
Front: "Front to delete",
Back: "Back to delete",
Front: "DeleteCard Front to delete",
Back: "DeleteCard Back to delete",
ReviewDate: time.Now(),
IntervalDays: 1,
CardGroupID: cardgroup.ID, // Ensure this matches the created cardgroup
Expand All @@ -302,19 +309,21 @@ func TestMutationResolver_DeleteCard(t *testing.T) {
}

func TestQueryResolver_Cards(t *testing.T) {
t.Parallel()

// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Name: "TestQueryResolver_Cards Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Step 2: Create a Card associated with the created Cardgroup
card := repository.Card{
Front: "Front",
Back: "Back",
Front: "Cards Front",
Back: "Cards Back",
ReviewDate: now,
IntervalDays: 1,
CardGroupID: cardgroup.ID, // Ensure this matches the created Cardgroup ID
Expand Down Expand Up @@ -352,19 +361,21 @@ func TestQueryResolver_Cards(t *testing.T) {
}

func TestQueryResolver_Card(t *testing.T) {
t.Parallel()

// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Name: "TestQueryResolver_Card Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Step 2: Create a Card associated with the created Cardgroup
card := repository.Card{
Front: "Front",
Back: "Back",
Front: "Card Front",
Back: "Card Back",
ReviewDate: now,
IntervalDays: 1,
CardGroupID: cardgroup.ID, // Ensure this matches the created Cardgroup ID
Expand Down
2 changes: 1 addition & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func initializeDatabase() *gorm.DB {

// Run migrations
log.Printf("Data Migration start ===============")
if err := pg.RunGooseMigrations(fullPath); err != nil {
if err := pg.RunGooseMigrationsUp(fullPath); err != nil {
log.Fatalf("failed to run migrations: %v", err)
}
log.Printf("Data Migration Done ===============")
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/repository/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (pg *Postgres) Open() error {
return nil
}

func (pg *Postgres) RunGooseMigrations(path string) error {
func (pg *Postgres) RunGooseMigrationsUp(path string) error {
dsn := pg.DSN()
cmd := exec.Command("goose", "-dir", path, "postgres", dsn, "up")
cmd.Stdout = os.Stdout
Expand Down
6 changes: 3 additions & 3 deletions backend/pkg/repository/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func setupTestDB(ctx context.Context, dbName string) (*Postgres, func(), error)

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

Expand Down Expand Up @@ -96,7 +96,7 @@ func TestPostgres_Open(t *testing.T) {
t.Fatalf("failed to ping database: %s", err)
}

if err = pg.RunGooseMigrations(migrationFilePath); err != nil {
if err = pg.RunGooseMigrationsUp(migrationFilePath); err != nil {
t.Fatalf("goose migration failed: %s", err)
}

Expand All @@ -114,7 +114,7 @@ func TestPostgres_RunGooseMigrations(t *testing.T) {
}
defer cleanup()

if err = pg.RunGooseMigrations(migrationFilePath); err != nil {
if err = pg.RunGooseMigrationsUp(migrationFilePath); err != nil {
t.Fatalf("goose migration failed: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion backend/testutils/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func SetupTestDB(ctx context.Context, user, password, dbName string) (*repositor

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

Expand Down

0 comments on commit df7ce23

Please sign in to comment.