Add members methods and tests

This commit is contained in:
Miguel de la Cruz 2021-09-13 22:05:56 +02:00
parent 70d0eb8334
commit 403c99032d
2 changed files with 55 additions and 13 deletions

View file

@ -134,13 +134,13 @@ func (gs *GameStore) gameMembersFromRows(rows *sql.Rows) ([]*model.GameMember, e
return gameMembers, nil
}
func (gs *GameStore) GetGameMembers(gameID int) ([]*model.GameMember, error) {
func (gs *GameStore) GetMembers(gameID int) ([]*model.GameMember, error) {
rows, err := gs.Q().Select(gameMemberColumns...).
From("gamemembers").
Where(sq.Eq{"game_id": gameID}).
Query()
if err != nil {
return nil, fmt.Errorf("cannot get gamemembers: %w", err)
return nil, fmt.Errorf("cannot get game members: %w", err)
}
defer rows.Close()
@ -155,3 +155,29 @@ func (gs *GameStore) GetGameMembers(gameID int) ([]*model.GameMember, error) {
return gameMembers, nil
}
func (gs *GameStore) GetMember(gameID, userID int) (*model.GameMember, error) {
rows, err := gs.Q().Select(gameMemberColumns...).
From("gamemembers").
Where(sq.Eq{"game_id": gameID, "user_id": userID}).
Query()
if err != nil {
return nil, fmt.Errorf("cannot get game member: %w", err)
}
defer rows.Close()
gameMembers, err := gs.gameMembersFromRows(rows)
if err != nil {
return nil, fmt.Errorf("cannot get game member from rows: %w", err)
}
if len(gameMembers) == 0 {
return nil, sql.ErrNoRows
}
return gameMembers[0], nil
}
// func (gs *GameStore) AddMember(gameID, userID int) (*model.GameMember, err) {
// }

View file

@ -15,16 +15,32 @@ func TestCreateGame(t *testing.T) {
user := th.NewUser()
gameName := "Awesome Game"
game, err := th.store.Game().Create(gameName, user.ID)
var game *model.Game
var gameMember *model.GameMember
var err error
t.Run("should correctly create a game", func(t *testing.T) {
game, err = th.store.Game().Create(gameName, user.ID)
require.NoError(t, err)
require.NotZero(t, game.ID)
require.Equal(t, gameName, game.Name)
require.Equal(t, user.ID, game.UserID)
})
gameMembers, err := th.store.Game().GetGameMembers(game.ID)
t.Run("should correctly fetch game members", func(t *testing.T) {
gameMembers, err := th.store.Game().GetMembers(game.ID)
require.NoError(t, err)
require.Len(t, gameMembers, 1)
require.Equal(t, game.ID, gameMembers[0].GameID)
require.Equal(t, user.ID, gameMembers[0].UserID)
require.Equal(t, model.RoleGameMaster, gameMembers[0].Role)
})
t.Run("should correctly fetch member for user", func(t *testing.T) {
gameMember, err = th.store.Game().GetMember(game.ID, user.ID)
require.NoError(t, err)
require.Equal(t, game.ID, gameMember.GameID)
require.Equal(t, user.ID, gameMember.UserID)
require.Equal(t, model.RoleGameMaster, gameMember.Role)
})
}