mirror of
https://github.com/IS1DI/x0gp.git
synced 2026-07-19 05:47:02 +00:00
Add races results GET endpoints (by ID and last finished) and regenerate swagger specs
This commit is contained in:
@@ -308,3 +308,88 @@ func TestLeaderboard(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetRaceAndLastFinished(t *testing.T) {
|
||||
dsn := os.Getenv("DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("DATABASE_URL is not set, skipping database integration tests")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to connect to test database: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
store := NewPgStore(pool)
|
||||
|
||||
cleanup := func() {
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM race_drivers WHERE race_id LIKE 'test-get-race-%'")
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM races WHERE id LIKE 'test-get-race-%'")
|
||||
}
|
||||
cleanup()
|
||||
defer cleanup()
|
||||
|
||||
// 1. Insert a finished race
|
||||
now := time.Now().UnixMilli()
|
||||
race := FinishedRace{
|
||||
ID: "test-get-race-1",
|
||||
Name: "Test Get Race 1",
|
||||
TrackID: "monaco",
|
||||
MaxCars: 4,
|
||||
Laps: 5,
|
||||
TimeLimitS: 300,
|
||||
Status: "finished",
|
||||
CreatedMs: now - 10000,
|
||||
StartedMs: now - 8000,
|
||||
FinishedMs: now - 5000,
|
||||
DurationMs: 3000,
|
||||
TotalLaps: 20,
|
||||
TotalDrivers: 1,
|
||||
}
|
||||
err = store.InsertFinished(ctx, race)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to insert finished race: %v", err)
|
||||
}
|
||||
|
||||
// 2. Fetch the race by ID
|
||||
got, err := store.GetFinished(ctx, "test-get-race-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetFinished failed: %v", err)
|
||||
}
|
||||
if got.ID != "test-get-race-1" || got.Name != "Test Get Race 1" {
|
||||
t.Errorf("expected test-get-race-1, got: %+v", got)
|
||||
}
|
||||
|
||||
// 3. Insert a second finished race (newer finished_ms)
|
||||
race2 := FinishedRace{
|
||||
ID: "test-get-race-2",
|
||||
Name: "Test Get Race 2",
|
||||
TrackID: "monaco",
|
||||
MaxCars: 4,
|
||||
Laps: 5,
|
||||
TimeLimitS: 300,
|
||||
Status: "finished",
|
||||
CreatedMs: now - 5000,
|
||||
StartedMs: now - 4000,
|
||||
FinishedMs: now - 1000,
|
||||
DurationMs: 3000,
|
||||
TotalLaps: 20,
|
||||
TotalDrivers: 1,
|
||||
}
|
||||
err = store.InsertFinished(ctx, race2)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to insert finished race 2: %v", err)
|
||||
}
|
||||
|
||||
// 4. Fetch the last finished race (should be test-get-race-2)
|
||||
last, err := store.GetLastFinished(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetLastFinished failed: %v", err)
|
||||
}
|
||||
if last.ID != "test-get-race-2" {
|
||||
t.Errorf("expected test-get-race-2 as last race, got: %s", last.ID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user