This commit is contained in:
@@ -9,17 +9,17 @@ import (
|
||||
|
||||
// Test the average calculation logic directly without database access
|
||||
func TestCalculateAverage(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{SoundtrackName: "Game1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Game3", TimesPlayed: 30},
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Soundtrack1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Soundtrack3", TimesPlayed: 30},
|
||||
}
|
||||
|
||||
var sum int32
|
||||
for _, data := range games {
|
||||
for _, data := range soundtracks {
|
||||
sum += data.TimesPlayed
|
||||
}
|
||||
result := sum / int32(len(games))
|
||||
result := sum / int32(len(soundtracks))
|
||||
expected := int32(20)
|
||||
|
||||
if result != expected {
|
||||
@@ -28,9 +28,9 @@ func TestCalculateAverage(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCalculateAverageEmpty(t *testing.T) {
|
||||
games := []repository.Soundtrack{}
|
||||
soundtracks := []repository.Soundtrack{}
|
||||
|
||||
if len(games) == 0 {
|
||||
if len(soundtracks) == 0 {
|
||||
result := int32(0)
|
||||
expected := int32(0)
|
||||
if result != expected {
|
||||
@@ -40,10 +40,10 @@ func TestCalculateAverageEmpty(t *testing.T) {
|
||||
}
|
||||
|
||||
var sum int32
|
||||
for _, data := range games {
|
||||
for _, data := range soundtracks {
|
||||
sum += data.TimesPlayed
|
||||
}
|
||||
result := sum / int32(len(games))
|
||||
result := sum / int32(len(soundtracks))
|
||||
expected := int32(0)
|
||||
|
||||
if result != expected {
|
||||
@@ -52,107 +52,107 @@ func TestCalculateAverageEmpty(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCalculateAverageSingle(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{SoundtrackName: "Game1", TimesPlayed: 42},
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Soundtrack1", TimesPlayed: 42},
|
||||
}
|
||||
|
||||
var sum int32
|
||||
for _, data := range games {
|
||||
for _, data := range soundtracks {
|
||||
sum += data.TimesPlayed
|
||||
}
|
||||
result := sum / int32(len(games))
|
||||
result := sum / int32(len(soundtracks))
|
||||
expected := int32(42)
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("Average calculation with single game = %v, want %v", result, expected)
|
||||
t.Errorf("Average calculation with single soundtrack = %v, want %v", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandomGame(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{SoundtrackName: "Game1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Game3", TimesPlayed: 30},
|
||||
func TestGetRandomSoundtrack(t *testing.T) {
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Soundtrack1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Soundtrack3", TimesPlayed: 30},
|
||||
}
|
||||
|
||||
// Set seed for reproducible tests
|
||||
rand.Seed(42)
|
||||
|
||||
result := games[rand.Intn(len(games))]
|
||||
result := soundtracks[rand.Intn(len(soundtracks))]
|
||||
|
||||
if result.SoundtrackName == "" {
|
||||
t.Error("random game selection returned empty game")
|
||||
t.Error("random soundtrack selection returned empty soundtrack")
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, g := range games {
|
||||
if g.SoundtrackName == result.SoundtrackName {
|
||||
for _, s := range soundtracks {
|
||||
if s.SoundtrackName == result.SoundtrackName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Errorf("random game selection returned game not in list: %v", result.SoundtrackName)
|
||||
t.Errorf("random soundtrack selection returned soundtrack not in list: %v", result.SoundtrackName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindGameByID(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{ID: 1, SoundtrackName: "Game1", TimesPlayed: 10},
|
||||
{ID: 2, SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
{ID: 3, SoundtrackName: "Game3", TimesPlayed: 30},
|
||||
func TestFindSoundtrackByID(t *testing.T) {
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{ID: 1, SoundtrackName: "Soundtrack1", TimesPlayed: 10},
|
||||
{ID: 2, SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
{ID: 3, SoundtrackName: "Soundtrack3", TimesPlayed: 30},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
games []repository.Soundtrack
|
||||
gameID int32
|
||||
expected repository.Soundtrack
|
||||
name string
|
||||
soundtracks []repository.Soundtrack
|
||||
soundtrackID int32
|
||||
expected repository.Soundtrack
|
||||
}{
|
||||
{
|
||||
name: "existing game",
|
||||
games: games,
|
||||
gameID: 2,
|
||||
expected: repository.Soundtrack{ID: 2, SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
name: "existing soundtrack",
|
||||
soundtracks: soundtracks,
|
||||
soundtrackID: 2,
|
||||
expected: repository.Soundtrack{ID: 2, SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
},
|
||||
{
|
||||
name: "non-existing game",
|
||||
games: games,
|
||||
gameID: 99,
|
||||
expected: repository.Soundtrack{},
|
||||
name: "non-existing soundtrack",
|
||||
soundtracks: soundtracks,
|
||||
soundtrackID: 99,
|
||||
expected: repository.Soundtrack{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var result repository.Soundtrack
|
||||
for _, game := range tt.games {
|
||||
if game.ID == tt.gameID {
|
||||
result = game
|
||||
for _, s := range tt.soundtracks {
|
||||
if s.ID == tt.soundtrackID {
|
||||
result = s
|
||||
break
|
||||
}
|
||||
}
|
||||
if result.ID != tt.expected.ID || result.SoundtrackName != tt.expected.SoundtrackName {
|
||||
t.Errorf("findGameByID() = %v, want %v", result, tt.expected)
|
||||
t.Errorf("findSoundtrackByID() = %v, want %v", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractSoundtrackNames(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{SoundtrackName: "Game1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Game3", TimesPlayed: 30},
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Soundtrack1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Soundtrack3", TimesPlayed: 30},
|
||||
}
|
||||
|
||||
var result []string
|
||||
for _, game := range games {
|
||||
result = append(result, game.SoundtrackName)
|
||||
for _, s := range soundtracks {
|
||||
result = append(result, s.SoundtrackName)
|
||||
}
|
||||
|
||||
expected := []string{"Game1", "Game2", "Game3"}
|
||||
expected := []string{"Soundtrack1", "Soundtrack2", "Soundtrack3"}
|
||||
|
||||
if len(result) != len(expected) {
|
||||
t.Errorf("extractSoundtrackNames() length = %d, want %d", len(result), len(expected))
|
||||
@@ -167,29 +167,29 @@ func TestExtractSoundtrackNames(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestShuffleSoundtrackNames(t *testing.T) {
|
||||
games := []string{"Game1", "Game2", "Game3"}
|
||||
soundtracks := []string{"Soundtrack1", "Soundtrack2", "Soundtrack3"}
|
||||
|
||||
// Test that shuffle doesn't lose any elements
|
||||
// We can't test the order since it's random, but we can test length and contents
|
||||
original := make([]string, len(games))
|
||||
copy(original, games)
|
||||
original := make([]string, len(soundtracks))
|
||||
copy(original, soundtracks)
|
||||
|
||||
// Simple shuffle implementation for testing
|
||||
for i := range games {
|
||||
for i := range soundtracks {
|
||||
j := i // In real code this would be random
|
||||
games[i], games[j] = games[j], games[i]
|
||||
soundtracks[i], soundtracks[j] = soundtracks[j], soundtracks[i]
|
||||
}
|
||||
|
||||
if len(games) != len(original) {
|
||||
t.Errorf("shuffleSoundtrackNames() changed length from %d to %d", len(original), len(games))
|
||||
if len(soundtracks) != len(original) {
|
||||
t.Errorf("shuffleSoundtrackNames() changed length from %d to %d", len(original), len(soundtracks))
|
||||
return
|
||||
}
|
||||
|
||||
// Check all original elements are still present
|
||||
for _, orig := range original {
|
||||
found := false
|
||||
for _, g := range games {
|
||||
if g == orig {
|
||||
for _, s := range soundtracks {
|
||||
if s == orig {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user