-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusixmatch_test.go
141 lines (107 loc) · 2.98 KB
/
musixmatch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package musixmatch
import (
"encoding/json"
"github.com/fr05t1k/musixmatch/config"
"github.com/fr05t1k/musixmatch/http"
"github.com/fr05t1k/musixmatch/http/methods"
"github.com/stretchr/testify/assert"
"net/url"
"os"
"testing"
//"github.com/fr05t1k/musixmatch/entity/track"
"fmt"
"github.com/fr05t1k/musixmatch/entity/track"
)
const trackId uint32 = 113303287
func getApiKey() string {
key := os.Getenv("MUSIXMATCH_API_KEY")
if key == "" {
panic("Please specify MUSIXMATCH_API_KEY environment variable")
}
return key
}
func TestGetLyrics(t *testing.T) {
mm := NewClient(getApiKey())
expectedId := uint32(trackId)
lyrics, err := mm.GetLyrics(expectedId)
assert.Nil(t, err)
assert.True(t, len(lyrics.Body) > 0)
}
func TestMusixMatch_GetSnippet(t *testing.T) {
mm := NewClient(getApiKey())
expectedId := uint32(trackId)
snippet, err := mm.GetSnippet(expectedId)
assert.Nil(t, err)
assert.True(t, len(snippet.Body) > 0)
}
func TestMusixMatch_GetSubtitles(t *testing.T) {
mm := NewClient(getApiKey())
expectedId := uint32(trackId)
subtitle, _ := mm.GetSubtitles(expectedId)
assert.Nil(t, subtitle)
}
func TestMusixMatch_SearchTrack(t *testing.T) {
mm := NewClient(getApiKey())
request := http.SearchRequest{
Query: "Heathens",
}
tracks, err := mm.SearchTrack(request)
assert.Nil(t, err)
assert.NotEmpty(t, tracks)
trackList := tracks[0]
assert.NotEmpty(t, trackList.Track.Id)
assert.NotEmpty(t, trackList.Track.AlbumId)
assert.NotEmpty(t, trackList.Track.Name)
assert.NotEmpty(t, trackList.Track.Rating)
}
func TestMusixMatch_GetTrack(t *testing.T) {
mm := NewClient(getApiKey())
trackEntity, err := mm.GetTrack(trackId)
assert.Nil(t, err)
assert.NotEmpty(t, trackEntity)
}
func TestMusixMatch_GetMatchingTrack(t *testing.T) {
mm := NewClient(getApiKey())
trackEntity, err := mm.GetMatchingTrack("Yesterday", "Beatles")
assert.Nil(t, err)
assert.NotEmpty(t, trackEntity)
}
func TestMusixMatch_Request(t *testing.T) {
mm := NewClient(getApiKey())
params := url.Values{}
params.Add(config.TrackId, fmt.Sprintf("%d", trackId))
resp, err := mm.Request(methods.TrackGet, params)
assert.Nil(t, err)
var trackResponse track.Response
json.Unmarshal(resp, &trackResponse)
assert.Equal(t, uint16(200), trackResponse.Message.Header.StatusCode)
}
// Basic usages
func ExampleNewClient() {
client := NewClient("{apikey}")
lyrics, err := client.GetLyrics(trackId)
if err != nil {
fmt.Println(lyrics.Body)
}
}
// Search track request
func ExampleClient_SearchTrack() {
mm := NewClient(getApiKey())
request := http.SearchRequest{
Query: "Heathens",
}
tracks, err := mm.SearchTrack(request)
if err != nil {
fmt.Println(len(tracks))
}
}
func ExampleClient_Request() {
mm := NewClient(getApiKey())
// prepare a method and a query
params := url.Values{}
params.Add(config.TrackId, fmt.Sprintf("%d", trackId))
resp, _ := mm.Request(methods.TrackGet, params)
var trackResponse track.Response
// Let's hydrate response
json.Unmarshal(resp, &trackResponse)
}