Skip to content

Commit 99860ea

Browse files
committed
fix some bugs and add more tests
1 parent 9b39f19 commit 99860ea

File tree

3 files changed

+378
-38
lines changed

3 files changed

+378
-38
lines changed

Source/Client.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,17 @@ public class Client {
194194
///
195195
/// :param: offset Specify the first entry to retrieve (0-based, 0 is the most recent log entry).
196196
/// :param: length Specify the maximum number of entries to retrieve starting at offset. Maximum allowed value: 1000.
197-
public func getLogsWithOffset(offset: UInt, lenght: UInt, block: CompletionHandler) {
198-
let path = "1/logs?offset=\(offset)&lenght=\(lenght)"
197+
public func getLogsWithOffset(offset: UInt, length: UInt, block: CompletionHandler) {
198+
let path = "1/logs?offset=\(offset)&length=\(length)"
199199
performHTTPQuery(path, method: .GET, body: nil, block: block)
200200
}
201201

202202
/// Return last logs entries.
203203
///
204204
/// :param: offset Specify the first entry to retrieve (0-based, 0 is the most recent log entry).
205205
/// :param: length Specify the maximum number of entries to retrieve starting at offset. Maximum allowed value: 1000.
206-
public func getLogsWithType(type: String, offset: UInt, lenght: UInt, block: CompletionHandler) {
207-
let path = "1/logs?offset=\(offset)&lenght=\(lenght)&type=\(type)"
206+
public func getLogsWithType(type: String, offset: UInt, length: UInt, block: CompletionHandler) {
207+
let path = "1/logs?offset=\(offset)&length=\(length)&type=\(type)"
208208
performHTTPQuery(path, method: .GET, body: nil, block: block)
209209
}
210210

@@ -328,17 +328,17 @@ public class Client {
328328

329329
/// Query multiple indexes with one API call.
330330
///
331-
/// :param: queries An array of queries with the associated index (Array of Dictionnary object ["indexName": "targettedIndex", "query": "theASQuery"]).
331+
/// :param: queries An array of queries with the associated index (Array of Dictionnary object ["indexName": "targettedIndex", "query": QueryObject]).
332332
public func multipleQueries(queries: [AnyObject], block: CompletionHandler? = nil) {
333333
let path = "1/indexes/*/queries"
334334

335-
var convertedQueries = [AnyObject]()
335+
var convertedQueries = [[String: String]]()
336336
convertedQueries.reserveCapacity(queries.count)
337337
for query in queries {
338-
if let query = query as? [String: String] {
338+
if let query = query as? [String: AnyObject] {
339339
convertedQueries.append([
340-
"params": query["query"]!.urlEncode(),
341-
"indexName": query["indexName"]!
340+
"params": (query["query"] as Query).buildURL(),
341+
"indexName": query["indexName"] as String
342342
])
343343
}
344344
}

Tests/ClientTests.swift

Lines changed: 133 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ class ClientTests: XCTestCase {
7373
XCTFail("Error during waitTask: \(error)")
7474
expectation.fulfill()
7575
} else {
76-
XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed")
77-
7876
self.client.listIndexes({ (JSON, error) -> Void in
7977
if let error = error {
8078
XCTFail("Error during listIndexes: \(error)")
@@ -127,8 +125,6 @@ class ClientTests: XCTestCase {
127125
XCTFail("Error during waitTask: \(error)")
128126
expecation.fulfill()
129127
} else {
130-
XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed")
131-
132128
let dstIndex = self.client.getIndex("algol?à-swift2")
133129
dstIndex.search(Query(), block: { (JSON, error) -> Void in
134130
if let error = error {
@@ -187,11 +183,7 @@ class ClientTests: XCTestCase {
187183
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
188184
if let error = error {
189185
XCTFail("Error during waitTask: \(error)")
190-
expecation.fulfill()
191186
} else {
192-
XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed")
193-
expecation.fulfill()
194-
195187
self.index.search(Query(), block: { (JSON, error) -> Void in
196188
if let error = error {
197189
XCTFail("Error during search: \(error)")
@@ -215,6 +207,8 @@ class ClientTests: XCTestCase {
215207
dstIndexExpectation.fulfill()
216208
})
217209
}
210+
211+
expecation.fulfill()
218212
})
219213
}
220214
})
@@ -233,4 +227,135 @@ class ClientTests: XCTestCase {
233227

234228
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
235229
}
230+
231+
func testGetLogs() {
232+
let expecation = expectationWithDescription("testGetLogs")
233+
let object = ["city": "San Francisco", "objectID": "a/go/?à"]
234+
235+
index.addObject(object, block: { (JSON, error) -> Void in
236+
if let error = error {
237+
XCTFail("Error during addObject: \(error)")
238+
expecation.fulfill()
239+
} else {
240+
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
241+
if let error = error {
242+
XCTFail("Error during waitTask: \(error)")
243+
expecation.fulfill()
244+
} else {
245+
self.client.getLogs({ (JSON, error) -> Void in
246+
if let error = error {
247+
XCTFail("Error during getLogs: \(error)")
248+
} else {
249+
let logs = JSON!["logs"] as [AnyObject]
250+
XCTAssertNotEqual(logs.count, 0, "Get logs failed")
251+
}
252+
253+
expecation.fulfill()
254+
})
255+
}
256+
})
257+
}
258+
})
259+
260+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
261+
}
262+
263+
func testGetLogsWithOffset() {
264+
let expecation = expectationWithDescription("testGetLogsWithOffset")
265+
let object = ["city": "San Francisco", "objectID": "a/go/?à"]
266+
267+
index.addObject(object, block: { (JSON, error) -> Void in
268+
if let error = error {
269+
XCTFail("Error during addObject: \(error)")
270+
expecation.fulfill()
271+
} else {
272+
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
273+
if let error = error {
274+
XCTFail("Error during waitTask: \(error)")
275+
expecation.fulfill()
276+
} else {
277+
self.client.getLogsWithOffset(0, length: 1, block: { (JSON, error) -> Void in
278+
if let error = error {
279+
XCTFail("Error during getLogs: \(error)")
280+
} else {
281+
let logs = JSON!["logs"] as [AnyObject]
282+
XCTAssertEqual(logs.count, 1, "Get logs failed")
283+
}
284+
285+
expecation.fulfill()
286+
})
287+
}
288+
})
289+
}
290+
})
291+
292+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
293+
}
294+
295+
func testGetLogsWithType() {
296+
let expecation = expectationWithDescription("testGetLogsWithType")
297+
let object = ["city": "San Francisco", "objectID": "a/go/?à"]
298+
299+
index.addObject(object, block: { (JSON, error) -> Void in
300+
if let error = error {
301+
XCTFail("Error during addObject: \(error)")
302+
expecation.fulfill()
303+
} else {
304+
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
305+
if let error = error {
306+
XCTFail("Error during waitTask: \(error)")
307+
expecation.fulfill()
308+
} else {
309+
self.client.getLogsWithType("error", offset: 0, length: 1, block: { (JSON, error) -> Void in
310+
if let error = error {
311+
XCTFail("Error during getLogs: \(error)")
312+
} else {
313+
let logs = JSON!["logs"] as [AnyObject]
314+
XCTAssertEqual(logs.count, 1, "Get logs failed")
315+
}
316+
317+
expecation.fulfill()
318+
})
319+
}
320+
})
321+
}
322+
})
323+
324+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
325+
}
326+
327+
func testMultipleQueries() {
328+
let expectation = expectationWithDescription("testMultipleQueries")
329+
let object = ["city": "San Francisco"]
330+
331+
index.addObject(object, block: { (JSON, error) -> Void in
332+
if let error = error {
333+
XCTFail("Error during addObject: \(error)")
334+
expectation.fulfill()
335+
} else {
336+
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
337+
if let error = error {
338+
XCTFail("Error during waitTask: \(error)")
339+
expectation.fulfill()
340+
} else {
341+
let queries = [["indexName": self.index.indexName, "query": Query()]]
342+
343+
self.client.multipleQueries(queries, block: { (JSON, error) -> Void in
344+
if let error = error {
345+
XCTFail("Error during multipleQueries: \(error)")
346+
} else {
347+
let items = JSON!["results"] as [[String: AnyObject]]
348+
let nbHits = items[0]["nbHits"] as Int
349+
XCTAssertEqual(nbHits, 1, "Wrong number of object in the index")
350+
}
351+
352+
expectation.fulfill()
353+
})
354+
}
355+
})
356+
}
357+
})
358+
359+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
360+
}
236361
}

0 commit comments

Comments
 (0)