From bac07433846678dee92ebe60e0551da84f58efcd Mon Sep 17 00:00:00 2001 From: Dilip Parmar Date: Fri, 17 Apr 2020 17:40:13 +0530 Subject: [PATCH] Added test cases for rollback and reset context --- CoreDataWrapper.xcodeproj/project.pbxproj | 4 + .../CoreDataWrapperTest.swift | 82 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 CoreDataWrapper_iOSTests/CoreDataWrapperTest.swift diff --git a/CoreDataWrapper.xcodeproj/project.pbxproj b/CoreDataWrapper.xcodeproj/project.pbxproj index 22467e9..b40ccc6 100644 --- a/CoreDataWrapper.xcodeproj/project.pbxproj +++ b/CoreDataWrapper.xcodeproj/project.pbxproj @@ -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 */ @@ -68,6 +69,7 @@ EE7E8B0824496A5300A86153 /* PersonalProtectionTests.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = PersonalProtectionTests.xcdatamodel; sourceTree = ""; }; EE7E8B0E24496B4F00A86153 /* Car+CoreDataProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Car+CoreDataProperties.swift"; sourceTree = ""; }; EE7E8B0F24496B4F00A86153 /* Car+CoreDataClass.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Car+CoreDataClass.swift"; sourceTree = ""; }; + EE9D819D2449B4FB00DD483A /* CoreDataWrapperTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataWrapperTest.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -148,6 +150,7 @@ EE7E8AF32449681500A86153 /* CoreDataWrapper_iOSTests */ = { isa = PBXGroup; children = ( + EE9D819D2449B4FB00DD483A /* CoreDataWrapperTest.swift */, EE7E8AFD2449687200A86153 /* AsyncOperationsTests.swift */, EE7E8AFE2449687200A86153 /* SyncOperationsTests.swift */, EE7E8AF62449681500A86153 /* Info.plist */, @@ -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 */, diff --git a/CoreDataWrapper_iOSTests/CoreDataWrapperTest.swift b/CoreDataWrapper_iOSTests/CoreDataWrapperTest.swift new file mode 100644 index 0000000..38854f6 --- /dev/null +++ b/CoreDataWrapper_iOSTests/CoreDataWrapperTest.swift @@ -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) + } +}