2121//SOFTWARE.
2222@testable import CoreDataWrapper_iOS
2323import XCTest
24+ import CoreData
2425
2526class AsyncOperationBlockingTests : XCTestCase {
2627
28+ private var shouldInitializeCoreData : Bool = true
2729 private var coreDataWrapper : CoreDataWrapper !
2830 override func setUp( ) {
2931 // Put setup code here. This method is called before the invocation of each test method in the class.
30- self . coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
31- databaseFileName: " CoreDataWrapper " ,
32- bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
33- storeType: . inMemory)
34-
35- let loadExpectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
36- self . coreDataWrapper. loadStore { ( isSuccess, error) in
37- XCTAssert ( isSuccess)
38- XCTAssertNil ( error)
39- loadExpectation. fulfill ( )
32+ if shouldInitializeCoreData == true {
33+ self . coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
34+ databaseFileName: " CoreDataWrapper " ,
35+ bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
36+ storeType: . inMemory)
37+
38+ let loadExpectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
39+ self . coreDataWrapper. loadStore { ( isSuccess, error) in
40+ XCTAssert ( isSuccess)
41+ XCTAssertNil ( error)
42+ loadExpectation. fulfill ( )
43+ }
44+ wait ( for: [ loadExpectation] , timeout: 5.0 )
4045 }
41- wait ( for: [ loadExpectation] , timeout: 5.0 )
42- self . coreDataWrapper. deleteAllOf ( type: Car . self, shouldSave: true )
4346 }
4447
4548 override func tearDown( ) {
4649 // Put teardown code here. This method is called after the invocation of each test method in the class.
50+ self . coreDataWrapper. purgeStore ( )
4751 }
4852
4953 func testInitializationAsyncBlocking( ) {
50-
5154 XCTAssertNotNil ( coreDataWrapper)
5255 }
5356
5457 func testAddObjAsyncBlocking( ) {
55-
5658 XCTAssertNotNil ( coreDataWrapper)
5759 let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
5860 self . coreDataWrapper. addAsyncOf ( type: Car . self, context: self . coreDataWrapper!. bgContext, isBlocking: true ) { ( car) in
@@ -109,16 +111,35 @@ class AsyncOperationBlockingTests: XCTestCase {
109111 let car = self . coreDataWrapper. addOf ( type: Car . self, properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: true )
110112 XCTAssertNotNil ( car)
111113
114+ func managedObjectContextObjectsDidChange( notification: Notification ) -> Bool {
115+ guard let userInfo = notification. userInfo else { return false }
116+
117+ if let inserts = userInfo [ NSInsertedObjectsKey] as? Set < NSManagedObject > , inserts. count > 0 {
118+ return false
119+ }
120+ if let updates = userInfo [ NSUpdatedObjectsKey] as? Set < NSManagedObject > , updates. count > 0 {
121+ return false
122+ }
123+ if let deletes = userInfo [ NSDeletedObjectsKey] as? Set < NSManagedObject > , deletes. count > 0 {
124+ if let deleted = deletes. first as? Car {
125+ XCTAssertEqual ( deleted. model, " Audi " )
126+ XCTAssertEqual ( deleted. regNo, 30 )
127+ return true
128+ }
129+ return false
130+ }
131+ return false
132+ }
133+ let notificationExpectation = expectation ( forNotification: NSNotification . Name. NSManagedObjectContextObjectsDidChange,
134+ object: self . coreDataWrapper. bgContext,
135+ handler: managedObjectContextObjectsDidChange)
112136 let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
113137 self . coreDataWrapper. deleteAsyncBy ( objectId: car!. objectID, context: self . coreDataWrapper. bgContext, shouldSave: true , isBlocking: true , completion: { ( isDeleted) in
114138 XCTAssert ( isDeleted)
115139 expectation. fulfill ( )
116140 } , completionOnMainThread: false )
141+ wait ( for: [ notificationExpectation] , timeout: 1.0 )
117142 wait ( for: [ expectation] , timeout: 5.0 )
118-
119- let fetched = self . coreDataWrapper. fetchBy ( objectId: car!. objectID) as? Car
120- XCTAssertNil ( fetched? . model)
121- XCTAssertNil ( fetched? . regNo)
122143 }
123144
124145 func testFetchAllAsyncBlocking( ) {
@@ -172,26 +193,37 @@ class AsyncOperationBlockingTests: XCTestCase {
172193 }
173194
174195 func testUpdateObjAsyncBlocking( ) {
175-
196+ self . shouldInitializeCoreData = false
197+ let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
198+ databaseFileName: " CoreDataWrapper " ,
199+ bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
200+ storeType: . inMemory)
176201 XCTAssertNotNil ( coreDataWrapper)
177202
178- let car = self . coreDataWrapper. addOf ( type: Car . self, properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: true )
203+ let loadExpectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
204+ coreDataWrapper. loadStore { ( isSuccess, error) in
205+ XCTAssert ( isSuccess)
206+ XCTAssertNil ( error)
207+ loadExpectation. fulfill ( )
208+ }
209+ wait ( for: [ loadExpectation] , timeout: 5.0 )
210+
211+ let car = coreDataWrapper. addOf ( type: Car . self, properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: true )
179212 XCTAssertNotNil ( car)
180213
181214 let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
182215
183- self . coreDataWrapper. updateAsyncBy ( objectId: car!. objectID, context: self . coreDataWrapper! . bgContext, properties: [ " model " : " dp1 " , " regNo " : 40 ] , shouldSave: true , isBlocking: true , completion: { ( isUpdated) in
216+ coreDataWrapper. updateAsyncBy ( objectId: car!. objectID, context: coreDataWrapper. bgContext, properties: [ " model " : " dp1 " , " regNo " : 40 ] , shouldSave: true , isBlocking: true , completion: { ( isUpdated) in
184217
185218 XCTAssert ( isUpdated)
186-
187- XCTAssertEqual ( car? . model, " dp1 " )
188- XCTAssertEqual ( car? . regNo, 40 )
189-
190219 expectation. fulfill ( )
191220
192221 } , completionOnMainThread: false )
193222
194223 wait ( for: [ expectation] , timeout: 1.0 )
224+
225+
226+ coreDataWrapper. purgeStore ( )
195227 }
196228
197229 func testUpdateAllAsyncBlocking( ) {
@@ -209,8 +241,7 @@ class AsyncOperationBlockingTests: XCTestCase {
209241
210242 let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
211243
212- self . coreDataWrapper. updateAllAsyncOf ( type: Car . self, context: self . coreDataWrapper!. bgContext, properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: true , isBlocking: true , completion: { ( updated) in
213-
244+ self . coreDataWrapper. updateAllAsyncOf ( type: Car . self, context: self . coreDataWrapper!. newBgContext ( ) , properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: true , isBlocking: false , completion: { ( updated) in
214245 XCTAssert ( updated)
215246 expectation. fulfill ( )
216247 } , completionOnMainThread: false )
@@ -272,6 +303,7 @@ class AsyncOperationBlockingTests: XCTestCase {
272303 }
273304
274305 func testPerformOperationAsyncBlocking( ) {
306+ self . shouldInitializeCoreData = false
275307 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
276308 databaseFileName: " CoreDataWrapper " ,
277309 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
@@ -308,6 +340,7 @@ class AsyncOperationBlockingTests: XCTestCase {
308340 }
309341
310342 func testUpdateAllSqliteAsyncBlocking( ) {
343+ self . shouldInitializeCoreData = false
311344 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
312345 databaseFileName: " CoreDataWrapper " ,
313346 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
@@ -352,6 +385,7 @@ class AsyncOperationBlockingTests: XCTestCase {
352385 }
353386
354387 func testUpdateAllSqliteAsyncMainThreadBlocking( ) {
388+ self . shouldInitializeCoreData = false
355389 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
356390 databaseFileName: " CoreDataWrapper " ,
357391 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
@@ -376,26 +410,27 @@ class AsyncOperationBlockingTests: XCTestCase {
376410 XCTAssertNotNil ( car3)
377411
378412 let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
379- coreDataWrapper. updateAllAsyncOf ( type: Car . self, context: coreDataWrapper. bgContext, properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: false , isBlocking: true , completion: { ( updated) in
413+ coreDataWrapper. updateAllAsyncOf ( type: Car . self, context: coreDataWrapper. bgContext, properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: true , isBlocking: true , completion: { ( updated) in
380414
381415 XCTAssert ( updated)
382-
383- let fetched = coreDataWrapper. fetchAllOf ( type: Car . self, sortBy: [ " model " : true ] )
384- XCTAssertEqual ( fetched? . count, 3 )
385-
386- let filtered = fetched!. filter { ( car) -> Bool in
387- car. model == " Audi " && car. regNo == 30
388- }
389- XCTAssertEqual ( filtered. count, 3 )
390416 expectation. fulfill ( )
391417
392418 } , completionOnMainThread: true )
393419 wait ( for: [ expectation] , timeout: 1.0 )
420+
421+ let fetched = coreDataWrapper. fetchAllOf ( type: Car . self, sortBy: [ " model " : true ] )
422+ XCTAssertEqual ( fetched? . count, 3 )
423+
424+ let filtered = fetched!. filter { ( car) -> Bool in
425+ car. model == " Audi " && car. regNo == 30
426+ }
427+ XCTAssertEqual ( filtered. count, 3 )
394428
395429 coreDataWrapper. purgeStore ( )
396430 }
397431
398432 func testUpdateAllSqliteAsyncMainThreadBGContextBlocking( ) {
433+ self . shouldInitializeCoreData = false
399434 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
400435 databaseFileName: " CoreDataWrapper " ,
401436 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
@@ -437,6 +472,7 @@ class AsyncOperationBlockingTests: XCTestCase {
437472
438473
439474 func testDeleteAllSqliteAsyncBlocking( ) {
475+ self . shouldInitializeCoreData = false
440476 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
441477 databaseFileName: " CoreDataWrapper " ,
442478 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
@@ -724,19 +760,40 @@ class AsyncOperationBlockingTests: XCTestCase {
724760
725761 let car = self . coreDataWrapper. addOf ( type: Car . self, properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: true )
726762 XCTAssertNotNil ( car)
763+ XCTAssertEqual ( car!. regNo, 30 )
764+ XCTAssertEqual ( car!. model, " Audi " )
727765
728- let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
766+ func managedObjectContextObjectsDidChange( notification: Notification ) -> Bool {
767+ guard let userInfo = notification. userInfo else { return false }
768+
769+ if let inserts = userInfo [ NSInsertedObjectsKey] as? Set < NSManagedObject > , inserts. count > 0 {
770+ return false
771+ }
772+ if let updates = userInfo [ NSUpdatedObjectsKey] as? Set < NSManagedObject > , updates. count > 0 {
773+ if let carFound = updates. first as? Car {
774+ XCTAssertEqual ( carFound. model, " dp1 " )
775+ XCTAssertEqual ( carFound. regNo, 40 )
776+ return true
777+ }
778+ return false
779+ }
780+ if let deletes = userInfo [ NSDeletedObjectsKey] as? Set < NSManagedObject > , deletes. count > 0 {
781+ return false
782+ }
783+ return false
784+ }
729785 let context = self . coreDataWrapper. newBgContext ( )
786+ let notificationExpectation = expectation ( forNotification: NSNotification . Name. NSManagedObjectContextObjectsDidChange,
787+ object: context,
788+ handler: managedObjectContextObjectsDidChange)
789+
790+ let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
730791 self . coreDataWrapper. updateAsyncBy ( objectId: car!. objectID, context: context, properties: [ " model " : " dp1 " , " regNo " : 40 ] , shouldSave: true , isBlocking: true , completion: { ( isUpdated) in
731792 XCTAssert ( isUpdated)
732793 expectation. fulfill ( )
733-
734794 } , completionOnMainThread: false )
795+ wait ( for: [ notificationExpectation] , timeout: 1.0 )
735796 wait ( for: [ expectation] , timeout: 1.0 )
736-
737- let carFound = self . coreDataWrapper. fetchBy ( objectId: car!. objectID) as? Car
738- XCTAssertEqual ( carFound!. model, " dp1 " )
739- XCTAssertEqual ( carFound!. regNo, 40 )
740797 }
741798
742799 func testUpdateObjAsyncWidBGContextMainThreadBlocking( ) {
@@ -797,21 +854,33 @@ class AsyncOperationBlockingTests: XCTestCase {
797854 let car3 = self . coreDataWrapper. addOf ( type: Car . self, properties: [ " model " : " dp3 " , " regNo " : 40 ] , shouldSave: true )
798855 XCTAssertNotNil ( car3)
799856
800- let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
857+ func managedObjectContextObjectsDidChange( notification: Notification ) -> Bool {
858+ guard let userInfo = notification. userInfo else { return false }
859+
860+ if let inserts = userInfo [ NSInsertedObjectsKey] as? Set < NSManagedObject > , inserts. count > 0 {
861+ return false
862+ }
863+ if let updates = userInfo [ NSUpdatedObjectsKey] as? Set < NSManagedObject > , updates. count > 0 {
864+ XCTAssertEqual ( updates. count, 3 )
865+ return true
866+ }
867+ if let deletes = userInfo [ NSDeletedObjectsKey] as? Set < NSManagedObject > , deletes. count > 0 {
868+ return false
869+ }
870+ return false
871+ }
801872 let context = self . coreDataWrapper. newBgContext ( )
873+ let notificationExpectation = expectation ( forNotification: NSNotification . Name. NSManagedObjectContextObjectsDidChange,
874+ object: context,
875+ handler: managedObjectContextObjectsDidChange)
876+
877+ let expectation = XCTestExpectation . init ( description: " \( #file) \( #line) " )
802878 self . coreDataWrapper. updateAllAsyncOf ( type: Car . self, context: context, properties: [ " model " : " Audi " , " regNo " : 30 ] , shouldSave: true , isBlocking: true , completion: { ( updated) in
803879 XCTAssert ( updated)
804880 expectation. fulfill ( )
805881 } , completionOnMainThread: false )
882+ wait ( for: [ notificationExpectation] , timeout: 1.0 )
806883 wait ( for: [ expectation] , timeout: 1.0 )
807-
808- let fetched = self . coreDataWrapper. fetchAllOf ( type: Car . self, sortBy: [ " model " : true ] )
809- XCTAssertEqual ( fetched? . count, 3 )
810-
811- let filtered = fetched!. filter { ( car) -> Bool in
812- car. model == " Audi " && car. regNo == 30
813- }
814- XCTAssertEqual ( filtered. count, 3 )
815884 }
816885
817886 func testUpdateAllAsyncWidBGContextMainThreadBlocking( ) {
@@ -945,6 +1014,7 @@ class AsyncOperationBlockingTests: XCTestCase {
9451014 }
9461015
9471016 func testPerformOperationAsyncWidBGContextBlocking( ) {
1017+ self . shouldInitializeCoreData = false
9481018 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
9491019 databaseFileName: " CoreDataWrapper " ,
9501020 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
@@ -982,6 +1052,7 @@ class AsyncOperationBlockingTests: XCTestCase {
9821052 }
9831053
9841054 func testPerformOperationAsyncWidBGContextMainThreadBlocking( ) {
1055+ self . shouldInitializeCoreData = false
9851056 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
9861057 databaseFileName: " CoreDataWrapper " ,
9871058 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
@@ -1021,6 +1092,7 @@ class AsyncOperationBlockingTests: XCTestCase {
10211092 }
10221093
10231094 func testUpdateAllSqliteAsyncWidBGContextBlocking( ) {
1095+ self . shouldInitializeCoreData = false
10241096 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
10251097 databaseFileName: " CoreDataWrapper " ,
10261098 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
@@ -1066,6 +1138,7 @@ class AsyncOperationBlockingTests: XCTestCase {
10661138 }
10671139
10681140 func testDeleteAllSqliteAsyncWidBGContextBlocking( ) {
1141+ self . shouldInitializeCoreData = false
10691142 let coreDataWrapper = CoreDataWrapper . init ( modelFileName: " CoreDataWrapper " ,
10701143 databaseFileName: " CoreDataWrapper " ,
10711144 bundle: Bundle ( for: AsyncOperationBlockingTests . self) ,
0 commit comments