Skip to content

Commit 538ab91

Browse files
committed
Implemented migrator tests
1 parent 308466c commit 538ab91

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

CoreDataMigration-Example.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
431DCEDF1F6842FF00CF6316 /* CoreDataMigration_Example_1.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 431DCEDA1F683E0100CF6316 /* CoreDataMigration_Example_1.sqlite */; };
2424
431DCEE01F68430200CF6316 /* CoreDataMigration_Example_2.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 431DCEDB1F683E0100CF6316 /* CoreDataMigration_Example_2.sqlite */; };
2525
431DCEE11F68430400CF6316 /* CoreDataMigration_Example_3.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 431DCEDC1F683E0100CF6316 /* CoreDataMigration_Example_3.sqlite */; };
26+
431DCEE31F6843F800CF6316 /* CoreDataMigratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 431DCEE21F6843F800CF6316 /* CoreDataMigratorTests.swift */; };
27+
431DCEE61F68479700CF6316 /* CoreDataMigration_Example_WAL.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 431DCEE51F68474200CF6316 /* CoreDataMigration_Example_WAL.sqlite */; };
28+
431DCEE71F68479A00CF6316 /* CoreDataMigration_Example_WAL.sqlite-wal in Resources */ = {isa = PBXBuildFile; fileRef = 431DCEE41F68474200CF6316 /* CoreDataMigration_Example_WAL.sqlite-wal */; };
2629
43370DB01F66E7A6006188EC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43370D751F66E74A006188EC /* AppDelegate.swift */; };
2730
43370DB31F66E830006188EC /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 43370D781F66E74A006188EC /* LaunchScreen.storyboard */; };
2831
43370DB41F66E832006188EC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 43370D7A1F66E74A006188EC /* Main.storyboard */; };
@@ -62,6 +65,9 @@
6265
431DCEDB1F683E0100CF6316 /* CoreDataMigration_Example_2.sqlite */ = {isa = PBXFileReference; lastKnownFileType = file; path = CoreDataMigration_Example_2.sqlite; sourceTree = "<group>"; };
6366
431DCEDC1F683E0100CF6316 /* CoreDataMigration_Example_3.sqlite */ = {isa = PBXFileReference; lastKnownFileType = file; path = CoreDataMigration_Example_3.sqlite; sourceTree = "<group>"; };
6467
431DCEDD1F683E2B00CF6316 /* CoreDataMigrationVersionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataMigrationVersionTests.swift; sourceTree = "<group>"; };
68+
431DCEE21F6843F800CF6316 /* CoreDataMigratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataMigratorTests.swift; sourceTree = "<group>"; };
69+
431DCEE41F68474200CF6316 /* CoreDataMigration_Example_WAL.sqlite-wal */ = {isa = PBXFileReference; lastKnownFileType = file; path = "CoreDataMigration_Example_WAL.sqlite-wal"; sourceTree = "<group>"; };
70+
431DCEE51F68474200CF6316 /* CoreDataMigration_Example_WAL.sqlite */ = {isa = PBXFileReference; lastKnownFileType = file; path = CoreDataMigration_Example_WAL.sqlite; sourceTree = "<group>"; };
6571
43370D731F66E74A006188EC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
6672
43370D751F66E74A006188EC /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
6773
43370D761F66E74A006188EC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -153,6 +159,7 @@
153159
children = (
154160
431DCED91F683E0100CF6316 /* Models */,
155161
431DCEDD1F683E2B00CF6316 /* CoreDataMigrationVersionTests.swift */,
162+
431DCEE21F6843F800CF6316 /* CoreDataMigratorTests.swift */,
156163
);
157164
path = Migration;
158165
sourceTree = "<group>";
@@ -163,6 +170,8 @@
163170
431DCEDA1F683E0100CF6316 /* CoreDataMigration_Example_1.sqlite */,
164171
431DCEDB1F683E0100CF6316 /* CoreDataMigration_Example_2.sqlite */,
165172
431DCEDC1F683E0100CF6316 /* CoreDataMigration_Example_3.sqlite */,
173+
431DCEE51F68474200CF6316 /* CoreDataMigration_Example_WAL.sqlite */,
174+
431DCEE41F68474200CF6316 /* CoreDataMigration_Example_WAL.sqlite-wal */,
166175
);
167176
path = Models;
168177
sourceTree = "<group>";
@@ -382,8 +391,10 @@
382391
buildActionMask = 2147483647;
383392
files = (
384393
431DCEE11F68430400CF6316 /* CoreDataMigration_Example_3.sqlite in Resources */,
394+
431DCEE71F68479A00CF6316 /* CoreDataMigration_Example_WAL.sqlite-wal in Resources */,
385395
431DCEDF1F6842FF00CF6316 /* CoreDataMigration_Example_1.sqlite in Resources */,
386396
431DCEE01F68430200CF6316 /* CoreDataMigration_Example_2.sqlite in Resources */,
397+
431DCEE61F68479700CF6316 /* CoreDataMigration_Example_WAL.sqlite in Resources */,
387398
);
388399
runOnlyForDeploymentPostprocessing = 0;
389400
};
@@ -418,6 +429,7 @@
418429
buildActionMask = 2147483647;
419430
files = (
420431
431DCEDE1F683E2B00CF6316 /* CoreDataMigrationVersionTests.swift in Sources */,
432+
431DCEE31F6843F800CF6316 /* CoreDataMigratorTests.swift in Sources */,
421433
);
422434
runOnlyForDeploymentPostprocessing = 0;
423435
};

