- Labels: changed from []Label to []LabelOrString (API returns both objects and strings) - Trailer: changed from *TrailerInfo to *AlbumTrailerStatus (Python has AlbumTrailerStatus with only 'available' field) - Added LabelOrString type with custom UnmarshalJSON - Added AlbumTrailerStatus type with Available field
337 lines
10 KiB
Go
337 lines
10 KiB
Go
package models
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestAlbum_DeJSON(t *testing.T) {
|
|
client := newMockClient()
|
|
data := []byte(`{
|
|
"id": "123",
|
|
"title": "Test Album",
|
|
"cover_uri": "https://example.com/cover.jpg",
|
|
"genre": "rock",
|
|
"year": 2023,
|
|
"track_count": 10,
|
|
"available": true,
|
|
"release_date": "2023-01-15",
|
|
"description": "Test album description",
|
|
"region": "RU",
|
|
"available_for_premium_users": true,
|
|
"available_for_premium_users_only": false,
|
|
"content_warning": "explicit",
|
|
"label": "Test Label",
|
|
"catalog_number": "CAT-123",
|
|
"options": ["option1", "option2"],
|
|
"artists": [{"id": "1", "name": "Artist 1", "cover_uri": "https://example.com/artist.jpg"}],
|
|
"labels": [{"id": "label1", "name": "Label 1"}],
|
|
"volumes": [[{"id": "t1", "title": "Track 1", "duration_ms": 180000}]],
|
|
"meta": {"availability": {"available_for_premium_users": true, "territories": ["RU", "US"]}},
|
|
"best": {"tracks": [{"id": "bt1", "title": "Best Track", "duration_ms": 200000}]},
|
|
"track_positions": [{"revision": 1, "timestamp": "2023-01-01T00:00:00Z", "position": 1, "from_cache": false}],
|
|
"similar_albums": [{"id": "sim1", "title": "Similar Album", "cover_uri": "https://example.com/sim.jpg", "year": 2022}],
|
|
"trailer": {"available": true}
|
|
}`)
|
|
|
|
album := &Album{BaseModel: *NewBaseModel(client)}
|
|
result, err := album.DeJSON(data, client)
|
|
if err != nil {
|
|
t.Fatalf("DeJSON failed: %v", err)
|
|
}
|
|
|
|
resultAlbum := result.(*Album)
|
|
if resultAlbum.ID != "123" {
|
|
t.Errorf("Expected ID '123', got '%s'", resultAlbum.ID)
|
|
}
|
|
if resultAlbum.Title != "Test Album" {
|
|
t.Errorf("Expected Title 'Test Album', got '%s'", resultAlbum.Title)
|
|
}
|
|
if resultAlbum.CoverURI != "https://example.com/cover.jpg" {
|
|
t.Errorf("Expected CoverURI 'https://example.com/cover.jpg', got '%s'", resultAlbum.CoverURI)
|
|
}
|
|
if resultAlbum.Genre != "rock" {
|
|
t.Errorf("Expected Genre 'rock', got '%s'", resultAlbum.Genre)
|
|
}
|
|
if resultAlbum.Year != 2023 {
|
|
t.Errorf("Expected Year 2023, got %d", resultAlbum.Year)
|
|
}
|
|
if resultAlbum.TrackCount != 10 {
|
|
t.Errorf("Expected TrackCount 10, got %d", resultAlbum.TrackCount)
|
|
}
|
|
if !resultAlbum.Available {
|
|
t.Errorf("Expected Available true, got false")
|
|
}
|
|
if resultAlbum.ReleaseDate != "2023-01-15" {
|
|
t.Errorf("Expected ReleaseDate '2023-01-15', got '%s'", resultAlbum.ReleaseDate)
|
|
}
|
|
if resultAlbum.Description != "Test album description" {
|
|
t.Errorf("Expected Description 'Test album description', got '%s'", resultAlbum.Description)
|
|
}
|
|
if resultAlbum.Region != "RU" {
|
|
t.Errorf("Expected Region 'RU', got '%s'", resultAlbum.Region)
|
|
}
|
|
if !resultAlbum.AvailableForPremiumUsers {
|
|
t.Errorf("Expected AvailableForPremiumUsers true, got false")
|
|
}
|
|
if resultAlbum.AvailableForPremiumUsersOnly {
|
|
t.Errorf("Expected AvailableForPremiumUsersOnly false, got true")
|
|
}
|
|
if resultAlbum.ContentWarning != "explicit" {
|
|
t.Errorf("Expected ContentWarning 'explicit', got '%s'", resultAlbum.ContentWarning)
|
|
}
|
|
if resultAlbum.Label != "Test Label" {
|
|
t.Errorf("Expected Label 'Test Label', got '%s'", resultAlbum.Label)
|
|
}
|
|
if resultAlbum.CatalogNumber != "CAT-123" {
|
|
t.Errorf("Expected CatalogNumber 'CAT-123', got '%s'", resultAlbum.CatalogNumber)
|
|
}
|
|
if len(resultAlbum.Options) != 2 {
|
|
t.Errorf("Expected 2 options, got %d", len(resultAlbum.Options))
|
|
}
|
|
|
|
// Test nested artists
|
|
if len(resultAlbum.Artists) != 1 {
|
|
t.Errorf("Expected 1 artist, got %d", len(resultAlbum.Artists))
|
|
}
|
|
if resultAlbum.Artists[0].Name != "Artist 1" {
|
|
t.Errorf("Expected artist name 'Artist 1', got '%s'", resultAlbum.Artists[0].Name)
|
|
}
|
|
|
|
// Test nested labels
|
|
if len(resultAlbum.Labels) != 1 {
|
|
t.Errorf("Expected 1 label, got %d", len(resultAlbum.Labels))
|
|
}
|
|
if resultAlbum.Labels[0].String() != "Label 1" {
|
|
t.Errorf("Expected label name 'Label 1', got '%s'", resultAlbum.Labels[0].String())
|
|
}
|
|
|
|
// Test volumes
|
|
if len(resultAlbum.Volumes) != 1 {
|
|
t.Errorf("Expected 1 volume, got %d", len(resultAlbum.Volumes))
|
|
}
|
|
if len(resultAlbum.Volumes[0]) != 1 {
|
|
t.Errorf("Expected 1 track in volume, got %d", len(resultAlbum.Volumes[0]))
|
|
}
|
|
if resultAlbum.Volumes[0][0].Title != "Track 1" {
|
|
t.Errorf("Expected track title 'Track 1', got '%s'", resultAlbum.Volumes[0][0].Title)
|
|
}
|
|
|
|
// Test meta
|
|
if resultAlbum.Meta == nil {
|
|
t.Fatal("Expected Meta to be non-nil")
|
|
}
|
|
if resultAlbum.Meta.Availability == nil {
|
|
t.Fatal("Expected Availability to be non-nil")
|
|
}
|
|
if !resultAlbum.Meta.Availability.AvailableForPremiumUsers {
|
|
t.Errorf("Expected AvailableForPremiumUsers true in meta")
|
|
}
|
|
if len(resultAlbum.Meta.Availability.Territories) != 2 {
|
|
t.Errorf("Expected 2 territories, got %d", len(resultAlbum.Meta.Availability.Territories))
|
|
}
|
|
|
|
// Test best
|
|
if resultAlbum.Best == nil {
|
|
t.Fatal("Expected Best to be non-nil")
|
|
}
|
|
if len(resultAlbum.Best.Tracks) != 1 {
|
|
t.Errorf("Expected 1 best track, got %d", len(resultAlbum.Best.Tracks))
|
|
}
|
|
if resultAlbum.Best.Tracks[0].Title != "Best Track" {
|
|
t.Errorf("Expected best track title 'Best Track', got '%s'", resultAlbum.Best.Tracks[0].Title)
|
|
}
|
|
|
|
// Test track positions
|
|
if len(resultAlbum.TrackPositions) != 1 {
|
|
t.Errorf("Expected 1 track position, got %d", len(resultAlbum.TrackPositions))
|
|
}
|
|
if resultAlbum.TrackPositions[0].Position != 1 {
|
|
t.Errorf("Expected position 1, got %d", resultAlbum.TrackPositions[0].Position)
|
|
}
|
|
if resultAlbum.TrackPositions[0].Revision != 1 {
|
|
t.Errorf("Expected revision 1, got %d", resultAlbum.TrackPositions[0].Revision)
|
|
}
|
|
|
|
// Test similar albums
|
|
if len(resultAlbum.SimilarAlbums) != 1 {
|
|
t.Errorf("Expected 1 similar album, got %d", len(resultAlbum.SimilarAlbums))
|
|
}
|
|
if resultAlbum.SimilarAlbums[0].Title != "Similar Album" {
|
|
t.Errorf("Expected similar album title 'Similar Album', got '%s'", resultAlbum.SimilarAlbums[0].Title)
|
|
}
|
|
|
|
// Test trailer
|
|
if resultAlbum.Trailer == nil {
|
|
t.Fatal("Expected Trailer to be non-nil")
|
|
}
|
|
if !resultAlbum.Trailer.Available {
|
|
t.Errorf("Expected Trailer.Available true, got false")
|
|
}
|
|
}
|
|
|
|
func TestAlbum_DeJSON_Minimal(t *testing.T) {
|
|
client := newMockClient()
|
|
data := []byte(`{
|
|
"id": "minimal",
|
|
"title": "Minimal Album"
|
|
}`)
|
|
|
|
album := &Album{BaseModel: *NewBaseModel(client)}
|
|
result, err := album.DeJSON(data, client)
|
|
if err != nil {
|
|
t.Fatalf("DeJSON failed: %v", err)
|
|
}
|
|
|
|
resultAlbum := result.(*Album)
|
|
if resultAlbum.ID != "minimal" {
|
|
t.Errorf("Expected ID 'minimal', got '%s'", resultAlbum.ID)
|
|
}
|
|
if resultAlbum.Title != "Minimal Album" {
|
|
t.Errorf("Expected Title 'Minimal Album', got '%s'", resultAlbum.Title)
|
|
}
|
|
// Other fields should be zero values
|
|
if resultAlbum.Year != 0 {
|
|
t.Errorf("Expected Year 0, got %d", resultAlbum.Year)
|
|
}
|
|
if resultAlbum.Available {
|
|
t.Errorf("Expected Available false, got true")
|
|
}
|
|
}
|
|
|
|
func TestAlbum_DeList(t *testing.T) {
|
|
client := newMockClient()
|
|
data := []byte(`[
|
|
{"id": "1", "title": "Album 1", "year": 2022, "track_count": 5, "available": true},
|
|
{"id": "2", "title": "Album 2", "year": 2023, "track_count": 8, "available": true},
|
|
{"id": "3", "title": "Album 3", "year": 2021, "track_count": 12, "available": false}
|
|
]`)
|
|
|
|
album := &Album{BaseModel: *NewBaseModel(client)}
|
|
result, err := album.DeList(data, client)
|
|
if err != nil {
|
|
t.Fatalf("DeList failed: %v", err)
|
|
}
|
|
|
|
if len(result) != 3 {
|
|
t.Errorf("Expected 3 albums, got %d", len(result))
|
|
}
|
|
|
|
albums := make([]*Album, len(result))
|
|
for i, a := range result {
|
|
albums[i] = a.(*Album)
|
|
}
|
|
|
|
if albums[0].ID != "1" || albums[0].Title != "Album 1" || albums[0].Year != 2022 {
|
|
t.Errorf("Album 1 mismatch: %+v", albums[0])
|
|
}
|
|
if albums[1].ID != "2" || albums[1].Title != "Album 2" || albums[1].Year != 2023 {
|
|
t.Errorf("Album 2 mismatch: %+v", albums[1])
|
|
}
|
|
if albums[2].ID != "3" || albums[2].Title != "Album 3" || albums[2].Year != 2021 {
|
|
t.Errorf("Album 3 mismatch: %+v", albums[2])
|
|
}
|
|
}
|
|
|
|
func TestAlbum_DeList_Empty(t *testing.T) {
|
|
client := newMockClient()
|
|
data := []byte(`[]`)
|
|
|
|
album := &Album{BaseModel: *NewBaseModel(client)}
|
|
result, err := album.DeList(data, client)
|
|
if err != nil {
|
|
t.Fatalf("DeList failed: %v", err)
|
|
}
|
|
|
|
if len(result) != 0 {
|
|
t.Errorf("Expected 0 albums, got %d", len(result))
|
|
}
|
|
}
|
|
|
|
func TestAlbumMeta_DeJSON(t *testing.T) {
|
|
client := newMockClient()
|
|
data := []byte(`{
|
|
"availability": {"available_for_premium_users": true, "territories": ["RU", "US", "EU"]},
|
|
"upsell": {"type": "premium", "description": "Upgrade to premium for full access"}
|
|
}`)
|
|
|
|
meta := &AlbumMeta{BaseModel: *NewBaseModel(client)}
|
|
result, err := meta.DeJSON(data, client)
|
|
if err != nil {
|
|
t.Fatalf("DeJSON failed: %v", err)
|
|
}
|
|
|
|
resultMeta := result.(*AlbumMeta)
|
|
if resultMeta.Availability == nil {
|
|
t.Fatal("Expected Availability to be non-nil")
|
|
}
|
|
if !resultMeta.Availability.AvailableForPremiumUsers {
|
|
t.Errorf("Expected AvailableForPremiumUsers true")
|
|
}
|
|
if len(resultMeta.Availability.Territories) != 3 {
|
|
t.Errorf("Expected 3 territories, got %d", len(resultMeta.Availability.Territories))
|
|
}
|
|
if resultMeta.Upsell == nil {
|
|
t.Fatal("Expected Upsell to be non-nil")
|
|
}
|
|
if resultMeta.Upsell.Type != "premium" {
|
|
t.Errorf("Expected Upsell Type 'premium', got '%s'", resultMeta.Upsell.Type)
|
|
}
|
|
if resultMeta.Upsell.Description != "Upgrade to premium for full access" {
|
|
t.Errorf("Expected Upsell Description 'Upgrade to premium for full access', got '%s'", resultMeta.Upsell.Description)
|
|
}
|
|
}
|
|
|
|
func TestAlbumMeta_DeList(t *testing.T) {
|
|
client := newMockClient()
|
|
data := []byte(`[
|
|
{"availability": {"available_for_premium_users": true}},
|
|
{"availability": {"available_for_premium_users": false}}
|
|
]`)
|
|
|
|
meta := &AlbumMeta{BaseModel: *NewBaseModel(client)}
|
|
result, err := meta.DeList(data, client)
|
|
if err != nil {
|
|
t.Fatalf("DeList failed: %v", err)
|
|
}
|
|
|
|
if len(result) != 2 {
|
|
t.Errorf("Expected 2 meta objects, got %d", len(result))
|
|
}
|
|
}
|
|
|
|
func TestAlbumTrailerStatus_DeJSON(t *testing.T) {
|
|
client := newMockClient()
|
|
data := []byte(`{
|
|
"available": true
|
|
}`)
|
|
|
|
status := &AlbumTrailerStatus{BaseModel: *NewBaseModel(client)}
|
|
result, err := status.DeJSON(data, client)
|
|
if err != nil {
|
|
t.Fatalf("DeJSON failed: %v", err)
|
|
}
|
|
|
|
resultStatus := result.(*AlbumTrailerStatus)
|
|
if !resultStatus.Available {
|
|
t.Errorf("Expected Available true, got false")
|
|
}
|
|
}
|
|
|
|
func TestAlbumTrailerStatus_DeList(t *testing.T) {
|
|
client := newMockClient()
|
|
data := []byte(`[
|
|
{"available": true},
|
|
{"available": false}
|
|
]`)
|
|
|
|
status := &AlbumTrailerStatus{BaseModel: *NewBaseModel(client)}
|
|
result, err := status.DeList(data, client)
|
|
if err != nil {
|
|
t.Fatalf("DeList failed: %v", err)
|
|
}
|
|
|
|
if len(result) != 2 {
|
|
t.Errorf("Expected 2 statuses, got %d", len(result))
|
|
}
|
|
}
|