202 lines
4.6 KiB
Go
202 lines
4.6 KiB
Go
package backend
|
|
|
|
import (
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"music-server/internal/db/repository"
|
|
)
|
|
|
|
// 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},
|
|
}
|
|
|
|
var sum int32
|
|
for _, data := range games {
|
|
sum += data.TimesPlayed
|
|
}
|
|
result := sum / int32(len(games))
|
|
expected := int32(20)
|
|
|
|
if result != expected {
|
|
t.Errorf("Average calculation = %v, want %v", result, expected)
|
|
}
|
|
}
|
|
|
|
func TestCalculateAverageEmpty(t *testing.T) {
|
|
games := []repository.Soundtrack{}
|
|
|
|
if len(games) == 0 {
|
|
result := int32(0)
|
|
expected := int32(0)
|
|
if result != expected {
|
|
t.Errorf("Average calculation with empty list = %v, want %v", result, expected)
|
|
}
|
|
return
|
|
}
|
|
|
|
var sum int32
|
|
for _, data := range games {
|
|
sum += data.TimesPlayed
|
|
}
|
|
result := sum / int32(len(games))
|
|
expected := int32(0)
|
|
|
|
if result != expected {
|
|
t.Errorf("Average calculation with empty list = %v, want %v", result, expected)
|
|
}
|
|
}
|
|
|
|
func TestCalculateAverageSingle(t *testing.T) {
|
|
games := []repository.Soundtrack{
|
|
{SoundtrackName: "Game1", TimesPlayed: 42},
|
|
}
|
|
|
|
var sum int32
|
|
for _, data := range games {
|
|
sum += data.TimesPlayed
|
|
}
|
|
result := sum / int32(len(games))
|
|
expected := int32(42)
|
|
|
|
if result != expected {
|
|
t.Errorf("Average calculation with single game = %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},
|
|
}
|
|
|
|
// Set seed for reproducible tests
|
|
rand.Seed(42)
|
|
|
|
result := games[rand.Intn(len(games))]
|
|
|
|
if result.SoundtrackName == "" {
|
|
t.Error("random game selection returned empty game")
|
|
}
|
|
|
|
found := false
|
|
for _, g := range games {
|
|
if g.SoundtrackName == result.SoundtrackName {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
t.Errorf("random game selection returned game 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},
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
games []repository.Soundtrack
|
|
gameID int32
|
|
expected repository.Soundtrack
|
|
}{
|
|
{
|
|
name: "existing game",
|
|
games: games,
|
|
gameID: 2,
|
|
expected: repository.Soundtrack{ID: 2, SoundtrackName: "Game2", TimesPlayed: 20},
|
|
},
|
|
{
|
|
name: "non-existing game",
|
|
games: games,
|
|
gameID: 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
|
|
break
|
|
}
|
|
}
|
|
if result.ID != tt.expected.ID || result.SoundtrackName != tt.expected.SoundtrackName {
|
|
t.Errorf("findGameByID() = %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},
|
|
}
|
|
|
|
var result []string
|
|
for _, game := range games {
|
|
result = append(result, game.SoundtrackName)
|
|
}
|
|
|
|
expected := []string{"Game1", "Game2", "Game3"}
|
|
|
|
if 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("extractSoundtrackNames()[%d] = %v, want %v", i, v, expected[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestShuffleSoundtrackNames(t *testing.T) {
|
|
games := []string{"Game1", "Game2", "Game3"}
|
|
|
|
// 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)
|
|
|
|
// Simple shuffle implementation for testing
|
|
for i := range games {
|
|
j := i // In real code this would be random
|
|
games[i], games[j] = games[j], games[i]
|
|
}
|
|
|
|
if len(games) != len(original) {
|
|
t.Errorf("shuffleSoundtrackNames() changed length from %d to %d", len(original), len(games))
|
|
return
|
|
}
|
|
|
|
// Check all original elements are still present
|
|
for _, orig := range original {
|
|
found := false
|
|
for _, g := range games {
|
|
if g == orig {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("shuffleSoundtrackNames() lost element: %v", orig)
|
|
}
|
|
}
|
|
}
|