Skip to content

Commit

Permalink
Fix tests and add swipe management files
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 15, 2024
1 parent f8724f3 commit ef60a7c
Show file tree
Hide file tree
Showing 16 changed files with 386 additions and 15 deletions.
19 changes: 19 additions & 0 deletions backend/graph/services/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type CardService interface {
GetCardsByIDs(ctx context.Context, ids []int64) ([]*model.Card, error)
FetchAllCardsByCardGroup(ctx context.Context, cardGroupID int64, first *int) ([]*model.Card, error)
AddNewCards(ctx context.Context, targetCards []model.Card, cardGroupID int64) ([]*model.Card, error)
GetCardsByUserAndCardGroup(ctx context.Context, cardGroupID int64, order string, limit int) ([]repository.Card, error)
}

func NewCardService(db *gorm.DB, defaultLimit int) CardService {
Expand Down Expand Up @@ -302,3 +303,21 @@ func (s *cardService) AddNewCards(ctx context.Context, targetCards []model.Card,

return modifiedCards, nil
}

func (s *cardService) GetCardsByUserAndCardGroup(
ctx context.Context, cardGroupID int64, order string, limit int) ([]repository.Card, error) {
var cards []repository.Card

// Query to find the latest cards with matching user_id and cardgroup_id
err := s.db.WithContext(ctx).
Where("cardgroup_id = ?", cardGroupID).
Order(fmt.Sprintf("updated %s", order)).
Limit(limit).
Find(&cards).Error

if err != nil {
return nil, goerr.Wrap(err, "Failed to get latest cards by user and card group")
}

return cards, nil
}
45 changes: 44 additions & 1 deletion backend/graph/services/card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services_test
import (
"backend/graph/model"
"backend/graph/services"
repo "backend/pkg/repository"
"backend/testutils"
"context"
"strconv"
Expand Down Expand Up @@ -206,8 +207,11 @@ func (suite *CardTestSuite) TestCardService() {

suite.Run("Normal_FetchAllCardsByCardGroup", func() {
// Arrange
createdGroup, _, _ := testutils.CreateUserAndCardGroup(ctx, userService, cardGroupService, roleService)
createdGroup, createdUser, _ := testutils.CreateUserAndCardGroup(ctx, userService, cardGroupService, roleService)

if createdUser != nil {

}
// Create 200 dummy cards
for i := 0; i < 200; i++ {
input := model.NewCard{
Expand Down Expand Up @@ -425,6 +429,45 @@ func (suite *CardTestSuite) TestCardService() {
assert.Equal(t, createdCard.ID, updatedCard.ID) // Ensure the same card ID is retained
})

suite.Run("Normal_GetCardsByUserAndCardGroup", func() {
// Create a user and a card group
createdGroup, _, _ := testutils.CreateUserAndCardGroup(ctx, userService, cardGroupService, roleService)

// Create some cards
for i := 0; i < 5; i++ {
input := model.NewCard{
Front: "Front " + strconv.Itoa(i),
Back: "Back " + strconv.Itoa(i),
ReviewDate: time.Now().UTC(),
CardgroupID: createdGroup.ID,
}
_, err := cardService.CreateCard(ctx, input)
assert.NoError(suite.T(), err)
}

// Act
cards, err := cardService.GetCardsByUserAndCardGroup(ctx, createdGroup.ID, repo.DESC, 3)

// Assert
assert.NoError(suite.T(), err)
assert.Len(suite.T(), cards, 3)
for i := 0; i < 3; i++ {
assert.Equal(suite.T(), "Front "+strconv.Itoa(4-i), cards[i].Front)
}
})

suite.Run("Error_GetCardsByUserAndCardGroup_NoCards", func() {
// Arrange
createdGroup, _, _ := testutils.CreateUserAndCardGroup(ctx, userService, cardGroupService, roleService)

// Act
cards, err := cardService.GetCardsByUserAndCardGroup(ctx, createdGroup.ID, "desc", 3)

// Assert
assert.NoError(suite.T(), err)
assert.Empty(suite.T(), cards)
})

}

func TestCardTestSuite(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions backend/graph/services/cardgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,6 @@ func (suite *CardGroupTestSuite) TestCardGroupService() {
})

suite.Run("Normal_CardGroupUserState", func() {
userService := services.NewUserService(suite.db, 20)
cardGroupService := services.NewCardGroupService(suite.db, 20)
roleService := services.NewRoleService(suite.db, 20)

ctx := context.Background()
cardGroup, user, _ := testutils.CreateUserAndCardGroup(ctx, userService, cardGroupService, roleService)

// Perform the UpdateCardGroupUserState operation
Expand Down
22 changes: 22 additions & 0 deletions backend/graph/services/swiperecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type swipeRecordService struct {
defaultLimit int
}

const (
KNOWN = "known"
DONTKNOW = "dontknow"
MAYBE = "maybe"
)

type SwipeRecordService interface {
GetSwipeRecordByID(ctx context.Context, id int64) (*model.SwipeRecord, error)
CreateSwipeRecord(ctx context.Context, input model.NewSwipeRecord) (*model.SwipeRecord, error)
Expand All @@ -27,6 +33,7 @@ type SwipeRecordService interface {
SwipeRecordsByUser(ctx context.Context, userID int64) ([]*model.SwipeRecord, error)
PaginatedSwipeRecordsByUser(ctx context.Context, userID int64, first *int, after *int64, last *int, before *int64) (*model.SwipeRecordConnection, error)
GetSwipeRecordsByIDs(ctx context.Context, ids []int64) ([]*model.SwipeRecord, error)
GetSwipeRecordsByUserAndOrder(ctx context.Context, userID int64, order string, limit int) ([]repository.SwipeRecord, error)
}

func NewSwipeRecordService(db *gorm.DB, defaultLimit int) SwipeRecordService {
Expand Down Expand Up @@ -195,3 +202,18 @@ func (s *swipeRecordService) GetSwipeRecordsByIDs(ctx context.Context, ids []int

return gqlSwipeRecords, nil
}

func (s *swipeRecordService) GetSwipeRecordsByUserAndOrder(ctx context.Context, userID int64, order string, limit int) ([]repository.SwipeRecord, error) {

orderClause := fmt.Sprintf("updated %s", order)

var swipeRecords []repository.SwipeRecord

query := s.db.WithContext(ctx).Where("user_id = ?", userID).Limit(limit).Order(orderClause)

if err := query.Find(&swipeRecords).Error; err != nil {
return nil, goerr.Wrap(err, fmt.Sprintf("User id: %d", userID))
}

return swipeRecords, nil
}
41 changes: 41 additions & 0 deletions backend/graph/services/swiperecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,47 @@ func (suite *SwipeRecordTestSuite) TestSwipeRecordService() {
assert.Error(t, err)
assert.Nil(t, swipeRecords)
})

suite.Run("Normal_GetSwipeRecordsByUserAndOrder", func() {
// Use the helper function to create the user and role
userID, err := suite.createTestUserAndRole(ctx)
if err != nil {
suite.T().Fatal(err)
}

// Create multiple swipe records for the user
newSwipeRecord1 := model.NewSwipeRecord{
UserID: userID,
Direction: "left",
Created: time.Now().UTC(),
Updated: time.Now().UTC(),
}
swipeRecordService.CreateSwipeRecord(ctx, newSwipeRecord1)

newSwipeRecord2 := model.NewSwipeRecord{
UserID: userID,
Direction: "right",
Created: time.Now().UTC(),
Updated: time.Now().UTC(),
}
swipeRecordService.CreateSwipeRecord(ctx, newSwipeRecord2)

// Fetch swipe records by user and order
swipeRecords, err := swipeRecordService.GetSwipeRecordsByUserAndOrder(ctx, userID, "desc", 2)

assert.NoError(t, err)
assert.Len(t, swipeRecords, 2)
assert.Equal(t, "right", swipeRecords[0].Direction) // Assuming the latest update was "right"
})

suite.Run("Error_GetSwipeRecordsByUserAndOrder", func() {
// Attempt to fetch swipe records for an invalid user ID
swipeRecords, err := swipeRecordService.GetSwipeRecordsByUserAndOrder(ctx, -1, "desc", 2)

assert.NoError(t, err)
assert.Empty(t, swipeRecords)
})

}

func TestSwipeRecordTestSuite(t *testing.T) {
Expand Down
10 changes: 9 additions & 1 deletion backend/pkg/repository/repository.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package repository

import "gorm.io/gorm"
import (
"gorm.io/gorm"
)

// Repository Common database interface
type Repository interface {
GetConfig() DBConfig
GetDB() *gorm.DB
Expand All @@ -10,3 +13,8 @@ type Repository interface {
RunGooseMigrationsUp(path string) error
RunGooseMigrationsDown(path string) error
}

const (
DESC = "desc"
ASC = "asc"
)
11 changes: 11 additions & 0 deletions backend/pkg/textdic/text_dictionary_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ There is no leeway to provide services free of charge for the sake of others.
var inputEOF = `leeway 〔自分の好きなように行動・思考できる〕自由(裁量)度◆不可〔時間・金などの〕余裕、ゆとり
There is no leeway to provide services free of charge for the sake of others. 他人のために無償でサービスをする余裕はない。`

var inputMix = `get on with ~に急がせる、Get on with it. : 急げ。/さっさとやれ。
Hold me accountable for 自分の行動の結果を受け入れ、罰を受け、または自分が引き起こした損害を修復することを意味します。`

// Define the test cases
var testCases = []struct {
name string
Expand Down Expand Up @@ -61,6 +64,14 @@ There is no leeway to provide services free of charge for the sake of others.
{Word: "There is no leeway to provide services free of charge for the sake of others.", Definition: "他人のために無償でサービスをする余裕はない。"},
},
},
{
name: "Valid input MIX",
input: inputMix,
expected: []Node{
{Word: "get on with", Definition: "~に急がせる、Get on with it. : 急げ。/さっさとやれ。"},
{Word: "Hold me accountable for", Definition: "自分の行動の結果を受け入れ、罰を受け、または自分が引き起こした損害を修復することを意味します。"},
},
},
}

// Run TestParserService
Expand Down
12 changes: 12 additions & 0 deletions backend/pkg/usecases/swipe_manager/def1_state_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package swipe_manager

import (
repository "backend/graph/db"
"golang.org/x/net/context"
)

type Def1StateStrategy struct{}

func (d Def1StateStrategy) ChangeState(ctx context.Context, s *SwipeManagerUsecase, swipeRecords []repository.SwipeRecord) error {
return s.changeState(ctx, userID, DEF1)
}
12 changes: 12 additions & 0 deletions backend/pkg/usecases/swipe_manager/def2_state_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package swipe_manager

import (
repository "backend/graph/db"
"golang.org/x/net/context"
)

type Def2StateStrategy struct{}

func (d Def2StateStrategy) ChangeState(ctx context.Context, s *SwipeManagerUsecase, swipeRecords []repository.SwipeRecord) error {
return s.changeState(ctx, userID, DEF2)
}
12 changes: 12 additions & 0 deletions backend/pkg/usecases/swipe_manager/difficult_state_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package swipe_manager

import (
repository "backend/graph/db"
"golang.org/x/net/context"
)

type DifficultStateStrategy struct{}

func (d DifficultStateStrategy) ChangeState(ctx context.Context, s *SwipeManagerUsecase, swipeRecords []repository.SwipeRecord) error {
return s.changeState(ctx, userID, DIFFICULT)
}
12 changes: 12 additions & 0 deletions backend/pkg/usecases/swipe_manager/easy_state_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package swipe_manager

import (
repository "backend/graph/db"
"golang.org/x/net/context"
)

type EasyStateStrategy struct{}

func (e EasyStateStrategy) ChangeState(ctx context.Context, s *SwipeManagerUsecase, swipeRecords []repository.SwipeRecord) error {
return s.changeState(ctx, userID, EASY)
}
12 changes: 12 additions & 0 deletions backend/pkg/usecases/swipe_manager/good_state_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package swipe_manager

import (
repository "backend/graph/db"
"golang.org/x/net/context"
)

type GoodStateStrategy struct{}

func (g GoodStateStrategy) ChangeState(ctx context.Context, s *SwipeManagerUsecase, swipeRecords []repository.SwipeRecord) error {
return s.changeState(ctx, userID, GOOD)
}
23 changes: 23 additions & 0 deletions backend/pkg/usecases/swipe_manager/state_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package swipe_manager

import (
repository "backend/graph/db"
"golang.org/x/net/context"
)

type StateStrategy interface {
ChangeState(ctx context.Context, s *SwipeManagerUsecase, swipeRecords []repository.SwipeRecord) error
}

type StateContext struct {
strategy StateStrategy
}

func (sc *StateContext) SetStrategy(strategy StateStrategy) {
sc.strategy = strategy
}

func (sc *StateContext) ExecuteStrategy(ctx context.Context, s *SwipeManagerUsecase, swipeRecords []repository.SwipeRecord) ([]repository.Card, error) {
sc.strategy.ChangeState(ctx, s, swipeRecords)
return nil, nil
}
Loading

0 comments on commit ef60a7c

Please sign in to comment.