-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunit_SearchApiTests.swift
243 lines (228 loc) · 9.98 KB
/
unit_SearchApiTests.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
//
// unit_SearchApiTests.swift
// YM-macTests
//
// Created by Developer on 25.08.2021.
//
import XCTest
#if canImport(YmuzApiMac)
@testable import YmuzApiMac
#endif
#if canImport(YmuzApi)
@testable import YmuzApi
#endif
class unit_SearchApiTests: 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 testNormalSearchSuggestionResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.getSearchSugggestion(text: "ciao", completion: { result in
do {
let suggestion = try result.get()
XCTAssertTrue(suggestion.suggestions.count > 0, "Possible text suggestions' array is empty")
} catch {
print(error)
XCTAssert(false, "Empty search suggestion 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 testIrrationalSearchSuggestionResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.getSearchSugggestion(text: "bla bla bla abl abl abl", completion: { result in
do {
let suggestion = try result.get()
XCTAssertNil(suggestion.best, "Best search suggestion isn't null")
XCTAssertTrue(suggestion.suggestions.count == 0, "Possible text suggestions' array isn't empty")
} catch {
print(error)
XCTAssert(false, "Empty search suggestion 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 testNormalSearchResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.search(text: "ciao", noCorrect: false, type: .all, page: 0, includeBestPlaylists: false) { result in
do {
let searchData = try result.get()
XCTAssert(searchData.text.compare("ciao") == .orderedSame, "Search text from request (ciao) doesn't match with response data (" + searchData.text + ")")
} catch {
print(error)
XCTAssert(false, "Empty search response 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 testIrrationalSearchResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.search(text: "ciao", noCorrect: false, type: .all, page: -1, includeBestPlaylists: false) { result in
let search = try? result.get()
XCTAssertNil(search, "Search response with negative page index isn't null")
exp.fulfill()
}
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testNextPageSearchResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.search(text: "ciao", noCorrect: false, type: .all, page: 0, includeBestPlaylists: false) { result in
do {
let search = try result.get()
XCTAssert(search.text.compare("ciao") == .orderedSame, "Search text from request (ciao) doesn't match with response data (" + search.text + ")")
search.nextPage(includeBestPlaylists: false) {result2 in
do {
let pageResult = try result2.get()
XCTAssert(pageResult.text.compare("ciao") == .orderedSame, "Search text from request (ciao) doesn't match with response data for another page (" + pageResult.text + ")")
let oldPage = search.page ?? -1
let newPage = pageResult.page ?? -1
XCTAssertTrue(oldPage != newPage, "Page identificators of search responses are same")
} catch {
print(error)
XCTAssert(false, "Empty search response object with next page: " + error.localizedDescription)
}
exp.fulfill()
}
} catch {
print(error)
XCTAssert(false, "Empty search response object: " + error.localizedDescription)
}
}
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testPreviousPageSearchResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.search(text: "ciao", noCorrect: false, type: .all, page: 2, includeBestPlaylists: false) { result in
do {
let search = try result.get()
XCTAssert(search.text.compare("ciao") == .orderedSame, "Search text from request (ciao) doesn't match with response data (" + search.text + ")")
search.prevPage(includeBestPlaylists: false) {result2 in
do {
let pageResult = try result2.get()
XCTAssert(pageResult.text.compare("ciao") == .orderedSame, "Search text from request (ciao) doesn't match with response data for another page (" + pageResult.text + ")")
let oldPage = search.page ?? -1
let newPage = pageResult.page ?? -1
XCTAssertTrue(oldPage != newPage, "Page identificators of search responses are same")
} catch {
print(error)
XCTAssert(false, "Empty search response object with next page: " + error.localizedDescription)
}
exp.fulfill()
}
} catch {
print(error)
XCTAssert(false, "Empty search response object: " + error.localizedDescription)
}
}
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testSearchHistoryResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.getSearchHistory { result in
do {
let history = try result.get()
XCTAssertTrue(history.count > 0, "Search history array is empty")
} catch {
print(error)
XCTAssert(false, "Empty Search history array 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 testFeedbackSearchHistoryResponse() {
let exp = self.expectation(description: "Request time-out expectation")
let feedback = SearchFeedback(absBlockPosition: 3, absPosition: 3, blockPosition: 1, blockType: "album", entityId: "album:4440406", page: 0, position: 0, query: "beliver", searchRequestId: "music-stable-back-vla-22.vla.yp-c.yandex.net-1588698367499402-7678694071821572946-1588698367505")
client.feedbackSearchHistory(feedback) { result in
do {
let status = try result.get()
XCTAssertTrue(status, "False status search history feedback response")
} catch {
print(error)
XCTAssert(false, "Empty feedback search history status: " + 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 testClearSearchHistoryResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.clearSearchHistory { result in
do {
let status = try result.get()
XCTAssertTrue(status, "False status search history clear response")
} catch {
print(error)
XCTAssert(false, "Empty clear search history status: " + error.localizedDescription)
}
exp.fulfill()
}
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
}