Skip to content

Commit bac0743

Browse files
author
Dilip Parmar
committed
Added test cases for rollback and reset context
1 parent 32972b0 commit bac0743

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

CoreDataWrapper.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
EE7E8B1724496B4F00A86153 /* Car+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7E8B0F24496B4F00A86153 /* Car+CoreDataClass.swift */; };
4040
EE7E8B1824496B4F00A86153 /* Car+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7E8B0F24496B4F00A86153 /* Car+CoreDataClass.swift */; };
4141
EE7E8B1A24496F5500A86153 /* AsyncOperationsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7E8AFD2449687200A86153 /* AsyncOperationsTests.swift */; };
42+
EE9D819E2449B4FB00DD483A /* CoreDataWrapperTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE9D819D2449B4FB00DD483A /* CoreDataWrapperTest.swift */; };
4243
/* End PBXBuildFile section */
4344

4445
/* Begin PBXContainerItemProxy section */
@@ -68,6 +69,7 @@
6869
EE7E8B0824496A5300A86153 /* PersonalProtectionTests.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = PersonalProtectionTests.xcdatamodel; sourceTree = "<group>"; };
6970
EE7E8B0E24496B4F00A86153 /* Car+CoreDataProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Car+CoreDataProperties.swift"; sourceTree = "<group>"; };
7071
EE7E8B0F24496B4F00A86153 /* Car+CoreDataClass.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Car+CoreDataClass.swift"; sourceTree = "<group>"; };
72+
EE9D819D2449B4FB00DD483A /* CoreDataWrapperTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataWrapperTest.swift; sourceTree = "<group>"; };
7173
/* End PBXFileReference section */
7274

7375
/* Begin PBXFrameworksBuildPhase section */
@@ -148,6 +150,7 @@
148150
EE7E8AF32449681500A86153 /* CoreDataWrapper_iOSTests */ = {
149151
isa = PBXGroup;
150152
children = (
153+
EE9D819D2449B4FB00DD483A /* CoreDataWrapperTest.swift */,
151154
EE7E8AFD2449687200A86153 /* AsyncOperationsTests.swift */,
152155
EE7E8AFE2449687200A86153 /* SyncOperationsTests.swift */,
153156
EE7E8AF62449681500A86153 /* Info.plist */,
@@ -445,6 +448,7 @@
445448
isa = PBXSourcesBuildPhase;
446449
buildActionMask = 2147483647;
447450
files = (
451+
EE9D819E2449B4FB00DD483A /* CoreDataWrapperTest.swift in Sources */,
448452
EE7E8B0D24496A5300A86153 /* CoreDataWrapper.xcdatamodeld in Sources */,
449453
EE7E8B002449687200A86153 /* SyncOperationsTests.swift in Sources */,
450454
EE7E8B1A24496F5500A86153 /* AsyncOperationsTests.swift in Sources */,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)