Fix: test; limit changes on player update

This commit is contained in:
Grail Finder
2025-07-04 10:02:39 +03:00
parent 6ca8afd13d
commit 83215f5c14
9 changed files with 40 additions and 92 deletions

View File

@ -3,7 +3,6 @@ package repos
import (
"context"
"gralias/models"
"time"
"github.com/jmoiron/sqlx"
)
@ -21,9 +20,6 @@ func (p *RepoProvider) ListActions(ctx context.Context, roomID string) ([]models
if err != nil {
return nil, err
}
for i := range actions {
actions[i].CreatedAt = time.Unix(0, actions[i].CreatedAtUnix)
}
return actions, nil
}
@ -39,7 +35,6 @@ func (p *RepoProvider) GetLastClue(ctx context.Context, roomID string) (*models.
if err != nil {
return nil, err
}
action.CreatedAt = time.Unix(0, action.CreatedAtUnix)
return action, nil
}

View File

@ -7,8 +7,8 @@ import (
"time"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)
func setupActionsTestDB(t *testing.T) (*sqlx.DB, func()) {
@ -24,7 +24,7 @@ func setupActionsTestDB(t *testing.T) (*sqlx.DB, func()) {
word TEXT,
word_color TEXT,
number_associated TEXT,
created_at INTEGER
created_at TIMESTAMP
);
`
_, err = db.Exec(schema)
@ -87,9 +87,9 @@ func TestActionsRepo_ListActions(t *testing.T) {
CreatedAt: time.Now().Add(-1 * time.Second),
}
_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt.UnixNano())
_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt)
assert.NoError(t, err)
_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action2.Actor, action2.ActorColor, action2.Action, action2.Word, action2.WordColor, action2.Number, action2.CreatedAt.UnixNano())
_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action2.Actor, action2.ActorColor, action2.Action, action2.Word, action2.WordColor, action2.Number, action2.CreatedAt)
assert.NoError(t, err)
actions, err := repo.ListActions(context.Background(), roomID)
@ -135,11 +135,11 @@ func TestActionsRepo_GetLastClue(t *testing.T) {
CreatedAt: time.Now().Add(-1 * time.Second),
}
_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt.UnixNano())
_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt)
assert.NoError(t, err)
_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action2.Actor, action2.ActorColor, action2.Action, action2.Word, action2.WordColor, action2.Number, action2.CreatedAt.UnixNano())
_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action2.Actor, action2.ActorColor, action2.Action, action2.Word, action2.WordColor, action2.Number, action2.CreatedAt)
assert.NoError(t, err)
_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action3.Actor, action3.ActorColor, action3.Action, action3.Word, action3.WordColor, action3.Number, action3.CreatedAt.UnixNano())
_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action3.Actor, action3.ActorColor, action3.Action, action3.Word, action3.WordColor, action3.Number, action3.CreatedAt)
assert.NoError(t, err)
lastClue, err := repo.GetLastClue(context.Background(), roomID)
@ -164,7 +164,7 @@ func TestActionsRepo_DeleteActionsByRoomID(t *testing.T) {
Number: "3",
CreatedAt: time.Now(),
}
_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt.UnixNano())
_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt)
assert.NoError(t, err)
err = repo.DeleteActionsByRoomID(context.Background(), roomID)
@ -174,4 +174,5 @@ func TestActionsRepo_DeleteActionsByRoomID(t *testing.T) {
err = db.Get(&count, "SELECT COUNT(*) FROM actions WHERE room_id = ?", roomID)
assert.NoError(t, err)
assert.Equal(t, 0, count)
}
}

View File

@ -48,8 +48,8 @@ func (p *RepoProvider) PlayerAdd(ctx context.Context, player *models.Player) err
func (p *RepoProvider) PlayerUpdate(ctx context.Context, player *models.Player) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, "UPDATE players SET room_id = ?, username = ?, team = ?, role = ?, is_bot = ? WHERE id = ?",
player.RoomID, player.Username, player.Team, player.Role, player.IsBot, player.ID)
_, err := db.ExecContext(ctx, "UPDATE players SET team = ?, role = ? WHERE username = ?",
player.Team, player.Role, player.Username)
return err
}
@ -67,7 +67,7 @@ func (p *RepoProvider) PlayerSetRoomID(ctx context.Context, username, roomID str
func (p *RepoProvider) PlayerExitRoom(ctx context.Context, username string) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, "UPDATE players SET room_id='', team='', role='' WHERE username = ?", username)
_, err := db.ExecContext(ctx, "UPDATE players SET room_id=null, team='', role='' WHERE username = ?", username)
return err
}

View File

@ -2,6 +2,7 @@ package repos
import (
"context"
"fmt"
"gralias/models"
"github.com/jmoiron/sqlx"
@ -70,12 +71,14 @@ func (p *RepoProvider) RoomGetExtended(ctx context.Context, id string) (*models.
room := &models.Room{}
err := sqlx.GetContext(ctx, p.DB, room, `SELECT * FROM rooms WHERE id = ?`, id)
if err != nil {
err = fmt.Errorf("failed to get room; %w", err)
return nil, err
}
// Get players
players := []*models.Player{}
err = sqlx.SelectContext(ctx, p.DB, &players, `SELECT * FROM players WHERE room_id = ?`, id)
if err != nil {
err = fmt.Errorf("failed to get players; %w", err)
return nil, err
}
room.RedTeam.Color = string(models.UserTeamRed)
@ -108,6 +111,7 @@ func (p *RepoProvider) RoomGetExtended(ctx context.Context, id string) (*models.
wordCards := []models.WordCard{}
err = sqlx.SelectContext(ctx, p.DB, &wordCards, `SELECT * FROM word_cards WHERE room_id = ?`, id)
if err != nil {
err = fmt.Errorf("failed to get cards; %w", err)
return nil, err
}
room.Cards = wordCards
@ -115,6 +119,7 @@ func (p *RepoProvider) RoomGetExtended(ctx context.Context, id string) (*models.
actions := []models.Action{}
err = sqlx.SelectContext(ctx, p.DB, &actions, `SELECT * FROM actions WHERE room_id = ? ORDER BY created_at ASC`, id)
if err != nil {
err = fmt.Errorf("failed to get actions; %w", err)
return nil, err
}
room.ActionHistory = actions
@ -122,6 +127,7 @@ func (p *RepoProvider) RoomGetExtended(ctx context.Context, id string) (*models.
settings := &models.GameSettings{}
err = sqlx.GetContext(ctx, p.DB, settings, `SELECT * FROM settings WHERE room_id = ?`, id)
if err != nil {
err = fmt.Errorf("failed to get settings; %w", err)
return nil, err
}
room.Settings = *settings