-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunit_TrackApiTests.swift
250 lines (235 loc) · 10 KB
/
unit_TrackApiTests.swift
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// unitTrackApiTests.swift
// YM-macTests
//
// Created by Developer on 20.07.2021.
//
import XCTest
#if canImport(YmuzApiMac)
@testable import YmuzApiMac
#endif
#if canImport(YmuzApi)
@testable import YmuzApi
#endif
class unit_TrackApiTests: XCTestCase {
var client: YMClient!
var device: YMDevice {get {return TestConstants.device}}
var apiLang: ApiLanguage {get {return TestConstants.apiLang}}
var token: String {get {return TestCredentials.token}}
var xToken: String {get {return TestCredentials.xToken}}
var uid: Int {get {return TestCredentials.uid}}
override func setUp() {
client = YMClient.initialize(device: device, lang: apiLang, uid: uid, token: token, xToken: xToken)
}
override func tearDown() {
client = nil
}
func testTrackSingleResponse() async {
let trackId = "51965869:7956489"
//let exp = self.expectation(description: "Request time-out expectation")
_ = await withCheckedContinuation { continuation in
client.getTracks(trackIds: [trackId], positions: false) { result in
do {
let tracks = try result.get()
XCTAssertNotNil(tracks, "Track is nil")
XCTAssert(tracks.count > 0, "Tracks' array is empty")
XCTAssert(tracks[0].trackId.compare(trackId) == .orderedSame, "Incorrect parsing track object")
} catch {
print(error)
XCTAssert(false, "Empty track object: " + error.localizedDescription)
}
continuation.resume()
}
}
}
func testSimilarTracksResponse() {
let trackId = "51965869:7956489"
let exp = self.expectation(description: "Request time-out expectation")
client.getSimilarTracks(trackId: trackId) { result in
do {
let tracks = try result.get()
let stockTrack = tracks.track
XCTAssertNotNil(stockTrack, "Track is nil")
XCTAssert(tracks.similarTracks.count > 0, "Tracks' array is empty")
XCTAssert(stockTrack?.trackId.compare(trackId) == .orderedSame, "Stock track in similar tracks response doesn't match to the expected")
} catch {
print(error)
XCTAssert(false, "Empty tracks array: " + error.localizedDescription)
}
exp.fulfill()
}
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testTrackSuplementResponse() {
let trackId = "33943347:4158732"
let trackUID = "33943347"
let exp = self.expectation(description: "Request time-out expectation")
client.getTrackSupplement(trackId: trackId) { result in
do {
let supplement = try result.get()
let supId = supplement.id
XCTAssertTrue(supId.compare(trackUID) == .orderedSame, "Incorrect track supplement id")
} catch {
print(error)
XCTAssert(false, "Empty track supplement info: " + error.localizedDescription)
}
exp.fulfill()
}
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testTracksArrayResponse() {
let tracksId = ["51965869:7956489", "33943347:4158732"]
let exp = self.expectation(description: "Request time-out expectation")
client.getTracks(trackIds: tracksId, positions: false) { result in
do {
let tracks = try result.get()
XCTAssert(tracks.count > 0, "Tracks' array is empty")
XCTAssert(tracks.count == tracksId.count, "Response doesn't contain all tracks")
for i in 0...tracksId.count - 1 {
let trackId = tracksId[i]
let track = tracks[i]
XCTAssert(track.trackId.compare(trackId) == .orderedSame, "Incorrect parsing track object with trackId " + trackId)
//TODO add more model fields tests
}
} catch {
print(error)
XCTAssert(false, "Empty track object: " + error.localizedDescription)
}
exp.fulfill()
}
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testGetTrackDownloadLink() {
let trackId = "51965869:7956489"
let exp = self.expectation(description: "Request time-out expectation")
client.getTracks(trackIds: [trackId], positions: false) { result in
do {
let tracks = try result.get()
XCTAssertNotNil(tracks, "Track is nil")
XCTAssert(tracks.count > 0, "Tracks' array is empty")
tracks[0].getDownloadInfo { result2 in
do {
let downloadInfos = try result2.get()
XCTAssertNotNil(downloadInfos, "Track download infos is nil")
XCTAssert(downloadInfos.count > 0, "Track download infos' array is empty")
let info = downloadInfos[0]
info.getDirectLink { result in
do {
let link = try result.get()
XCTAssert(link.compare("") != .orderedSame, "Incorrect direct link format")
} catch {
print(error)
XCTAssert(false, "Empty track download link: " + error.localizedDescription)
}
exp.fulfill()
}
} catch {
print(error)
XCTAssert(false, "Empty track download info object: " + error.localizedDescription)
}
}
} catch {
print(error)
XCTAssert(false, "Empty track object: " + error.localizedDescription)
}
}
waitForExpectations(timeout: 10) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testGetTrackDownloadLinkV2() {
let trackId = "51965869:7956489"
let exp = self.expectation(description: "Request time-out expectation")
client.getTracks(trackIds: [trackId], positions: false) { result in
do {
let tracks = try result.get()
XCTAssertNotNil(tracks, "Track is nil")
XCTAssert(tracks.count > 0, "Tracks' array is empty")
tracks[0].getDownloadInfoV2(canUseStreaming: true) { result2 in
do {
let downloadInfos = try result2.get()
XCTAssertNotNil(downloadInfos, "Track download infos is nil")
XCTAssert(downloadInfos.count > 0, "Track download infos' array is empty")
let info = downloadInfos[0]
info.getDirectLink { result in
do {
let link = try result.get()
XCTAssert(link.compare("") != .orderedSame, "Incorrect direct link format")
} catch {
print(error)
XCTAssert(false, "Empty track download link: " + error.localizedDescription)
}
exp.fulfill()
}
} catch {
print(error)
XCTAssert(false, "Empty track download info object: " + error.localizedDescription)
}
}
} catch {
print(error)
XCTAssert(false, "Empty track object: " + error.localizedDescription)
}
}
waitForExpectations(timeout: 10) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testGetTrackLyrics() {
let trackId = "565378"
let exp = self.expectation(description: "Request time-out expectation")
client.getTracks(trackIds: [trackId], positions: false) { result in
do {
let tracks = try result.get()
XCTAssertNotNil(tracks, "Track is nil")
XCTAssert(tracks.count > 0, "Tracks' array is empty")
tracks[0].getLyricsDownloadInfo(format: "LRC") { result2 in
do {
let downloadInfo = try result2.get()
XCTAssertNotNil(downloadInfo, "Track lyrics download info is nil")
exp.fulfill()
} catch {
print(error)
XCTAssert(false, "Empty track lyrics info object: " + error.localizedDescription)
}
}
} catch {
print(error)
XCTAssert(false, "Empty track object: " + error.localizedDescription)
}
}
waitForExpectations(timeout: 10) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
}