From 70d0eb8334b787cddcac83ff8a1e2a5114c64ad6 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 13 Sep 2021 21:58:52 +0200 Subject: [PATCH] Add watch-test target and test around game creation --- Makefile | 4 ++ modd-test.conf | 8 ++++ server/services/store/game.go | 62 +++++++++++++++++++++++++----- server/services/store/game_test.go | 30 +++++++++++++++ server/services/store/store.go | 4 +- server/services/store/testlib.go | 1 + 6 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 modd-test.conf create mode 100644 server/services/store/game_test.go diff --git a/Makefile b/Makefile index 24aadca..64112c6 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,10 @@ mod: test: cd server && go test -v -race -count 1 ./... +.PHONY: watch-test +watch-test: + modd -f modd-test.conf + .PHONY: watch watch: modd diff --git a/modd-test.conf b/modd-test.conf new file mode 100644 index 0000000..9304e35 --- /dev/null +++ b/modd-test.conf @@ -0,0 +1,8 @@ +{ + prep: echo "Starting tests in watch mode" +} + +server/**/*.go { + indir: server + prep: go test -v -race ./... +} \ No newline at end of file diff --git a/server/services/store/game.go b/server/services/store/game.go index 9fcf80f..6c669ca 100644 --- a/server/services/store/game.go +++ b/server/services/store/game.go @@ -2,6 +2,7 @@ package store import ( "database/sql" + "fmt" sq "github.com/Masterminds/squirrel" @@ -54,18 +55,18 @@ func (gs *GameStore) getGameByCondition(condition sq.Eq) (*model.Game, error) { } func (gs *GameStore) getGamesByCondition(condition sq.Eq) ([]*model.Game, error) { - rows, err := gs.Q().Select(userColumns...). + rows, err := gs.Q().Select(gameColumns...). From("games"). Where(condition). Query() if err != nil { - return nil, err + return nil, fmt.Errorf("cannot get games: %w", err) } defer rows.Close() games, err := gs.gamesFromRows(rows) if err != nil { - return nil, err + return nil, fmt.Errorf("cannot get games from rows: %w", err) } if len(games) == 0 { @@ -85,29 +86,72 @@ func (gs *GameStore) Create(name string, userID int) (*model.Game, error) { res, err := gs.Q().Insert("games"). Columns(gameColumns[1:]...). - Values(name, userID). + Values(userID, name). Exec() if err != nil { - return nil, err + return nil, fmt.Errorf("cannot insert game: %w", err) } id, err := res.LastInsertId() if err != nil { - return nil, err + return nil, fmt.Errorf("cannot get last insert id for created game: %w", err) } game, err := gs.GetByID(int(id)) if err != nil { - return nil, err + return nil, fmt.Errorf("cannot get game by id: %w", err) } - res, err = gs.Q().Insert("gamemember"). + res, err = gs.Q().Insert("gamemembers"). Columns(gameMemberColumns...). Values(game.ID, userID, model.RoleGameMaster). Exec() if err != nil { - return nil, err + return nil, fmt.Errorf("cannot insert gamemember for created game: %w", err) } return game, nil } + +func (gs *GameStore) gameMembersFromRows(rows *sql.Rows) ([]*model.GameMember, error) { + gameMembers := []*model.GameMember{} + + for rows.Next() { + var gameMember model.GameMember + + err := rows.Scan( + &gameMember.GameID, + &gameMember.UserID, + &gameMember.Role, + ) + if err != nil { + return nil, err + } + + gameMembers = append(gameMembers, &gameMember) + } + + return gameMembers, nil +} + +func (gs *GameStore) GetGameMembers(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) + } + defer rows.Close() + + gameMembers, err := gs.gameMembersFromRows(rows) + if err != nil { + return nil, fmt.Errorf("cannot get game members from rows: %w", err) + } + + if len(gameMembers) == 0 { + return nil, sql.ErrNoRows + } + + return gameMembers, nil +} diff --git a/server/services/store/game_test.go b/server/services/store/game_test.go new file mode 100644 index 0000000..0e67b8b --- /dev/null +++ b/server/services/store/game_test.go @@ -0,0 +1,30 @@ +package store + +import ( + "testing" + + "git.ctrlz.es/mgdelacroix/craban/model" + + "github.com/stretchr/testify/require" +) + +func TestCreateGame(t *testing.T) { + th, teardown := NewTestHelper(t) + defer teardown() + + 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) + + 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) +} diff --git a/server/services/store/store.go b/server/services/store/store.go index 5693420..5c9fa9f 100644 --- a/server/services/store/store.go +++ b/server/services/store/store.go @@ -67,13 +67,13 @@ CREATE TABLE IF NOT EXISTS users ( admin BOOLEAN NOT NULL ); -CREATE TABLE IF NOT EXISTS game ( +CREATE TABLE IF NOT EXISTS games ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, name VARCHAR(255) NOT NULL ); -CREATE TABLE IF NOT EXISTS gamemember ( +CREATE TABLE IF NOT EXISTS gamemembers ( game_id INTEGER NOT NULL, user_id INTEGER NOT NULL, role VARCHAR(255) NOT NULL, diff --git a/server/services/store/testlib.go b/server/services/store/testlib.go index 7d0286a..e4b7325 100644 --- a/server/services/store/testlib.go +++ b/server/services/store/testlib.go @@ -31,6 +31,7 @@ func (th *TestHelper) NewUser() *model.User { Mail: fake.EmailAddress(), Username: fake.UserName(), Password: fake.SimplePassword(), + Admin: false, } newUser, err := th.store.User().Create(user)