Add watch-test target and test around game creation
This commit is contained in:
parent
9c79f08c96
commit
70d0eb8334
6 changed files with 98 additions and 11 deletions
4
Makefile
4
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
|
||||
|
|
8
modd-test.conf
Normal file
8
modd-test.conf
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
prep: echo "Starting tests in watch mode"
|
||||
}
|
||||
|
||||
server/**/*.go {
|
||||
indir: server
|
||||
prep: go test -v -race ./...
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
30
server/services/store/game_test.go
Normal file
30
server/services/store/game_test.go
Normal file
|
@ -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)
|
||||
}
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue