129 lines
3.1 KiB
Go
Raw Permalink Normal View History

2025-01-06 01:14:32 +01:00
package repo_test
import (
"testing"
"time"
"git.kapelle.org/niklas/beerpong-elo/internal/model"
"git.kapelle.org/niklas/beerpong-elo/internal/repo"
"github.com/stretchr/testify/assert"
)
func createRepo() repo.Repo {
return repo.NewInMemoryRepo()
}
func addGameToRepo(repo repo.Repo) model.GameID {
p1, _ := repo.GetOrCreatePlayerID("player1")
p2, _ := repo.GetOrCreatePlayerID("player2")
p3, _ := repo.GetOrCreatePlayerID("player3")
p4, _ := repo.GetOrCreatePlayerID("player4")
id, _ := repo.AddGame(model.Game{
Added: time.Date(2024, 6, 15, 12, 30, 31, 0, time.UTC),
Author: p1,
Team0Player0: p1,
Team0Player1: p2,
Team1Player0: p3,
Team1Player1: p4,
Score: 3,
Overtime: true,
})
return id
}
func TestAddGame(t *testing.T) {
repo := createRepo()
id := addGameToRepo(repo)
retGame, err := repo.GetGame(id)
assert.NoError(t, err)
assert.Equal(t, id, retGame.ID, "Returned ID and ID of retrieved game are not the same")
assert.Equal(t, 3, retGame.Score, "Score is not the same")
assert.Equal(t, true, retGame.Overtime, "Overtime is not the same")
assert.Equal(t, time.Date(2024, 6, 15, 12, 30, 31, 0, time.UTC).Unix(), retGame.Added.Unix(), "Added time is not the same")
}
func TestCreatePlayer(t *testing.T) {
repo := createRepo()
id, err := repo.GetOrCreatePlayerID("player1")
assert.NoError(t, err)
player, err := repo.GetPlayer(id)
assert.NoError(t, err)
assert.NotNil(t, player)
}
func TestPlayerNotFound(t *testing.T) {
repo := createRepo()
result, err := repo.GetPlayer("non existing")
assert.NoError(t, err)
assert.Nil(t, result)
}
func TestAddPlayer(t *testing.T) {
repo := createRepo()
gameID := addGameToRepo(repo)
retGame, err := repo.GetGame(gameID)
assert.NoError(t, err)
result, err := repo.GetPlayer(retGame.Team0Player0)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, retGame.Team0Player0, result.ID)
result, err = repo.GetPlayer(retGame.Team0Player1)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, retGame.Team0Player1, result.ID)
result, err = repo.GetPlayer(retGame.Team1Player0)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, retGame.Team1Player0, result.ID)
result, err = repo.GetPlayer(retGame.Team1Player1)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, retGame.Team1Player1, result.ID)
result, err = repo.GetPlayer(retGame.Author)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, retGame.Team0Player0, result.ID)
}
func TestAddResult(t *testing.T) {
repo := createRepo()
gameID := addGameToRepo(repo)
retGame, err := repo.GetGame(gameID)
assert.NoError(t, err)
gameResult := model.GameResult{
Game: gameID,
Player: retGame.Team0Player0,
}
gameResultID, err := repo.AddGameResult(gameResult)
assert.NoError(t, err)
retResult, err := repo.GetGameResult(gameResultID)
assert.NoError(t, err)
assert.NotNil(t, retResult)
assert.Equal(t, retGame.Team0Player0, retResult.Player, "PlayerID is not the same as the one used in the game")
assert.Equal(t, gameID, retResult.Game, "GameID is not the same as the added Game")
}