/
DataProviderTests.swift
244 lines (212 loc) · 12.3 KB
/
DataProviderTests.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
//
// DataProviderTests.swift
// AmuseKit
//
// Created by Jota Uribe on 18/06/21.
//
@testable import AmuseKit
import Combine
import XCTest
final class DataProviderTests: XCTestCase {
let timeout: TimeInterval = 2.0
var tasks: Set<AnyCancellable> = []
override func tearDown() {
tasks = []
}
func test_developerTokenSavesOnStorage() {
let developerToken = "A1U2S3E4R5T6O7K8E9N0"
let mockStorage = MockStorageService()
let sut: AmuseKit.DataProvider = .init(storage: mockStorage,
requestCoordinator: MockRequestCoordinator())
sut.setDeveloperToken(developerToken)
XCTAssertEqual(mockStorage.developerToken, developerToken)
}
func test_userTokenSavesOnStorage() {
let userToken = "A1U2S3E4R5T6O7K8E9N0"
let mockStorage = MockStorageService()
let sut: AmuseKit.DataProvider = .init(storage: mockStorage,
requestCoordinator: MockRequestCoordinator())
sut.setUserToken(userToken)
XCTAssertEqual(mockStorage.userToken, userToken)
}
func test_request_withErrorResponse_returnsErrors() throws {
let completionExpectation = XCTestExpectation(description: "completion should be called")
let valueExpectation = XCTestExpectation(description: "value callback should be called")
let sut: AmuseKit.DataProvider = .mock(resourceName: "invalid_authentication")
try sut.library(.albums).sink { error in
completionExpectation.fulfill()
} receiveValue: { response in
let error = response.errors?.first
XCTAssertEqual(error?.id, "KHFHBMG2ZFDUYJQUMALKZAZ6LI")
XCTAssertEqual(error?.code, "40300")
XCTAssertEqual(error?.detail, "Invalid authentication")
XCTAssertEqual(error?.status, "403")
valueExpectation.fulfill()
}.store(in: &tasks)
wait(for: [valueExpectation, completionExpectation], timeout: timeout)
}
// MARK: - Catalog Methods
func test_catalogRequest_withMissingDeveloperToken_throwsError() throws {
let sut: AmuseKit.DataProvider = .init(storage: MockStorageService(),
requestCoordinator: MockRequestCoordinator())
XCTAssertThrowsError(try sut.catalog(.albums, ids: []))
}
func test_catalogAlbumsRequest_withValidResponse_returnsData() throws {
let ids = ["123", "456", "789"]
let sut: AmuseKit.DataProvider = .mock(resourceName: "catalog_albums")
try test_catalogRequest_withValidResponse_returnsData(provider: sut,
resourceType: .albums,
ids: ids)
}
func test_catalogArtistsRequest_withValidResponse_returnsData() throws {
let ids = ["123", "456", "789"]
let sut: AmuseKit.DataProvider = .mock(resourceName: "catalog_artists")
try test_catalogRequest_withValidResponse_returnsData(provider: sut,
resourceType: .artists,
ids: ids)
}
func test_catalogMusicVideosRequest_withValidResponse_returnsData() throws {
let ids = ["123", "456", "789"]
let sut: AmuseKit.DataProvider = .mock(resourceName: "catalog_music-videos")
try test_catalogRequest_withValidResponse_returnsData(provider: sut,
resourceType: .musicVideos,
ids: ids)
}
func test_catalogPlaylistsRequest_withValidResponse_returnsData() throws {
let ids = ["123", "456", "789"]
let sut: AmuseKit.DataProvider = .mock(resourceName: "catalog_playlists")
try test_catalogRequest_withValidResponse_returnsData(provider: sut,
resourceType: .playlists,
ids: ids)
}
func test_catalogSongsRequest_withValidResponse_returnsData() throws {
let ids = ["123", "456", "789"]
let sut: AmuseKit.DataProvider = .mock(resourceName: "catalog_songs")
try test_catalogRequest_withValidResponse_returnsData(provider: sut,
resourceType: .songs,
ids: ids)
}
func test_catalogSearchRequest_withValidResponse_returnsResults() throws {
let completionExpectation = XCTestExpectation(description: "completion should be called")
let valueExpectation = XCTestExpectation(description: "value callback should be called")
let sut: AmuseKit.DataProvider = .mock(resourceName: "catalog_search")
try sut.catalogSearch(searchTerm: "")
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in
completionExpectation.fulfill()
}, receiveValue: { value in
XCTAssertNotNil(value.results?.playlists)
XCTAssertNotNil(value.results?.albums)
XCTAssertNotNil(value.results?.artists)
XCTAssertNotNil(value.results?.songs)
XCTAssertNotNil(value.results?.musicVideos)
valueExpectation.fulfill()
}).store(in: &tasks)
wait(for: [completionExpectation, valueExpectation], timeout: timeout)
}
// MARK: Library Methods
func test_libraryRequest_withMissingDeveloperToken_throwsError() throws {
let sut: AmuseKit.DataProvider = .init(storage: MockStorageService(),
requestCoordinator: MockRequestCoordinator())
XCTAssertThrowsError(try sut.library(.albums))
}
func test_libraryAlbumsRequest_withValidResponse_returnsData() throws {
let sut: AmuseKit.DataProvider = .mock(resourceName: "library_albums")
try test_libraryRequest_withValidResponse_returnsData(provider: sut,
resourceType: .albums)
}
func test_libraryArtistsRequest_withValidResponse_returnsData() throws {
let sut: AmuseKit.DataProvider = .mock(resourceName: "library_artists")
try test_libraryRequest_withValidResponse_returnsData(provider: sut,
resourceType: .artists)
}
func test_libraryMusicVideosRequest_withValidResponse_returnsData() throws {
let sut: AmuseKit.DataProvider = .mock(resourceName: "library_music-videos")
try test_libraryRequest_withValidResponse_returnsData(provider: sut,
resourceType: .musicVideos)
}
func test_libraryPlaylistsRequest_withValidResponse_returnsData() throws {
let sut: AmuseKit.DataProvider = .mock(resourceName: "library_playlists")
try test_libraryRequest_withValidResponse_returnsData(provider: sut,
resourceType: .playlists)
}
func test_librarySongsRequest_withValidResponse_returnsData() throws {
let sut: AmuseKit.DataProvider = .mock(resourceName: "library_songs")
try test_libraryRequest_withValidResponse_returnsData(provider: sut,
resourceType: .songs)
}
func test_LibrarySearchRequest_withValidResponse_returnsResults() throws {
let completionExpectation = XCTestExpectation(description: "completion should be called")
let valueExpectation = XCTestExpectation(description: "value callback should be called")
let sut: AmuseKit.DataProvider = .mock(resourceName: "library_search")
try sut.librarySearch(searchTerm: "")
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in
completionExpectation.fulfill()
}, receiveValue: { value in
XCTAssertNotNil(value.results?.playlists)
XCTAssertNotNil(value.results?.albums)
XCTAssertNotNil(value.results?.artists)
XCTAssertNotNil(value.results?.songs)
valueExpectation.fulfill()
}).store(in: &tasks)
wait(for: [completionExpectation, valueExpectation], timeout: timeout)
}
static var allTests = [
("test_developerTokenSavesOnStorage", test_developerTokenSavesOnStorage),
("test_userTokenSavesOnStorage", test_userTokenSavesOnStorage),
("test_request_withErrorResponse_returnsErrors", test_request_withErrorResponse_returnsErrors),
// Catalog
("test_catalogRequest_withMissingDeveloperToken_throwsError", test_catalogRequest_withMissingDeveloperToken_throwsError),
("test_catalogAlbumsRequest_withValidResponse_returnsData", test_catalogAlbumsRequest_withValidResponse_returnsData),
("test_catalogArtistsRequest_withValidResponse_returnsData", test_catalogArtistsRequest_withValidResponse_returnsData),
("test_catalogMusicVideosRequest_withValidResponse_returnsData", test_catalogMusicVideosRequest_withValidResponse_returnsData),
("test_catalogPlaylistsRequest_withValidResponse_returnsData", test_catalogPlaylistsRequest_withValidResponse_returnsData),
("test_catalogSongsRequest_withValidResponse_returnsData", test_catalogSongsRequest_withValidResponse_returnsData),
("test_catalogSearchRequest_withValidResponse_returnsResults", test_catalogSearchRequest_withValidResponse_returnsResults),
// Library
("test_libraryRequest_withMissingDeveloperToken_throwsError", test_libraryRequest_withMissingDeveloperToken_throwsError),
("test_libraryAlbumsRequest_withValidResponse_returnsData", test_libraryAlbumsRequest_withValidResponse_returnsData),
("test_libraryArtistsRequest_withValidResponse_returnsData", test_libraryArtistsRequest_withValidResponse_returnsData),
("test_libraryMusicVideosRequest_withValidResponse_returnsData", test_libraryMusicVideosRequest_withValidResponse_returnsData),
("test_libraryPlaylistsRequest_withValidResponse_returnsData", test_libraryPlaylistsRequest_withValidResponse_returnsData),
("test_librarySongsRequest_withValidResponse_returnsData", test_librarySongsRequest_withValidResponse_returnsData),
("test_LibrarySearchRequest_withValidResponse_returnsResults", test_LibrarySearchRequest_withValidResponse_returnsResults)
]
}
// MARK: - Helper Methods
extension DataProviderTests {
private func test_catalogRequest_withValidResponse_returnsData<Resource: Codable>(
provider: AmuseKit.DataProvider,
resourceType: AmuseKit.CatalogResourceConvertible<Resource>,
ids: [String]
) throws {
let completionExpectation = XCTestExpectation(description: "completion should be called")
let valueExpectation = XCTestExpectation(description: "value callback should be called")
try provider.catalog(resourceType, ids: ids)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in
completionExpectation.fulfill()
}, receiveValue: { value in
XCTAssertNotNil(value.data)
valueExpectation.fulfill()
}).store(in: &tasks)
wait(for: [completionExpectation, valueExpectation], timeout: timeout)
}
private func test_libraryRequest_withValidResponse_returnsData<Resource: Codable>(
provider: AmuseKit.DataProvider,
resourceType: AmuseKit.LibraryResourceConvertible<Resource>
) throws {
let completionExpectation = XCTestExpectation(description: "completion should be called")
let valueExpectation = XCTestExpectation(description: "value callback should be called")
try provider.library(resourceType)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in
completionExpectation.fulfill()
}, receiveValue: { value in
XCTAssertNotNil(value.data)
valueExpectation.fulfill()
}).store(in: &tasks)
wait(for: [completionExpectation, valueExpectation], timeout: timeout)
}
}