generated from VLADIMIR/template
fix
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package game_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"evening_detective_server/internal/modules/storytelling"
|
||||
"evening_detective_server/internal/repos/games_repo"
|
||||
"evening_detective_server/internal/repos/teams_repo"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func TestFinishedTeamStoryPostgres(t *testing.T) {
|
||||
dsn := os.Getenv("TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("set TEST_DATABASE_URL to run PostgreSQL integration check")
|
||||
}
|
||||
ctx := context.Background()
|
||||
config, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config.MaxConns = 1
|
||||
pool, err := pgxpool.NewWithConfig(ctx, config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
_, err = pool.Exec(ctx, `
|
||||
CREATE TEMP TABLE games (id INT, name TEXT, description TEXT, start_at TIMESTAMP,
|
||||
format TEXT, status TEXT, scenario_id INT, started_at TIMESTAMP, ended_at TIMESTAMP);
|
||||
CREATE TEMP TABLE teams (id INT, name TEXT, captain_email TEXT, participants_count INT,
|
||||
status TEXT, password TEXT, game_id INT);
|
||||
INSERT INTO games VALUES (1, 'Game', '', NOW(), 'online', 'finish', 1, NULL, NULL);
|
||||
INSERT INTO teams VALUES (1, 'Team', '', 1, 'register', 'secret', 1);`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// No action/scenario repositories: finished games must not load either.
|
||||
s := &GameService{gamesRepo: games_repo.NewGamesRepo(pool), teamsRepo: teams_repo.NewTeamsRepo(pool)}
|
||||
for _, format := range []string{"online", "offline"} {
|
||||
if _, err := pool.Exec(ctx, `UPDATE games SET format = $1`, format); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
story, game, actions, err := s.GetTeamActions(ctx, 1, "secret")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(story, &storytelling.Story{}) || len(actions) != 0 || game.Status != "finish" {
|
||||
t.Fatalf("finished game exposed content or lost status: story=%+v game=%+v actions=%+v", story, game, actions)
|
||||
}
|
||||
}
|
||||
if _, _, _, err := s.GetTeamActions(ctx, 1, "wrong"); !errors.Is(err, teams_repo.ErrTeamNotFound) {
|
||||
t.Fatalf("password check bypassed: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -258,6 +258,9 @@ func (s *GameService) GetTeamActions(ctx context.Context, teamId int, password s
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
if game.Status == "finish" {
|
||||
return &storytelling.Story{}, mapGame(game, s.domain), nil, nil
|
||||
}
|
||||
|
||||
actions, err := s.actionsRepo.GetActionsByTeamID(ctx, teamId)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user