@@ -376,6 +376,46 @@ class IndexTests: OnlineTestCase {
376376
377377 self . waitForExpectations ( timeout: expectationTimeout, handler: nil )
378378 }
379+
380+ func testPartialUpdateObjectCreateIfNotExists( ) {
381+ let expectation = self . expectation ( description: " testPartialUpdateObject " )
382+ let objectID = " unknown "
383+
384+ // Partial update with `createIfNotExists=false` should not create object.
385+ self . index. partialUpdateObject ( [ " city " : " Los Angeles " ] , withID: objectID, createIfNotExists: false ) { ( content, error) -> Void in
386+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
387+ self . index. waitTask ( withID: content![ " taskID " ] as! Int ) { ( content, error) -> Void in
388+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
389+ self . index. getObject ( withID: objectID) { ( content, error) -> Void in
390+ guard let error = error else {
391+ XCTFail ( " The object should not have been created " )
392+ expectation. fulfill ( )
393+ return
394+ }
395+ XCTAssertEqual ( StatusCode . notFound. rawValue, ( error as NSError ) . code)
396+
397+ // Partial update with `createIfNotExists=true` should create object.
398+ self . index. partialUpdateObject ( [ " city " : " Los Angeles " ] , withID: objectID, createIfNotExists: true ) { ( content, error) -> Void in
399+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
400+ self . index. waitTask ( withID: content![ " taskID " ] as! Int ) { ( content, error) -> Void in
401+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
402+ self . index. getObject ( withID: objectID) { ( content, error) -> Void in
403+ guard error == nil else {
404+ XCTFail ( " The object should have been created ( \( error!. localizedDescription) ) " )
405+ expectation. fulfill ( )
406+ return
407+ }
408+ XCTAssertEqual ( content ? [ " objectID " ] as? String , objectID)
409+ XCTAssertEqual ( content ? [ " city " ] as? String , " Los Angeles " )
410+ expectation. fulfill ( )
411+ }
412+ }
413+ }
414+ }
415+ }
416+ }
417+ self . waitForExpectations ( timeout: expectationTimeout, handler: nil )
418+ }
379419
380420 func testPartialUpdateObjects( ) {
381421 let expectation = self . expectation ( description: " testPartialUpdateObjects " )
@@ -425,7 +465,53 @@ class IndexTests: OnlineTestCase {
425465
426466 self . waitForExpectations ( timeout: expectationTimeout, handler: nil )
427467 }
428-
468+
469+ func testPartialUpdateObjectsCreateIfNotExists( ) {
470+ let expectation = self . expectation ( description: #function)
471+ let objectUpdates : [ JSONObject ] = [
472+ [ " city " : " Paris " , " objectID " : " a/go/?à " ] ,
473+ [ " city " : " Strasbourg " , " objectID " : " a/go/?à$ " ]
474+ ]
475+ // Partial updates with `createIfNotExists=false` should not create objects.
476+ self . index. partialUpdateObjects ( objectUpdates, createIfNotExists: false ) { ( content, error) -> Void in
477+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
478+ self . index. waitTask ( withID: content![ " taskID " ] as! Int ) { ( content, error) -> Void in
479+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
480+ self . index. getObjects ( withIDs: [ " a/go/?à " , " a/go/?à$ " ] ) { ( content, error) -> Void in
481+ // NOTE: Multiple get does not return an error, but simply returns `null` for missing objects.
482+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
483+ guard let results = content ? [ " results " ] as? [ Any ] else { XCTFail ( " Invalid results " ) ; expectation. fulfill ( ) ; return }
484+ XCTAssertEqual ( results. count, 2 )
485+ for i in 0 ..< 2 {
486+ XCTAssert ( results [ i] is NSNull )
487+ }
488+
489+ // Partial updates with `createIfNotExists=true` should create objects.
490+ self . index. partialUpdateObjects ( objectUpdates, createIfNotExists: true ) { ( content, error) -> Void in
491+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
492+ self . index. waitTask ( withID: content![ " taskID " ] as! Int ) { ( content, error) -> Void in
493+ guard error == nil else { XCTFail ( error!. localizedDescription) ; expectation. fulfill ( ) ; return }
494+ self . index. getObjects ( withIDs: [ " a/go/?à " , " a/go/?à$ " ] ) { ( content, error) -> Void in
495+ guard error == nil else {
496+ XCTFail ( " Objects should have been created ( \( error!. localizedDescription) ) " )
497+ expectation. fulfill ( )
498+ return
499+ }
500+ guard let results = content ? [ " results " ] as? [ JSONObject ] else { XCTFail ( " Invalid results " ) ; expectation. fulfill ( ) ; return }
501+ XCTAssertEqual ( results. count, 2 )
502+ for i in 0 ..< 2 {
503+ XCTAssertEqual ( results [ i] [ " objectID " ] as? String , objectUpdates [ i] [ " objectID " ] as? String )
504+ }
505+ expectation. fulfill ( )
506+ }
507+ }
508+ }
509+ }
510+ }
511+ }
512+ self . waitForExpectations ( timeout: expectationTimeout, handler: nil )
513+ }
514+
429515 func testSaveObject( ) {
430516 let expectation = self . expectation ( description: " testSaveObject " )
431517 let object : JSONObject = [ " city " : " New York " , " initial " : " NY " , " objectID " : " a/go/?à " ]
0 commit comments