music-kraken/internal/data/song_test.go
2025-10-07 14:27:16 +02:00

185 lines
4.3 KiB
Go

package data
import (
"testing"
)
func TestSongDeduplicate(t *testing.T) {
song := Song{
Name: "song_a",
Album: Album{
Name: "album_a",
Artists: []Artist{
{Name: "artist_a"},
{Name: "artist_a"},
{Name: "artist_b"},
},
},
Artists: []Artist{
{Name: "artist a"},
{Name: "artist a"},
{Name: "artist_b"},
},
}
compiled := song.Compile().(Song)
if len(compiled.Artists) != 2 {
t.Errorf("expected 2 unique artists after deduplication, but got %d: %v", len(compiled.Artists), compiled.Artists)
}
if len(compiled.Album.Artists) != 2 {
t.Errorf("expected 2 unique album artists after deduplication, but got %d: %v", len(compiled.Album.Artists), compiled.Album.Artists)
}
}
func TestSongUnify(t *testing.T) {
song := Song{
Name: " foo Bar BAZ",
Album: Album{
Name: " foo",
},
}
compiled := song.Compile().(Song)
if compiled.UnifiedName != "foo bar baz" {
t.Errorf("song unified name mismatch: expected 'foo bar baz', but got '%s'", compiled.UnifiedName)
}
if compiled.Album.UnifiedName != "foo" {
t.Errorf("album unified name mismatch: expected 'foo', but got '%s'", compiled.Album.UnifiedName)
}
}
func TestAlbumDeduplicate(t *testing.T) {
album := Album{
Name: "album_a",
Songs: []Song{
{Name: "song a"},
{Name: "song a"},
{Name: "song_b"},
{Name: "song_c"},
{Name: "song_d"},
{Name: "song a"},
},
Artists: []Artist{
{Name: "artist_a"},
{
Name: "artist_a",
Albums: []Album{
{Name: "album_a"},
{Name: "album_b"},
{Name: "album_a"},
},
},
},
}
compiled := album.Compile().(Album)
if len(compiled.Songs) != 4 {
t.Errorf("expected 4 unique songs after deduplication, but got %d: %v", len(compiled.Songs), compiled.Songs)
}
if len(compiled.Artists) != 1 {
t.Errorf("expected 1 unique artist after deduplication, but got %d: %v", len(compiled.Artists), compiled.Artists)
}
if len(compiled.Artists[0].Albums) != 2 {
t.Errorf("expected artist to have 2 unique albums after deduplication, but got %d: %v", len(compiled.Artists[0].Albums), compiled.Artists[0].Albums)
}
}
func TestAlbumUnify(t *testing.T) {
album := Album{
Name: " foo Bar BAZ",
}
compiled := album.Compile().(Album)
if compiled.UnifiedName != "foo bar baz" {
t.Errorf(`the song name doesn't get compiled properly`)
}
}
func TestArtistDeduplicate(t *testing.T) {
artist := Artist{
Name: "main_artist",
Albums: []Album{
{Name: "concept_album"},
{
Name: "concept_album",
Artists: []Artist{
{
Name: "main_artist",
Albums: []Album{
{Name: "concept_album", Songs: []Song{{Name: "song_x"}, {Name: "song_x"}}},
{Name: "other_album"},
},
},
{Name: "featured_artist"},
{Name: "featured_artist"},
},
Songs: []Song{
{
Name: "complex_song",
Artists: []Artist{
{Name: "main_artist"},
{Name: "featured_artist"},
{Name: "main_artist"},
},
},
},
},
},
}
compiled := artist.Compile().(Artist)
if len(compiled.Albums) != 1 {
t.Errorf("should deduplicate album artists, got %d", len(compiled.Albums[0].Artists))
}
if len(compiled.Albums[0].Artists) != 2 {
t.Errorf("should deduplicate album artists, got %d", len(compiled.Albums[0].Artists))
}
if len(compiled.Albums[0].Artists[0].Albums) != 2 {
t.Errorf("should deduplicate nested albums, got %d", len(compiled.Albums[0].Artists[0].Albums))
}
if len(compiled.Albums[0].Songs[0].Artists) != 2 {
t.Errorf("should deduplicate song artists, got %d", len(compiled.Albums[0].Songs[0].Artists))
}
}
func TestArtistUnify(t *testing.T) {
artist := Artist{
Name: " The BEATLES ",
Albums: []Album{
{
Name: " Abbey Road ",
Songs: []Song{
{Name: "Come Together "},
{Name: "Something"},
},
},
},
}
compiled := artist.Compile().(Artist)
if compiled.UnifiedName != "the beatles" {
t.Errorf("artist name not unified properly, got: %s", compiled.UnifiedName)
}
if compiled.Albums[0].UnifiedName != "abbey road" {
t.Errorf("album name in artist not unified properly, got: %s", compiled.Albums[0].UnifiedName)
}
if compiled.Albums[0].Songs[0].UnifiedName != "come together" {
t.Errorf("song name in artist album not unified properly, got: %s", compiled.Albums[0].Songs[0].UnifiedName)
}
}