CoreDataMigration-ExampleTests/Migration/CoreDataMigrationVersionTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
import XCTest
1010
import CoreData
11+
1112
@testable import CoreDataMigration_Example
1213

1314
class CoreDataMigrationVersionTests: XCTestCase {
1415

16+
// MARK: - CustomClasses
17+
1518
class CoreDataMigrationModelSpy: CoreDataMigrationModel {
1619

1720
var inferredMappingModelWasCalled = false
@@ -30,6 +33,8 @@ class CoreDataMigrationVersionTests: XCTestCase {
3033
}
3134
}
3235

36+
// MARK: - Setup
37+
3338
override func setUp() {
3439
super.setUp()
3540
// Put setup code here. This method is called before the invocation of each test method in the class.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//
2+
// CoreDataMigratorTests.swift
3+
// CoreDataMigration-ExampleTests
4+
//
5+
// Created by William Boles on 12/09/2017.
6+
// Copyright © 2017 William Boles. All rights reserved.
7+
//
8+
9+
import XCTest
10+
import CoreData
11+
12+
@testable import CoreDataMigration_Example
13+
14+
class CoreDataMigratorTests: XCTestCase {
15+
16+
func clearTmpDirectoryContents() {
17+
let tmpDirectoryContents = try! FileManager.default.contentsOfDirectory(atPath: NSTemporaryDirectory())
18+
tmpDirectoryContents.forEach {
19+
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent($0)
20+
try! FileManager.default.removeItem(atPath: fileURL.path)
21+
}
22+
}
23+
24+
func moveFileFromBundleToTmpDirectory(fileName: String) -> URL {
25+
let destinationURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent(fileName)
26+
let bundleURL = Bundle(for: CoreDataMigratorTests.self).resourceURL!.appendingPathComponent(fileName)
27+
try! FileManager.default.copyItem(at: bundleURL, to: destinationURL)
28+
29+
return destinationURL
30+
}
31+
32+
// MARK: - Properties
33+
34+
var migrator: CoreDataMigrator!
35+
36+
// MARK: - Setup
37+
38+
override func setUp() {
39+
super.setUp()
40+
clearTmpDirectoryContents()
41+
migrator = CoreDataMigrator()
42+
}
43+
44+
override func tearDown() {
45+
clearTmpDirectoryContents()
46+
super.tearDown()
47+
}
48+
49+
// MARK: - SingleStepMigrations
50+
51+
func test_individualStepMigration_1to2() {
52+
let sourceURL = self.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
53+
let targetURL = sourceURL
54+
55+
let modifiedDateBeforeMigration = try! FileManager.default.attributesOfItem(atPath: sourceURL.path)[FileAttributeKey.modificationDate] as! Date
56+
57+
migrator.migrateStore(from: sourceURL, to: targetURL, targetVersion: CoreDataMigrationModel(version: .version2))
58+
59+
let modifiedDateAfterMigration = try! FileManager.default.attributesOfItem(atPath: targetURL.path)[FileAttributeKey.modificationDate] as! Date
60+
61+
XCTAssertTrue(FileManager.default.fileExists(atPath: targetURL.path))
62+
XCTAssertTrue(modifiedDateAfterMigration.timeIntervalSince(modifiedDateBeforeMigration) > 0)
63+
}
64+
65+
func test_individualStepMigration_2to3() {
66+
let sourceURL = self.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_2.sqlite")
67+
let targetURL = sourceURL
68+
69+
let modifiedDateBeforeMigration = try! FileManager.default.attributesOfItem(atPath: sourceURL.path)[FileAttributeKey.modificationDate] as! Date
70+
71+
migrator.migrateStore(from: sourceURL, to: targetURL, targetVersion: CoreDataMigrationModel(version: .version3))
72+
73+
let modifiedDateAfterMigration = try! FileManager.default.attributesOfItem(atPath: targetURL.path)[FileAttributeKey.modificationDate] as! Date
74+
75+
XCTAssertTrue(FileManager.default.fileExists(atPath: targetURL.path))
76+
XCTAssertTrue(modifiedDateAfterMigration.timeIntervalSince(modifiedDateBeforeMigration) > 0)
77+
}
78+
79+
func test_individualStepMigration_3to4() {
80+
let sourceURL = self.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_3.sqlite")
81+
let targetURL = sourceURL
82+
83+
let modifiedDateBeforeMigration = try! FileManager.default.attributesOfItem(atPath: sourceURL.path)[FileAttributeKey.modificationDate] as! Date
84+
85+
migrator.migrateStore(from: sourceURL, to: targetURL, targetVersion: CoreDataMigrationModel(version: .version4))
86+
87+
let modifiedDateAfterMigration = try! FileManager.default.attributesOfItem(atPath: targetURL.path)[FileAttributeKey.modificationDate] as! Date
88+
89+
XCTAssertTrue(FileManager.default.fileExists(atPath: targetURL.path))
90+
XCTAssertTrue(modifiedDateAfterMigration.timeIntervalSince(modifiedDateBeforeMigration) > 0)
91+
}
92+
93+
// MARK: - MultipleStepMigrations
94+
95+
func test_multipleStepMigration_1to4() {
96+
let sourceURL = self.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
97+
let targetURL = sourceURL
98+
99+
let modifiedDateBeforeMigration = try! FileManager.default.attributesOfItem(atPath: sourceURL.path)[FileAttributeKey.modificationDate] as! Date
100+
101+
migrator.migrateStore(from: sourceURL, to: targetURL, targetVersion: CoreDataMigrationModel(version: .version4))
102+
103+
let modifiedDateAfterMigration = try! FileManager.default.attributesOfItem(atPath: targetURL.path)[FileAttributeKey.modificationDate] as! Date
104+
105+
XCTAssertTrue(FileManager.default.fileExists(atPath: targetURL.path))
106+
XCTAssertTrue(modifiedDateAfterMigration.timeIntervalSince(modifiedDateBeforeMigration) > 0)
107+
}
108+
109+
// MARK: - MigrationRequired
110+
111+
func test_requiresMigration_true() {
112+
let storeLocation = self.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
113+
114+
let requiresMigration = migrator.requiresMigration(storeLocation: storeLocation)
115+
116+
XCTAssertTrue(requiresMigration)
117+
}
118+
119+
func test_requiresMigration_false() {
120+
let storeLocation = self.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_3.sqlite")
121+
let migrationModel = CoreDataMigrationModel(version: .version3)
122+
123+
let requiresMigration = migrator.requiresMigration(storeLocation: storeLocation, currentMigrationModel: migrationModel)
124+
125+
XCTAssertFalse(requiresMigration)
126+
}
127+
128+
// MARK: - CheckPointing
129+
130+
func test_forceWALTransactions_success() {
131+
let storeLocation = self.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_WAL.sqlite")
132+
let walLocation = self.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_WAL.sqlite-wal")
133+
134+
let sizeBeforeCheckPointing = try! FileManager.default.attributesOfItem(atPath: storeLocation.path)[FileAttributeKey.size] as! NSNumber
135+
136+
migrator.forceWALCheckpointing(storeLocation: storeLocation)
137+
138+
let sizeAfterCheckPointing = try! FileManager.default.attributesOfItem(atPath: storeLocation.path)[FileAttributeKey.size] as! NSNumber
139+
140+
XCTAssertFalse(FileManager.default.fileExists(atPath: walLocation.path))
141+
XCTAssertTrue(sizeAfterCheckPointing.doubleValue > sizeBeforeCheckPointing.doubleValue)
142+
}
143+
}

0 commit comments

Comments
 (0)