@@ -76,35 +76,50 @@ class MirroredIndexTests: OfflineTestCase {
7676 super. tearDown ( )
7777 }
7878
79- private func sync ( index: MirroredIndex , completionBlock: @escaping ( Error ? ) -> Void ) {
79+ private func populate ( index: MirroredIndex , completionBlock: @escaping ( Error ? ) -> Void ) {
8080 // Delete the index.
8181 client. deleteIndex ( withName: index. name) { ( content, error) -> Void in
8282 XCTAssertNil ( error)
83+ if error != nil {
84+ completionBlock ( error)
85+ return
86+ }
8387 // Populate the online index.
8488 index. addObjects ( Array ( self . moreObjects. values) ) { ( content, error) in
8589 guard let taskID = content ? [ " taskID " ] as? Int else { XCTFail ( ) ; return }
8690 index. waitTask ( withID: taskID) { ( content, error) in
8791 XCTAssertNil ( error)
88-
89- // Sync the offline mirror.
90- index. mirrored = true
91- let query = Query ( )
92- query. numericFilters = [ " born < 1980 " ]
93- index. dataSelectionQueries = [
94- DataSelectionQuery ( query: query, maxObjects: 10 )
95- ]
96- var observer : NSObjectProtocol ?
97- observer = NotificationCenter . default. addObserver ( forName: MirroredIndex . SyncDidFinishNotification, object: index, queue: OperationQueue . main) { ( notification) in
98- NotificationCenter . default. removeObserver ( observer!)
99- let error = notification. userInfo ? [ MirroredIndex . errorKey] as? Error
100- completionBlock ( error)
101- }
102- index. sync ( )
92+ completionBlock ( error)
10393 }
10494 }
10595 }
10696 }
10797
98+ private func sync( index: MirroredIndex , completionBlock: @escaping ( Error ? ) -> Void ) {
99+ populate ( index: index) { ( error) -> Void in
100+ XCTAssertNil ( error)
101+ if error != nil {
102+ completionBlock ( error)
103+ return
104+ }
105+
106+ // Sync the offline mirror.
107+ index. mirrored = true
108+ let query = Query ( )
109+ query. numericFilters = [ " born < 1980 " ]
110+ index. dataSelectionQueries = [
111+ DataSelectionQuery ( query: query, maxObjects: 10 )
112+ ]
113+ var observer : NSObjectProtocol ?
114+ observer = NotificationCenter . default. addObserver ( forName: MirroredIndex . SyncDidFinishNotification, object: index, queue: OperationQueue . main) { ( notification) in
115+ NotificationCenter . default. removeObserver ( observer!)
116+ let error = notification. userInfo ? [ MirroredIndex . errorKey] as? Error
117+ completionBlock ( error)
118+ }
119+ index. sync ( )
120+ }
121+ }
122+
108123 func testSync( ) {
109124 let expectation_indexing = self . expectation ( description: " indexing " )
110125 let waitTimeout = 5.0
@@ -526,4 +541,49 @@ class MirroredIndexTests: OfflineTestCase {
526541 }
527542 waitForExpectations ( timeout: onlineExpectationTimeout, handler: nil )
528543 }
544+
545+ /// Test that a non-mirrored index behaves like a purely online index.
546+ ///
547+ func testNotMirrored( ) {
548+ let expectation_populate = self . expectation ( description: #function + " (populate) " )
549+ let expectation_search = self . expectation ( description: #function + " (search) " )
550+ let expectation_browse = self . expectation ( description: #function + " (browse) " )
551+ let expectation_get_object = self . expectation ( description: #function + " (get object) " )
552+ let expectation_get_objects = self . expectation ( description: #function + " (get objects) " )
553+
554+ let index : MirroredIndex = client. index ( withName: safeIndexName ( #function) )
555+ // Check that the index is *not* mirrored by default.
556+ XCTAssertFalse ( index. mirrored)
557+
558+ populate ( index: index) { ( error) in
559+ // Check that a non-mirrored index returns online results without origin tagging.
560+ index. search ( Query ( ) ) { ( content, error) in
561+ XCTAssertNil ( error)
562+ XCTAssertEqual ( 5 , content ? [ " nbHits " ] as? Int )
563+ XCTAssertNil ( content ? [ " origin " ] )
564+ expectation_search. fulfill ( )
565+ }
566+ index. browse ( query: Query ( ) ) { ( content, error) in
567+ XCTAssertNil ( error)
568+ XCTAssertEqual ( 5 , content ? [ " nbHits " ] as? Int )
569+ XCTAssertNil ( content ? [ " origin " ] )
570+ expectation_browse. fulfill ( )
571+ }
572+ index. getObject ( withID: " 1 " ) { ( content, error) in
573+ XCTAssertNil ( error)
574+ XCTAssertEqual ( " Snoopy " , content ? [ " name " ] as? String )
575+ XCTAssertNil ( content ? [ " origin " ] )
576+ expectation_get_object. fulfill ( )
577+ }
578+ index. getObjects ( withIDs: [ " 1 " , " 2 " ] ) { ( content, error) in
579+ XCTAssertNil ( error)
580+ XCTAssertEqual ( 2 , ( content ? [ " results " ] as? [ JSONObject ] ) ? . count)
581+ XCTAssertNil ( content ? [ " origin " ] )
582+ expectation_get_objects. fulfill ( )
583+ }
584+ expectation_populate. fulfill ( )
585+ }
586+
587+ waitForExpectations ( timeout: onlineExpectationTimeout, handler: nil )
588+ }
529589}
0 commit comments