Skip to content

Commit

Permalink
Fix relations and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Jul 5, 2024
1 parent e497675 commit a2133a3
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CREATE TABLE users_roles

DROP TABLE IF EXISTS users_roles;
DROP TABLE IF EXISTS roles;
DROP TABLE IF EXISTS cardgroups_users;
DROP TABLE IF EXISTS card_groups_users;
DROP TABLE IF EXISTS cards;
DROP TABLE IF EXISTS cardgroups;
DROP TABLE IF EXISTS card_groups;
DROP TABLE IF EXISTS users;
7 changes: 6 additions & 1 deletion backend/pkg/flashcard/flashcard_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ func CreateCardReview(card *Card, db *gorm.DB) error {

// MigrateDB performs the database migration for the Card struct
func MigrateDB(db *gorm.DB) error {
return db.AutoMigrate(&Card{})
return db.AutoMigrate(
&CardGroup{},
&Card{},
&User{},
&Role{},
)
}
109 changes: 102 additions & 7 deletions backend/pkg/flashcard/flashcard_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func setupTestDB(t *testing.T) (*gorm.DB, func()) {
}
}

func createTestCardGroup(t *testing.T, db *gorm.DB, name string) uint {
func createTestCardGroup(t *testing.T, db *gorm.DB, name string) CardGroup {
t.Helper()
cardGroup := CardGroup{Name: name}
if err := db.Create(&cardGroup).Error; err != nil {
t.Fatalf("could not create test card group: %v", err)
}
return cardGroup.ID
return cardGroup
}

func createTestCard(t *testing.T, db *gorm.DB, front, back string, reviewDate time.Time, intervalDays int, cardGroupID uint) Card {
Expand All @@ -89,17 +89,35 @@ func createTestCard(t *testing.T, db *gorm.DB, front, back string, reviewDate ti
return card
}

func createTestUser(t *testing.T, db *gorm.DB, id, name string) User {
t.Helper()
user := User{ID: id, Name: name}
if err := db.Create(&user).Error; err != nil {
t.Fatalf("could not create test user: %v", err)
}
return user
}

func createTestRole(t *testing.T, db *gorm.DB, name string) Role {
t.Helper()
role := Role{Name: name}
if err := db.Create(&role).Error; err != nil {
t.Fatalf("could not create test role: %v", err)
}
return role
}

func TestFlashcardFunctions(t *testing.T) {
db, teardown := setupTestDB(t)
defer teardown()

cardGroupID := createTestCardGroup(t, db, "Test Group")
cardGroup := createTestCardGroup(t, db, "Test Group")

t.Run("Test GetDueCards", func(t *testing.T) {
pastDate := time.Now().AddDate(0, 0, -1)
futureDate := time.Now().AddDate(0, 0, 1)
createTestCard(t, db, "Past Due", "Test", pastDate, 1, cardGroupID)
createTestCard(t, db, "Future Due", "Test", futureDate, 1, cardGroupID)
createTestCard(t, db, "Past Due", "Test", pastDate, 1, cardGroup.ID)
createTestCard(t, db, "Future Due", "Test", futureDate, 1, cardGroup.ID)

dueCards, err := GetDueCards(db)
assert.NoError(t, err)
Expand All @@ -108,7 +126,7 @@ func TestFlashcardFunctions(t *testing.T) {
})

t.Run("Test UpdateCardReview", func(t *testing.T) {
card := createTestCard(t, db, "Update Test", "Test", time.Now(), 1, cardGroupID)
card := createTestCard(t, db, "Update Test", "Test", time.Now(), 1, cardGroup.ID)

err := UpdateCardReview(&card, db)
assert.NoError(t, err)
Expand All @@ -125,7 +143,7 @@ func TestFlashcardFunctions(t *testing.T) {
Back: "Test",
ReviewDate: time.Now(),
IntervalDays: 1,
CardGroupID: cardGroupID,
CardGroupID: cardGroup.ID,
}

err := CreateCardReview(&newCard, db)
Expand All @@ -151,4 +169,81 @@ func TestFlashcardFunctions(t *testing.T) {
err := MigrateDB(db)
assert.NoError(t, err)
})

t.Run("Test Card Model", func(t *testing.T) {
card := createTestCard(t, db, "Front", "Back", time.Now(), 1, cardGroup.ID)

assert.NotZero(t, card.ID)
assert.Equal(t, "Front", card.Front)
assert.Equal(t, "Back", card.Back)
assert.NotZero(t, card.Created)
assert.NotZero(t, card.Updated)
assert.Equal(t, cardGroup.ID, card.CardGroupID)

// Test autoCreateTime and autoUpdateTime
createdTime := card.Created
time.Sleep(time.Second)
db.Model(&card).Update("Front", "Updated Front")
db.First(&card, card.ID)
assert.WithinDuration(t, createdTime, card.Created, time.Second)
assert.True(t, card.Updated.After(createdTime))
})

t.Run("Test CardGroup Model", func(t *testing.T) {
assert.NotZero(t, cardGroup.ID)
assert.Equal(t, "Test Group", cardGroup.Name)
assert.NotZero(t, cardGroup.Created)
assert.NotZero(t, cardGroup.Updated)
})

t.Run("Test User Model", func(t *testing.T) {
user := createTestUser(t, db, "user1", "Test User")
assert.Equal(t, "user1", user.ID)
assert.Equal(t, "Test User", user.Name)
assert.NotZero(t, user.Created)
assert.NotZero(t, user.Updated)
})

t.Run("Test Role Model", func(t *testing.T) {
role := createTestRole(t, db, "Admin")
assert.NotZero(t, role.ID)
assert.Equal(t, "Admin", role.Name)
})

t.Run("Test Relationships", func(t *testing.T) {
cardGroup := createTestCardGroup(t, db, "Relationship Group")
card1 := createTestCard(t, db, "Card 1", "Back 1", time.Now(), 1, cardGroup.ID)
card2 := createTestCard(t, db, "Card 2", "Back 2", time.Now(), 1, cardGroup.ID)
user := createTestUser(t, db, "user2", "Relationship User")
role := createTestRole(t, db, "User")

// Test CardGroup - Card relationship
var fetchedCardGroup CardGroup
db.Preload("Cards").First(&fetchedCardGroup, cardGroup.ID)
assert.Len(t, fetchedCardGroup.Cards, 2)
assert.Equal(t, card1.ID, fetchedCardGroup.Cards[0].ID)
assert.Equal(t, card2.ID, fetchedCardGroup.Cards[1].ID)

// Test User - CardGroup relationship
db.Model(&user).Association("CardGroups").Append(&cardGroup)
var fetchedUser User
db.Preload("CardGroups").First(&fetchedUser, "id = ?", user.ID)
assert.Len(t, fetchedUser.CardGroups, 1)
assert.Equal(t, cardGroup.ID, fetchedUser.CardGroups[0].ID)

// Test User - Role relationship
db.Model(&user).Association("Roles").Append(&role)
db.Preload("Roles").First(&fetchedUser, "id = ?", user.ID)
assert.Len(t, fetchedUser.Roles, 1)
assert.Equal(t, role.ID, fetchedUser.Roles[0].ID)

// Clean up user-cardgroup relationships to allow deletion of cardgroup
db.Model(&user).Association("CardGroups").Delete(&cardGroup)

// Test deletion cascade
db.Delete(&cardGroup)
var deletedCards []Card
db.Where("cardgroup_id = ?", cardGroup.ID).Find(&deletedCards)
assert.Len(t, deletedCards, 0)
})
}

0 comments on commit a2133a3

Please sign in to comment.