Skip to content

Commit

Permalink
Added test cases for rollback and reset context
Browse files Browse the repository at this point in the history
  • Loading branch information
Dilip Parmar committed Apr 17, 2020
1 parent 32972b0 commit bac0743
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CoreDataWrapper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
EE7E8B1724496B4F00A86153 /* Car+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7E8B0F24496B4F00A86153 /* Car+CoreDataClass.swift */; };
EE7E8B1824496B4F00A86153 /* Car+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7E8B0F24496B4F00A86153 /* Car+CoreDataClass.swift */; };
EE7E8B1A24496F5500A86153 /* AsyncOperationsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7E8AFD2449687200A86153 /* AsyncOperationsTests.swift */; };
EE9D819E2449B4FB00DD483A /* CoreDataWrapperTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE9D819D2449B4FB00DD483A /* CoreDataWrapperTest.swift */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -148,6 +150,7 @@
EE7E8AF32449681500A86153 /* CoreDataWrapper_iOSTests */ = {
isa = PBXGroup;
children = (
EE9D819D2449B4FB00DD483A /* CoreDataWrapperTest.swift */,
EE7E8AFD2449687200A86153 /* AsyncOperationsTests.swift */,
EE7E8AFE2449687200A86153 /* SyncOperationsTests.swift */,
EE7E8AF62449681500A86153 /* Info.plist */,
Expand Down Expand Up @@ -445,6 +448,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
EE9D819E2449B4FB00DD483A /* CoreDataWrapperTest.swift in Sources */,
EE7E8B0D24496A5300A86153 /* CoreDataWrapper.xcdatamodeld in Sources */,
EE7E8B002449687200A86153 /* SyncOperationsTests.swift in Sources */,
EE7E8B1A24496F5500A86153 /* AsyncOperationsTests.swift in Sources */,
Expand Down
82 changes: 82 additions & 0 deletions CoreDataWrapper_iOSTests/CoreDataWrapperTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//MIT License
//
//Copyright (c) 2019 Dilip-Parmar
//
//Permission is hereby granted, free of charge, to any Car obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit Cars to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
@testable import CoreDataWrapper_iOS
import XCTest

class CoreDataWrapperTest: XCTestCase {

override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testResetChangesInContext() {
let coreDataWrapper = CoreDataWrapper.init(modelFileName: "CoreDataWrapper",
databaseFileName: "CoreDataWrapper",
bundle: Bundle(for: SyncOperationsTests.self),
storeType: .inMemory)
XCTAssertNotNil(coreDataWrapper)

let car = coreDataWrapper.addOf(type: Car.self)
XCTAssertNotNil(car)

let expectation = XCTestExpectation.init(description: "\(#file)\(#line)")
coreDataWrapper.resetChangesIn(context: coreDataWrapper.mainContext) {
expectation.fulfill()
}
wait(for: [expectation], timeout: 1.0)
let fetched = coreDataWrapper.fetchBy(objectId: car!.objectID)
XCTAssertNil(fetched)
}

func testRollbackInContext() {
let coreDataWrapper = CoreDataWrapper.init(modelFileName: "CoreDataWrapper",
databaseFileName: "CoreDataWrapper",
bundle: Bundle(for: SyncOperationsTests.self),
storeType: .sqlite)
XCTAssertNotNil(coreDataWrapper)

let _ = coreDataWrapper.deleteAllOf(type: Car.self, shouldSave: true)

let car = coreDataWrapper.addOf(type: Car.self)
XCTAssertNotNil(car)

coreDataWrapper.saveMainContext(isSync: true, completion: { (isSuccess) in
XCTAssert(isSuccess)
})

let car1 = coreDataWrapper.addOf(type: Car.self, properties: ["model": "dp", "regNo": 3], shouldSave: false)
XCTAssertNotNil(car1)

let expectation = XCTestExpectation.init(description: "\(#file)\(#line)")
coreDataWrapper.revertChangesIn(context: coreDataWrapper.mainContext) {
expectation.fulfill()
}
wait(for: [expectation], timeout: 1.0)

let fetched = coreDataWrapper.fetchAllOf(type: Car.self)
XCTAssertEqual(fetched?.count, 1)
}
}

0 comments on commit bac0743

Please # to comment.