Initial commit
This commit is contained in:
commit
02a6dabcfb
62 files changed
+15751
No files matched your search
@@ -0,0 +1,136 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// mockRequest implements RequestInterface for testing
|
||||
type mockRequest struct{}
|
||||
|
||||
func (m *mockRequest) Get(ctx context.Context, path string, params map[string]string, result any) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockRequest) Post(ctx context.Context, path string, body any, result any) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockRequest) Put(ctx context.Context, path string, body any, result any) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockRequest) Delete(ctx context.Context, path string, body any, result any) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockRequest) Download(ctx context.Context, url string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// mockClient implements ClientInterface for testing
|
||||
type mockClient struct {
|
||||
request *mockRequest
|
||||
}
|
||||
|
||||
func (m *mockClient) GetRequest() RequestInterface {
|
||||
return m.request
|
||||
}
|
||||
|
||||
func newMockClient() *mockClient {
|
||||
return &mockClient{request: &mockRequest{}}
|
||||
}
|
||||
|
||||
func TestTrack_DeJSON(t *testing.T) {
|
||||
client := newMockClient()
|
||||
data := []byte(`{
|
||||
"id": "123",
|
||||
"title": "Test Track",
|
||||
"duration_ms": 180000,
|
||||
"storage_id": "456",
|
||||
"cover_uri": "https://example.com/cover.jpg",
|
||||
"genre": "rock",
|
||||
"version": "album",
|
||||
"explicit": false,
|
||||
"available": true,
|
||||
"available_for_premium_users": true,
|
||||
"r128": -12.5,
|
||||
"track_sharing_flag": "allowed",
|
||||
"artists": [{"id": "1", "name": "Artist 1", "cover_uri": "https://example.com/artist.jpg"}],
|
||||
"albums": [{"id": "789", "title": "Test Album", "cover_uri": "https://example.com/album.jpg", "year": 2023}],
|
||||
"major": {"id": "major1", "name": "Major Label", "description": "Big label"},
|
||||
"lyrics": {"id": "lyr1", "text": "Test lyrics", "available": true, "has_text": true}
|
||||
}`)
|
||||
|
||||
track := &Track{BaseModel: *NewBaseModel(client)}
|
||||
result, err := track.DeJSON(data, client)
|
||||
if err != nil {
|
||||
t.Fatalf("DeJSON failed: %v", err)
|
||||
}
|
||||
|
||||
resultTrack := result.(*Track)
|
||||
if resultTrack.ID != "123" {
|
||||
t.Errorf("Expected ID '123', got '%s'", resultTrack.ID)
|
||||
}
|
||||
if resultTrack.Title != "Test Track" {
|
||||
t.Errorf("Expected Title 'Test Track', got '%s'", resultTrack.Title)
|
||||
}
|
||||
if resultTrack.DurationMs != 180000 {
|
||||
t.Errorf("Expected DurationMs 180000, got %d", resultTrack.DurationMs)
|
||||
}
|
||||
if len(resultTrack.Artists) != 1 {
|
||||
t.Errorf("Expected 1 artist, got %d", len(resultTrack.Artists))
|
||||
}
|
||||
if resultTrack.Major == nil || resultTrack.Major.Name != "Major Label" {
|
||||
t.Errorf("Expected Major 'Major Label'")
|
||||
}
|
||||
if resultTrack.Lyrics == nil || resultTrack.Lyrics.Text != "Test lyrics" {
|
||||
t.Errorf("Expected Lyrics 'Test lyrics'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrack_DeList(t *testing.T) {
|
||||
client := newMockClient()
|
||||
data := []byte(`[
|
||||
{"id": "1", "title": "Track 1", "duration_ms": 100000},
|
||||
{"id": "2", "title": "Track 2", "duration_ms": 200000}
|
||||
]`)
|
||||
|
||||
track := &Track{BaseModel: *NewBaseModel(client)}
|
||||
result, err := track.DeList(data, client)
|
||||
if err != nil {
|
||||
t.Fatalf("DeList failed: %v", err)
|
||||
}
|
||||
|
||||
if len(result) != 2 {
|
||||
t.Errorf("Expected 2 tracks, got %d", len(result))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrack_GetDownloadInfo(t *testing.T) {
|
||||
client := newMockClient()
|
||||
track := &Track{
|
||||
BaseModel: *NewBaseModel(client),
|
||||
DownloadInfo: []DownloadInfo{
|
||||
{Quality: "mp3", Codec: "mp3", Bitrate: 320, Size: 5000000, URL: "https://example.com/track.mp3"},
|
||||
{Quality: "flac", Codec: "flac", Bitrate: 1411, Size: 25000000, URL: "https://example.com/track.flac"},
|
||||
},
|
||||
}
|
||||
|
||||
info := track.GetDownloadInfo("mp3")
|
||||
if info == nil {
|
||||
t.Fatal("Expected mp3 download info")
|
||||
}
|
||||
if info.Bitrate != 320 {
|
||||
t.Errorf("Expected bitrate 320, got %d", info.Bitrate)
|
||||
}
|
||||
|
||||
info = track.GetDownloadInfo("flac")
|
||||
if info == nil {
|
||||
t.Fatal("Expected flac download info")
|
||||
}
|
||||
if info.Bitrate != 1411 {
|
||||
t.Errorf("Expected bitrate 1411, got %d", info.Bitrate)
|
||||
}
|
||||
|
||||
info = track.GetDownloadInfo("ogg")
|
||||
if info != nil {
|
||||
t.Errorf("Expected nil for ogg, got %v", info)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user