@@ -202,4 +202,278 @@ class AlgoliaSearchTests: XCTestCase {
202202
203203 waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
204204 }
205+
206+ func testPartialUpdateObject( ) {
207+ let expectation = expectationWithDescription ( " testPartialUpdateObject " )
208+ let object = [ " city " : " New York " , " initial " : " NY " , " objectID " : " a/go/?à-5 " ]
209+
210+ index. addObject ( object, block: { ( JSON, error) -> Void in
211+ if let error = error {
212+ XCTFail ( " Error during addObject " )
213+ expectation. fulfill ( )
214+ } else {
215+ self . index. partialUpdateObject ( [ " city " : " Los Angeles " ] , objectID: object [ " objectID " ] !, block: { ( JSON, error) -> Void in
216+ if let error = error {
217+ XCTFail ( " Error during partialUpdateObject " )
218+ expectation. fulfill ( )
219+ } else {
220+ self . index. waitTask ( JSON![ " taskID " ] as Int , block: { ( JSON, error) -> Void in
221+ if let error = error {
222+ XCTFail ( " Error during waitTask " )
223+ expectation. fulfill ( )
224+ } else {
225+ XCTAssertEqual ( JSON![ " status " ] as String , " published " , " Wait task failed " )
226+
227+ self . index. getObject ( object [ " objectID " ] !, block: { ( JSON, error) -> Void in
228+ if let error = error {
229+ XCTFail ( " Error during getObject " )
230+ } else {
231+ let city = JSON![ " city " ] as String
232+ let initial = JSON![ " initial " ] as String
233+ XCTAssertEqual ( city, " Los Angeles " , " Partial update is not applied " )
234+ XCTAssertEqual ( initial, " NY " , " Partial update failed " )
235+ }
236+
237+ expectation. fulfill ( )
238+ } )
239+ }
240+ } )
241+ }
242+ } )
243+ }
244+ } )
245+
246+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
247+ }
248+
249+ func testSaveObject( ) {
250+ let expectation = expectationWithDescription ( " testSaveObject " )
251+ let object = [ " city " : " New York " , " initial " : " NY " , " objectID " : " a/go/?à-6 " ]
252+
253+ index. addObject ( object, block: { ( JSON, error) -> Void in
254+ if let error = error {
255+ XCTFail ( " Error during addObject " )
256+ expectation. fulfill ( )
257+ } else {
258+ self . index. saveObject ( [ " city " : " Los Angeles " , " objectID " : " a/go/?à-6 " ] , block: { ( JSON, error) -> Void in
259+ if let error = error {
260+ XCTFail ( " Error during saveObject " )
261+ expectation. fulfill ( )
262+ } else {
263+ self . index. waitTask ( JSON![ " taskID " ] as Int , block: { ( JSON, error) -> Void in
264+ if let error = error {
265+ XCTFail ( " Error during waitTask " )
266+ expectation. fulfill ( )
267+ } else {
268+ XCTAssertEqual ( JSON![ " status " ] as String , " published " , " Wait task failed " )
269+
270+ self . index. getObject ( object [ " objectID " ] !, block: { ( JSON, error) -> Void in
271+ if let error = error {
272+ XCTFail ( " Error during getObject " )
273+ } else {
274+ let city = JSON![ " city " ] as String
275+ let initial : AnyObject ? = JSON![ " initial " ]
276+ XCTAssertEqual ( city, " Los Angeles " , " Save object is not applied " )
277+ XCTAssertNil ( initial, " Save object failed " )
278+ }
279+
280+ expectation. fulfill ( )
281+ } )
282+ }
283+ } )
284+ }
285+ } )
286+ }
287+ } )
288+
289+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
290+ }
291+
292+ func testClear( ) {
293+ let expectation = expectationWithDescription ( " testClear " )
294+ let object = [ " city " : " San Francisco " , " objectID " : " a/go/?à " ]
295+
296+ index. addObject ( object, block: { ( JSON, error) -> Void in
297+ if let error = error {
298+ XCTFail ( " Error during addObject " )
299+ expectation. fulfill ( )
300+ } else {
301+ self . index. clearIndex ( block: { ( JSON, error) -> Void in
302+ if let error = error {
303+ XCTFail ( " Error during clearIndex " )
304+ expectation. fulfill ( )
305+ } else {
306+ self . index. waitTask ( JSON![ " taskID " ] as Int , block: { ( JSON, error) -> Void in
307+ if let error = error {
308+ XCTFail ( " Error during waitTask " )
309+ expectation. fulfill ( )
310+ } else {
311+ XCTAssertEqual ( JSON![ " status " ] as String , " published " , " Wait task failed " )
312+
313+ self . index. search ( Query ( ) , block: { ( JSON, error) -> Void in
314+ if let error = error {
315+ XCTFail ( " Error during search " )
316+ } else {
317+ let nbHits = JSON![ " nbHits " ] as Int
318+ XCTAssertEqual ( nbHits, 0 , " Clear index failed " )
319+ }
320+
321+ expectation. fulfill ( )
322+ } )
323+ }
324+ } )
325+ }
326+ } )
327+ }
328+ } )
329+
330+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
331+ }
332+
333+ func testSettings( ) {
334+ let expectation = expectationWithDescription ( " testSettings " )
335+ let settings = [ " attributesToRetrieve " : [ " name " ] ]
336+
337+ index. setSettings ( settings, block: { ( JSON, error) -> Void in
338+ if let error = error {
339+ XCTFail ( " Error during setSettings " )
340+ expectation. fulfill ( )
341+ } else {
342+ self . index. waitTask ( JSON![ " taskID " ] as Int , block: { ( JSON, error) -> Void in
343+ if let error = error {
344+ XCTFail ( " Error during waitTask " )
345+ expectation. fulfill ( )
346+ } else {
347+ XCTAssertEqual ( JSON![ " status " ] as String , " published " , " Wait task failed " )
348+
349+ self . index. getSettings ( { ( JSON, error) -> Void in
350+ if let error = error {
351+ XCTFail ( " Error during getSettings " )
352+ } else {
353+ let attributesToRetrieve = JSON![ " attributesToRetrieve " ] as [ String ]
354+ XCTAssertEqual ( attributesToRetrieve, settings [ " attributesToRetrieve " ] !, " Set settings failed " )
355+ }
356+
357+ expectation. fulfill ( )
358+ } )
359+ }
360+ } )
361+ }
362+ } )
363+
364+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
365+ }
366+
367+ func testBrowse( ) {
368+ let expectation = expectationWithDescription ( " testBrowse " )
369+ let object = [ " city " : " San Francisco " , " objectID " : " a/go/?à " ]
370+
371+ index. addObject ( object, block: { ( JSON, error) -> Void in
372+ if let error = error {
373+ XCTFail ( " Error during addObject " )
374+ expectation. fulfill ( )
375+ } else {
376+ self . index. waitTask ( JSON![ " taskID " ] as Int , block: { ( JSON, error) -> Void in
377+ if let error = error {
378+ XCTFail ( " Error during waitTask " )
379+ expectation. fulfill ( )
380+ } else {
381+ XCTAssertEqual ( JSON![ " status " ] as String , " published " , " Wait task failed " )
382+
383+ self . index. browse ( page: 0 , block: { ( JSON, error) -> Void in
384+ if let error = error {
385+ XCTFail ( " Error during browse " )
386+ } else {
387+ let nbHits = JSON![ " nbHits " ] as Int
388+ XCTAssertEqual ( nbHits, 1 , " Wrong number of object in the index " )
389+ }
390+
391+ expectation. fulfill ( )
392+ } )
393+ }
394+ } )
395+ }
396+ } )
397+
398+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
399+ }
400+
401+ func testBrowseWithHitsPerPage( ) {
402+ let expectation = expectationWithDescription ( " testBrowseWithHitsPerPage " )
403+ let objects = [
404+ [ " city " : " San Francisco " , " objectID " : " a/go/?à " ] ,
405+ [ " city " : " New York " , " objectID " : " a/go/?à-1 " ]
406+ ]
407+
408+ index. addObjects ( objects, block: { ( JSON, error) -> Void in
409+ if let error = error {
410+ XCTFail ( " Error during addObjects " )
411+ expectation. fulfill ( )
412+ } else {
413+ self . index. waitTask ( JSON![ " taskID " ] as Int , block: { ( JSON, error) -> Void in
414+ if let error = error {
415+ XCTFail ( " Error during waitTask " )
416+ expectation. fulfill ( )
417+ } else {
418+ XCTAssertEqual ( JSON![ " status " ] as String , " published " , " Wait task failed " )
419+
420+ self . index. browse ( page: 0 , hitsPerPage: 1 , block: { ( JSON, error) -> Void in
421+ if let error = error {
422+ XCTFail ( " Error during browse " )
423+ } else {
424+ let nbPages = JSON![ " nbPages " ] as Int
425+ XCTAssertEqual ( nbPages, 2 , " Wrong number of page " )
426+ }
427+
428+ expectation. fulfill ( )
429+ } )
430+ }
431+ } )
432+ }
433+ } )
434+
435+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
436+ }
437+
438+ func testListIndexes( ) {
439+ let expectation = expectationWithDescription ( " testListIndexes " )
440+ let object = [ " city " : " San Francisco " , " objectID " : " a/go/?à " ]
441+
442+ index. addObject ( object, block: { ( JSON, error) -> Void in
443+ if let error = error {
444+ XCTFail ( " Error during addObject " )
445+ expectation. fulfill ( )
446+ } else {
447+ self . index. waitTask ( JSON![ " taskID " ] as Int , block: { ( JSON, error) -> Void in
448+ if let error = error {
449+ XCTFail ( " Error during waitTask " )
450+ expectation. fulfill ( )
451+ } else {
452+ XCTAssertEqual ( JSON![ " status " ] as String , " published " , " Wait task failed " )
453+
454+ self . client. listIndexes ( { ( JSON, error) -> Void in
455+ if let error = error {
456+ XCTFail ( " Error during listIndexes " )
457+ } else {
458+ let items = JSON![ " items " ] as [ [ String : AnyObject ] ]
459+
460+ var find = false
461+ for item in items {
462+ if ( item [ " name " ] as String ) == self . index. indexName {
463+ find = true
464+ }
465+ }
466+
467+ XCTAssertTrue ( find, " List indexes failed " )
468+ }
469+
470+ expectation. fulfill ( )
471+ } )
472+ }
473+ } )
474+ }
475+ } )
476+
477+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
478+ }
205479}
0 commit comments