Skip to content

Commit

Permalink
Add Swipe manager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 21, 2024
1 parent efbada7 commit 60a37ab
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 116 deletions.
2 changes: 1 addition & 1 deletion backend/graph/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"
)

// Skip fields for relations
// User Skip fields for relations
// https://qiita.com/kiki-ki/items/5f8ec3e198f2d4b19e42
type User struct {
ID int64 `gorm:"column:id;primaryKey" validate:"number"`
Expand Down
4 changes: 2 additions & 2 deletions backend/graph/services/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewCardService(db *gorm.DB, defaultLimit int) CardService {
return &cardService{db: db, defaultLimit: defaultLimit}
}

func ConvertToGormCard(input model.NewCard) *repository.Card {
func ConvertToGormCardFromNew(input model.NewCard) *repository.Card {
return &repository.Card{
Front: input.Front,
Back: input.Back,
Expand Down Expand Up @@ -115,7 +115,7 @@ func (s *cardService) GetCardByID(ctx context.Context, id int64) (*model.Card, e
}

func (s *cardService) CreateCard(ctx context.Context, input model.NewCard) (*model.Card, error) {
gormCard := ConvertToGormCard(input)
gormCard := ConvertToGormCardFromNew(input)
result := s.db.WithContext(ctx).Create(gormCard)
if result.Error != nil {
if strings.Contains(result.Error.Error(), "foreign key constraint") {
Expand Down
20 changes: 17 additions & 3 deletions backend/graph/services/cardgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ type CardGroupService interface {
GetCardGroupsByIDs(ctx context.Context, ids []int64) ([]*model.CardGroup, error)
UpdateCardGroupUserState(ctx context.Context, cardGroupID int64, userID int64, newState int) error
GetLatestCardgroupUsers(ctx context.Context, cardGroupID int64, limit int, sortOrder string) ([]*repository.CardgroupUser, error)
GetCardgroupUser(ctx context.Context, cardGroupID int64, userID int64) (*repository.CardgroupUser, error)
}

// NewCardGroupService creates a new CardGroupService instance.
func NewCardGroupService(db *gorm.DB, defaultLimit int) CardGroupService {
return &cardGroupService{db: db, defaultLimit: defaultLimit}
}

// ConvertToGormCardGroup converts a NewCardGroup input to a GORM-compatible Cardgroup model.
func ConvertToGormCardGroup(input model.NewCardGroup) *repository.Cardgroup {
// ConvertToGormCardGroupFromNew converts a NewCardGroup input to a GORM-compatible Cardgroup model.
func ConvertToGormCardGroupFromNew(input model.NewCardGroup) *repository.Cardgroup {
return &repository.Cardgroup{
Name: input.Name,
Created: time.Now().UTC(),
Expand Down Expand Up @@ -72,7 +73,7 @@ func (s *cardGroupService) GetCardGroupByID(ctx context.Context, id int64) (*mod

// CreateCardGroup creates a new card group in the database.
func (s *cardGroupService) CreateCardGroup(ctx context.Context, input model.NewCardGroup) (*model.CardGroup, error) {
gormCardGroup := ConvertToGormCardGroup(input)
gormCardGroup := ConvertToGormCardGroupFromNew(input)
result := s.db.WithContext(ctx).Create(&gormCardGroup)
if result.Error != nil {
return nil, goerr.Wrap(result.Error, "failed to create card group")
Expand Down Expand Up @@ -283,3 +284,16 @@ func (s *cardGroupService) GetLatestCardgroupUsers(ctx context.Context, cardGrou

return cardgroupUsers, nil
}

func (s *cardGroupService) GetCardgroupUser(ctx context.Context, cardGroupID int64, userID int64) (*repository.CardgroupUser, error) {
var cardgroupUser repository.CardgroupUser
if err := s.db.WithContext(ctx).
Where("cardgroup_id = ? AND user_id = ?", cardGroupID, userID).
First(&cardgroupUser).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, goerr.Wrap(err, fmt.Errorf("cardgroup user not found for cardGroupID: %d, userID: %d", cardGroupID, userID))
}
return nil, goerr.Wrap(err, "failed to retrieve cardgroup user")
}
return &cardgroupUser, nil
}
30 changes: 15 additions & 15 deletions backend/graph/services/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ func NewRoleService(db *gorm.DB, defaultLimit int) RoleService {
return &roleService{db: db, defaultLimit: defaultLimit}
}

func convertToRole(role repository.Role) *model.Role {
func ConvertToGormRoleFromNew(input model.NewRole) repository.Role {
return repository.Role{
Name: input.Name,
}
}

func ConvertToRole(role repository.Role) *model.Role {
return &model.Role{
ID: role.ID,
Name: role.Name,
Expand All @@ -42,12 +48,6 @@ func convertToRole(role repository.Role) *model.Role {
}
}

func convertToGormRole(input model.NewRole) repository.Role {
return repository.Role{
Name: input.Name,
}
}

func (s *roleService) GetRoleByUserID(ctx context.Context, userID int64) (*model.Role, error) {
var user repository.User
if err := s.db.WithContext(ctx).Preload("Roles").First(&user, userID).Error; err != nil {
Expand All @@ -58,7 +58,7 @@ func (s *roleService) GetRoleByUserID(ctx context.Context, userID int64) (*model
return nil, goerr.Wrap(err, "no roles found for user")
}
role := user.Roles[0] // Assuming a user has only one role
return convertToRole(role), nil
return ConvertToRole(role), nil
}

func (s *roleService) GetRoleByID(ctx context.Context, id int64) (*model.Role, error) {
Expand All @@ -69,16 +69,16 @@ func (s *roleService) GetRoleByID(ctx context.Context, id int64) (*model.Role, e
}
return nil, goerr.Wrap(err, "failed to get role by ID")
}
return convertToRole(role), nil
return ConvertToRole(role), nil
}

func (s *roleService) CreateRole(ctx context.Context, input model.NewRole) (*model.Role, error) {
gormRole := convertToGormRole(input)
gormRole := ConvertToGormRoleFromNew(input)
result := s.db.WithContext(ctx).Create(&gormRole)
if result.Error != nil {
return nil, goerr.Wrap(result.Error, "failed to create role")
}
return convertToRole(gormRole), nil
return ConvertToRole(gormRole), nil
}

func (s *roleService) UpdateRole(ctx context.Context, id int64, input model.NewRole) (*model.Role, error) {
Expand All @@ -90,7 +90,7 @@ func (s *roleService) UpdateRole(ctx context.Context, id int64, input model.NewR
if err := s.db.WithContext(ctx).Save(&role).Error; err != nil {
return nil, goerr.Wrap(err, "failed to update role")
}
return convertToRole(role), nil
return ConvertToRole(role), nil
}

func (s *roleService) DeleteRole(ctx context.Context, id int64) (*bool, error) {
Expand Down Expand Up @@ -160,7 +160,7 @@ func (s *roleService) Roles(ctx context.Context) ([]*model.Role, error) {
}
var gqlRoles []*model.Role
for _, role := range roles {
gqlRoles = append(gqlRoles, convertToRole(role))
gqlRoles = append(gqlRoles, ConvertToRole(role))
}
return gqlRoles, nil
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func (s *roleService) PaginatedRolesByUser(ctx context.Context, userID int64, fi
var edges []*model.RoleEdge
var nodes []*model.Role
for _, role := range roles {
node := convertToRole(role)
node := ConvertToRole(role)
edges = append(edges, &model.RoleEdge{
Cursor: role.ID,
Node: node,
Expand Down Expand Up @@ -225,7 +225,7 @@ func (s *roleService) GetRolesByIDs(ctx context.Context, ids []int64) ([]*model.

var gqlRoles []*model.Role
for _, role := range roles {
gqlRoles = append(gqlRoles, convertToRole(*role))
gqlRoles = append(gqlRoles, ConvertToRole(*role))
}

return gqlRoles, nil
Expand Down
17 changes: 14 additions & 3 deletions backend/graph/services/swiperecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
const (
UNDEFINED = 0
KNOWN = 1
DONTKNOW = 2
UNKNOWN = 2
MAYBE = 3
)

Expand All @@ -41,7 +41,18 @@ func NewSwipeRecordService(db *gorm.DB, defaultLimit int) SwipeRecordService {
return &swipeRecordService{db: db, defaultLimit: defaultLimit}
}

func ConvertToGormSwipeRecord(input model.NewSwipeRecord) *repository.SwipeRecord {
func ConvertToGormSwipeRecordFromNew(input model.NewSwipeRecord) *repository.SwipeRecord {
return &repository.SwipeRecord{
UserID: input.UserID,
CardID: input.CardID,
CardGroupID: input.CardGroupID,
Mode: input.Mode,
Created: input.Created,
Updated: input.Updated,
}
}

func ConvertToGormSwipeRecord(input model.SwipeRecord) *repository.SwipeRecord {
return &repository.SwipeRecord{
UserID: input.UserID,
CardID: input.CardID,
Expand Down Expand Up @@ -76,7 +87,7 @@ func (s *swipeRecordService) GetSwipeRecordByID(ctx context.Context, id int64) (
}

func (s *swipeRecordService) CreateSwipeRecord(ctx context.Context, input model.NewSwipeRecord) (*model.SwipeRecord, error) {
gormSwipeRecord := ConvertToGormSwipeRecord(input)
gormSwipeRecord := ConvertToGormSwipeRecordFromNew(input)
result := s.db.WithContext(ctx).Create(gormSwipeRecord)
if result.Error != nil {
if strings.Contains(result.Error.Error(), "foreign key constraint") {
Expand Down
20 changes: 10 additions & 10 deletions backend/graph/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func NewUserService(db *gorm.DB, defaultLimit int) UserService {
return &userService{db: db, defaultLimit: defaultLimit}
}

func convertToGormUser(input model.NewUser) *db.User {
func ConvertToGormUserFromNew(input model.NewUser) *db.User {
return &db.User{
Name: input.Name,
Created: time.Now().UTC(),
Updated: time.Now().UTC(),
}
}

func convertToUser(user db.User) *model.User {
func ConvertToUser(user db.User) *model.User {
return &model.User{
ID: user.ID,
Name: user.Name,
Expand All @@ -55,7 +55,7 @@ func (s *userService) GetUsersByRole(ctx context.Context, roleID int64) ([]*mode
}
var gqlUsers []*model.User
for _, user := range role.Users {
gqlUsers = append(gqlUsers, convertToUser(user))
gqlUsers = append(gqlUsers, ConvertToUser(user))
}
return gqlUsers, nil
}
Expand All @@ -65,19 +65,19 @@ func (s *userService) GetUserByID(ctx context.Context, id int64) (*model.User, e
if err := s.db.First(&user, id).Error; err != nil {
return nil, goerr.Wrap(err, fmt.Sprintf("failed to get user by ID: %d", id))
}
return convertToUser(user), nil
return ConvertToUser(user), nil
}

func (s *userService) CreateUser(ctx context.Context, input model.NewUser) (*model.User, error) {
gormUser := convertToGormUser(input)
gormUser := ConvertToGormUserFromNew(input)
result := s.db.WithContext(ctx).Create(gormUser)
if result.Error != nil {
if strings.Contains(result.Error.Error(), "unique constraint") {
return nil, goerr.Wrap(fmt.Errorf("user already exists"), result.Error)
}
return nil, goerr.Wrap(result.Error, "failed to create user")
}
return convertToUser(*gormUser), nil
return ConvertToUser(*gormUser), nil
}

func (s *userService) UpdateUser(ctx context.Context, id int64, input model.NewUser) (*model.User, error) {
Expand All @@ -90,7 +90,7 @@ func (s *userService) UpdateUser(ctx context.Context, id int64, input model.NewU
if err := s.db.WithContext(ctx).Save(&user).Error; err != nil {
return nil, goerr.Wrap(err, "failed to update user")
}
return convertToUser(user), nil
return ConvertToUser(user), nil
}

func (s *userService) DeleteUser(ctx context.Context, id int64) (*bool, error) {
Expand All @@ -109,7 +109,7 @@ func (s *userService) Users(ctx context.Context) ([]*model.User, error) {
}
var gqlUsers []*model.User
for _, user := range users {
gqlUsers = append(gqlUsers, convertToUser(user))
gqlUsers = append(gqlUsers, ConvertToUser(user))
}
return gqlUsers, nil
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (s *userService) PaginatedUsersByRole(ctx context.Context, roleID int64, fi
var edges []*model.UserEdge
var nodes []*model.User
for _, user := range paginatedUsers {
node := convertToUser(user)
node := ConvertToUser(user)
edges = append(edges, &model.UserEdge{
Cursor: user.ID,
Node: node,
Expand Down Expand Up @@ -195,7 +195,7 @@ func (s *userService) GetUsersByIDs(ctx context.Context, ids []int64) ([]*model.
}
var gqlUsers []*model.User
for _, user := range users {
gqlUsers = append(gqlUsers, convertToUser(user))
gqlUsers = append(gqlUsers, ConvertToUser(user))
}
return gqlUsers, nil
}
Loading

0 comments on commit 60a37ab

Please sign in to comment.