|
| 1 | +//MIT License |
| 2 | +// |
| 3 | +//Copyright (c) 2019 Dilip-Parmar |
| 4 | +// |
| 5 | +//Permission is hereby granted, free of charge, to any Car obtaining a copy |
| 6 | +//of this software and associated documentation files (the "Software"), to deal |
| 7 | +//in the Software without restriction, including without limitation the rights |
| 8 | +//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +//copies of the Software, and to permit Cars to whom the Software is |
| 10 | +//furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +//The above copyright notice and this permission notice shall be included in all |
| 13 | +//copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +//SOFTWARE. |
| 22 | +@testable import CoreDataWrapper_iOS |
| 23 | +import XCTest |
| 24 | + |
| 25 | +class CoreDataWrapperTest: XCTestCase { |
| 26 | + |
| 27 | + override func setUp() { |
| 28 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 29 | + } |
| 30 | + |
| 31 | + override func tearDown() { |
| 32 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 33 | + } |
| 34 | + |
| 35 | + func testResetChangesInContext() { |
| 36 | + let coreDataWrapper = CoreDataWrapper.init(modelFileName: "CoreDataWrapper", |
| 37 | + databaseFileName: "CoreDataWrapper", |
| 38 | + bundle: Bundle(for: SyncOperationsTests.self), |
| 39 | + storeType: .inMemory) |
| 40 | + XCTAssertNotNil(coreDataWrapper) |
| 41 | + |
| 42 | + let car = coreDataWrapper.addOf(type: Car.self) |
| 43 | + XCTAssertNotNil(car) |
| 44 | + |
| 45 | + let expectation = XCTestExpectation.init(description: "\(#file)\(#line)") |
| 46 | + coreDataWrapper.resetChangesIn(context: coreDataWrapper.mainContext) { |
| 47 | + expectation.fulfill() |
| 48 | + } |
| 49 | + wait(for: [expectation], timeout: 1.0) |
| 50 | + let fetched = coreDataWrapper.fetchBy(objectId: car!.objectID) |
| 51 | + XCTAssertNil(fetched) |
| 52 | + } |
| 53 | + |
| 54 | + func testRollbackInContext() { |
| 55 | + let coreDataWrapper = CoreDataWrapper.init(modelFileName: "CoreDataWrapper", |
| 56 | + databaseFileName: "CoreDataWrapper", |
| 57 | + bundle: Bundle(for: SyncOperationsTests.self), |
| 58 | + storeType: .sqlite) |
| 59 | + XCTAssertNotNil(coreDataWrapper) |
| 60 | + |
| 61 | + let _ = coreDataWrapper.deleteAllOf(type: Car.self, shouldSave: true) |
| 62 | + |
| 63 | + let car = coreDataWrapper.addOf(type: Car.self) |
| 64 | + XCTAssertNotNil(car) |
| 65 | + |
| 66 | + coreDataWrapper.saveMainContext(isSync: true, completion: { (isSuccess) in |
| 67 | + XCTAssert(isSuccess) |
| 68 | + }) |
| 69 | + |
| 70 | + let car1 = coreDataWrapper.addOf(type: Car.self, properties: ["model": "dp", "regNo": 3], shouldSave: false) |
| 71 | + XCTAssertNotNil(car1) |
| 72 | + |
| 73 | + let expectation = XCTestExpectation.init(description: "\(#file)\(#line)") |
| 74 | + coreDataWrapper.revertChangesIn(context: coreDataWrapper.mainContext) { |
| 75 | + expectation.fulfill() |
| 76 | + } |
| 77 | + wait(for: [expectation], timeout: 1.0) |
| 78 | + |
| 79 | + let fetched = coreDataWrapper.fetchAllOf(type: Car.self) |
| 80 | + XCTAssertEqual(fetched?.count, 1) |
| 81 | + } |
| 82 | +} |
0 commit comments