Skip to content

Commit

Permalink
Fix return cards from strategies and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 22, 2024
1 parent 735b5f2 commit 709712d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
6 changes: 1 addition & 5 deletions backend/pkg/usecases/swipe_manager/swipe_manager_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,11 @@ func (s *swipeManagerUsecase) DetermineCardAmount(
amountOfKnownWords int) (int, error) {
cardAmount := amountOfKnownWords
if len(cards) <= amountOfKnownWords {
cardAmount = len(cards) - 1
cardAmount = len(cards)
if cardAmount < 0 {
cardAmount = 0
}
}

if cardAmount == 0 {
return 0, goerr.New("no cards available to return")
}

return cardAmount, nil
}
57 changes: 50 additions & 7 deletions backend/pkg/usecases/swipe_manager/swipe_manager_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"backend/graph/model"
"backend/graph/services"
"backend/pkg/config"
"backend/pkg/logger"
repo "backend/pkg/repository"
"backend/testutils"
"context"
Expand Down Expand Up @@ -170,6 +171,10 @@ func (suite *SwipeManagerTestSuite) TestUpdateRecords() {
assert.NoError(suite.T(), err)
assert.IsType(suite.T(), &difficultStateStrategy{}, strategy)
assert.Equal(suite.T(), DIFFICULT, mode)

// Make sure if cards are returned
cards, err := usecase.ExecuteStrategy(ctx, savedSwipeRecord, strategy)
assert.NotEmpty(suite.T(), cards)
})

suite.Run("Normal_DefaultStateStrategy", func() {
Expand Down Expand Up @@ -198,7 +203,10 @@ func (suite *SwipeManagerTestSuite) TestUpdateRecords() {
assert.IsType(suite.T(), &defaultStateStrategy{}, strategy)
assert.Equal(suite.T(), DEFAULT, mode)

// Additional assertions can be added here if necessary to validate the behavior of the strategy
// Make sure if cards are returned
cards, err := usecase.ExecuteStrategy(ctx, savedSwipeRecord, strategy)
assert.NotEmpty(suite.T(), cards)
assert.Equal(suite.T(), 1, len(cards))
})

suite.Run("Normal_GoodStateStrategy", func() {
Expand Down Expand Up @@ -247,12 +255,20 @@ func (suite *SwipeManagerTestSuite) TestUpdateRecords() {
assert.NoError(suite.T(), err)
assert.IsType(suite.T(), &goodStateStrategy{}, strategy)
assert.Equal(suite.T(), GOOD, mode)

// Make sure if cards are returned
cards, err := usecase.ExecuteStrategy(ctx, savedSwipeRecord, strategy)
assert.NotEmpty(suite.T(), cards)
assert.Equal(suite.T(), config.Cfg.FLBatchDefaultAmount, len(cards))
} else {
logger.Logger.Info("Skip test due to the random data does not hit" +
" the count.")
}
})

suite.Run("Normal_EasyStateStrategy", func() {
// Arrange
card, _, user, err := testutils.CreateUserCardAndCardGroup(ctx,
card, cardGroup, user, err := testutils.CreateUserCardAndCardGroup(ctx,
suite.userService, suite.cardGroupService, suite.roleService, suite.cardService)
assert.NoError(suite.T(), err)

Expand All @@ -265,8 +281,17 @@ func (suite *SwipeManagerTestSuite) TestUpdateRecords() {
mode = services.KNOWN // Set Mode to KNOWN for all records
}

input := model.NewCard{
Front: "Front " + strconv.Itoa(i),
Back: "Back " + strconv.Itoa(i),
ReviewDate: time.Now().UTC(),
CardgroupID: cardGroup.ID,
}
createdCard, err := suite.cardService.CreateCard(ctx, input)
assert.NoError(suite.T(), err)

newSwipeRecord := model.NewSwipeRecord{
CardID: card.ID,
CardID: createdCard.ID,
CardGroupID: card.CardGroupID,
UserID: user.ID,
Mode: mode,
Expand All @@ -288,11 +313,16 @@ func (suite *SwipeManagerTestSuite) TestUpdateRecords() {
assert.NoError(suite.T(), err)
assert.IsType(suite.T(), &easyStateStrategy{}, strategy)
assert.Equal(suite.T(), EASY, mode)

// Make sure if cards are returned
cards, err := usecase.ExecuteStrategy(ctx, savedSwipeRecord, strategy)
assert.NotEmpty(suite.T(), cards)
assert.Equal(suite.T(), config.Cfg.FLBatchDefaultAmount, len(cards))
})

suite.Run("Normal_InWhileStateStrategy", func() {
// Arrange
card, _, user, err := testutils.CreateUserCardAndCardGroup(ctx,
_, cardGroup, user, err := testutils.CreateUserCardAndCardGroup(ctx,
suite.userService, suite.cardGroupService, suite.roleService, suite.cardService)
assert.NoError(suite.T(), err)

Expand All @@ -303,11 +333,20 @@ func (suite *SwipeManagerTestSuite) TestUpdateRecords() {
savedSwipeRecord := model.NewSwipeRecord{}
var swipeRecords []*repository.SwipeRecord
for i := 0; i < config.Cfg.FLBatchDefaultAmount; i++ {
input := model.NewCard{
Front: "Front " + strconv.Itoa(i),
Back: "Back " + strconv.Itoa(i),
ReviewDate: time.Now().UTC(),
CardgroupID: cardGroup.ID,
}
createdCard, err := suite.cardService.CreateCard(ctx, input)
assert.NoError(suite.T(), err)

mode := services.KNOWN // Ensuring all records are KNOWN

newSwipeRecord := model.NewSwipeRecord{
CardID: card.ID,
CardGroupID: card.CardGroupID,
CardID: createdCard.ID,
CardGroupID: cardGroup.ID,
UserID: user.ID,
Mode: mode,
Created: twoMonthsAgo,
Expand All @@ -328,6 +367,10 @@ func (suite *SwipeManagerTestSuite) TestUpdateRecords() {
assert.NoError(suite.T(), err)
assert.IsType(suite.T(), &inWhileStateStrategy{}, strategy)
assert.Equal(suite.T(), INWHILE, mode)

// Make sure if cards are returned
cards, err := usecase.ExecuteStrategy(ctx, savedSwipeRecord, strategy)
assert.Equal(suite.T(), config.Cfg.FLBatchDefaultAmount, len(cards))
})

suite.Run("Normal_DetermineCardAmount", func() {
Expand Down Expand Up @@ -369,7 +412,7 @@ func (suite *SwipeManagerTestSuite) TestUpdateRecords() {
cardAmount, err := usecase.DetermineCardAmount(cards, amountOfKnownWords)

// Assert
assert.Error(suite.T(), err)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 0, cardAmount)
})
}
Expand Down

0 comments on commit 709712d

Please sign in to comment.