Skip to content

Commit 89874cc

Browse files
author
Clément Le Provost
committed
Add test case for multiple queries strategy parameter
1 parent d4aa18a commit 89874cc

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

Tests/ClientTests.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,49 @@ class ClientTests: XCTestCase {
268268

269269
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
270270
}
271-
271+
272+
func testMultipleQueries_stopIfEnoughMatches() {
273+
let expectation = expectationWithDescription("testMultipleQueries")
274+
let object = ["city": "San Francisco"]
275+
276+
index.addObject(object, completionHandler: { (content, error) -> Void in
277+
if let error = error {
278+
XCTFail("Error during addObject: \(error)")
279+
expectation.fulfill()
280+
} else {
281+
self.index.waitTask(content!["taskID"] as! Int, completionHandler: { (content, error) -> Void in
282+
if let error = error {
283+
XCTFail("Error during waitTask: \(error)")
284+
expectation.fulfill()
285+
} else {
286+
let query = Query()
287+
query.hitsPerPage = 1
288+
let queries = [
289+
IndexQuery(index: self.index, query: query),
290+
IndexQuery(index: self.index, query: query)
291+
]
292+
293+
self.client.multipleQueries(queries, strategy: .StopIfEnoughMatches, completionHandler: { (content, error) -> Void in
294+
if let error = error {
295+
XCTFail("Error during multipleQueries: \(error)")
296+
} else {
297+
let items = content!["results"] as! [[String: AnyObject]]
298+
XCTAssert(items.count == 2) // each query should return an item...
299+
XCTAssertEqual(items[0]["nbHits"] as? Int, 1, "Wrong number of object in the index")
300+
// ... but the second query should not have been processed
301+
XCTAssertEqual(items[1]["processed"] as? Bool, false)
302+
XCTAssertEqual(items[1]["nbHits"] as? Int, 0, "Wrong number of object in the index")
303+
}
304+
expectation.fulfill()
305+
})
306+
}
307+
})
308+
}
309+
})
310+
311+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
312+
}
313+
272314
func testHeaders() {
273315
// Make a call with a valid API key.
274316
let expectation1 = expectationWithDescription("Valid API key")

0 commit comments

Comments
 (0)