Fixed some small bugs after merge

This commit is contained in:
2026-06-14 11:30:58 +02:00
parent 0894d65ec5
commit 4e5bdc4ee2
21 changed files with 1460 additions and 186 deletions
+35 -37
View File
@@ -9,10 +9,10 @@ import (
// Test the average calculation logic directly without database access
func TestCalculateAverage(t *testing.T) {
games := []repository.Game{
{GameName: "Game1", TimesPlayed: 10},
{GameName: "Game2", TimesPlayed: 20},
{GameName: "Game3", TimesPlayed: 30},
games := []repository.Soundtrack{
{SoundtrackName: "Game1", TimesPlayed: 10},
{SoundtrackName: "Game2", TimesPlayed: 20},
{SoundtrackName: "Game3", TimesPlayed: 30},
}
var sum int32
@@ -28,7 +28,7 @@ func TestCalculateAverage(t *testing.T) {
}
func TestCalculateAverageEmpty(t *testing.T) {
games := []repository.Game{}
games := []repository.Soundtrack{}
if len(games) == 0 {
result := int32(0)
@@ -52,8 +52,8 @@ func TestCalculateAverageEmpty(t *testing.T) {
}
func TestCalculateAverageSingle(t *testing.T) {
games := []repository.Game{
{GameName: "Game1", TimesPlayed: 42},
games := []repository.Soundtrack{
{SoundtrackName: "Game1", TimesPlayed: 42},
}
var sum int32
@@ -69,10 +69,10 @@ func TestCalculateAverageSingle(t *testing.T) {
}
func TestGetRandomGame(t *testing.T) {
games := []repository.Game{
{GameName: "Game1", TimesPlayed: 10},
{GameName: "Game2", TimesPlayed: 20},
{GameName: "Game3", TimesPlayed: 30},
games := []repository.Soundtrack{
{SoundtrackName: "Game1", TimesPlayed: 10},
{SoundtrackName: "Game2", TimesPlayed: 20},
{SoundtrackName: "Game3", TimesPlayed: 30},
}
// Set seed for reproducible tests
@@ -80,93 +80,93 @@ func TestGetRandomGame(t *testing.T) {
result := games[rand.Intn(len(games))]
if result.GameName == "" {
if result.SoundtrackName == "" {
t.Error("random game selection returned empty game")
}
found := false
for _, g := range games {
if g.GameName == result.GameName {
if g.SoundtrackName == result.SoundtrackName {
found = true
break
}
}
if !found {
t.Errorf("random game selection returned game not in list: %v", result.GameName)
t.Errorf("random game selection returned game not in list: %v", result.SoundtrackName)
}
}
func TestFindGameByID(t *testing.T) {
games := []repository.Game{
{ID: 1, GameName: "Game1", TimesPlayed: 10},
{ID: 2, GameName: "Game2", TimesPlayed: 20},
{ID: 3, GameName: "Game3", TimesPlayed: 30},
games := []repository.Soundtrack{
{ID: 1, SoundtrackName: "Game1", TimesPlayed: 10},
{ID: 2, SoundtrackName: "Game2", TimesPlayed: 20},
{ID: 3, SoundtrackName: "Game3", TimesPlayed: 30},
}
tests := []struct {
name string
games []repository.Game
games []repository.Soundtrack
gameID int32
expected repository.Game
expected repository.Soundtrack
}{
{
name: "existing game",
games: games,
gameID: 2,
expected: repository.Game{ID: 2, GameName: "Game2", TimesPlayed: 20},
expected: repository.Soundtrack{ID: 2, SoundtrackName: "Game2", TimesPlayed: 20},
},
{
name: "non-existing game",
games: games,
gameID: 99,
expected: repository.Game{},
expected: repository.Soundtrack{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result repository.Game
var result repository.Soundtrack
for _, game := range tt.games {
if game.ID == tt.gameID {
result = game
break
}
}
if result.ID != tt.expected.ID || result.GameName != tt.expected.GameName {
if result.ID != tt.expected.ID || result.SoundtrackName != tt.expected.SoundtrackName {
t.Errorf("findGameByID() = %v, want %v", result, tt.expected)
}
})
}
}
func TestExtractGameNames(t *testing.T) {
games := []repository.Game{
{GameName: "Game1", TimesPlayed: 10},
{GameName: "Game2", TimesPlayed: 20},
{GameName: "Game3", TimesPlayed: 30},
func TestExtractSoundtrackNames(t *testing.T) {
games := []repository.Soundtrack{
{SoundtrackName: "Game1", TimesPlayed: 10},
{SoundtrackName: "Game2", TimesPlayed: 20},
{SoundtrackName: "Game3", TimesPlayed: 30},
}
var result []string
for _, game := range games {
result = append(result, game.GameName)
result = append(result, game.SoundtrackName)
}
expected := []string{"Game1", "Game2", "Game3"}
if len(result) != len(expected) {
t.Errorf("extractGameNames() length = %d, want %d", len(result), len(expected))
t.Errorf("extractSoundtrackNames() length = %d, want %d", len(result), len(expected))
return
}
for i, v := range result {
if v != expected[i] {
t.Errorf("extractGameNames()[%d] = %v, want %v", i, v, expected[i])
t.Errorf("extractSoundtrackNames()[%d] = %v, want %v", i, v, expected[i])
}
}
}
func TestShuffleGameNames(t *testing.T) {
func TestShuffleSoundtrackNames(t *testing.T) {
games := []string{"Game1", "Game2", "Game3"}
// Test that shuffle doesn't lose any elements
@@ -181,7 +181,7 @@ func TestShuffleGameNames(t *testing.T) {
}
if len(games) != len(original) {
t.Errorf("shuffleGameNames() changed length from %d to %d", len(original), len(games))
t.Errorf("shuffleSoundtrackNames() changed length from %d to %d", len(original), len(games))
return
}
@@ -195,9 +195,7 @@ func TestShuffleGameNames(t *testing.T) {
}
}
if !found {
t.Errorf("shuffleGameNames() lost element: %v", orig)
t.Errorf("shuffleSoundtrackNames() lost element: %v", orig)
}
}
}