From 403c99032d493fcb91efd161590329ba6d2735c4 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 13 Sep 2021 22:05:56 +0200 Subject: [PATCH] Add members methods and tests --- server/services/store/game.go | 30 +++++++++++++++++++++-- server/services/store/game_test.go | 38 +++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/server/services/store/game.go b/server/services/store/game.go index 6c669ca..12210e8 100644 --- a/server/services/store/game.go +++ b/server/services/store/game.go @@ -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) { + +// } diff --git a/server/services/store/game_test.go b/server/services/store/game_test.go index 0e67b8b..506b09a 100644 --- a/server/services/store/game_test.go +++ b/server/services/store/game_test.go @@ -15,16 +15,32 @@ func TestCreateGame(t *testing.T) { user := th.NewUser() gameName := "Awesome Game" - 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) + var game *model.Game + var gameMember *model.GameMember + var err error - gameMembers, err := th.store.Game().GetGameMembers(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 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) + }) + + 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) + }) }