Skip to content

Commit 2dc72dd

Browse files
committed
Updated unit tests to tear down Core Data stack without warnings
1 parent 116a97b commit 2dc72dd

File tree

6 files changed

+37
-31
lines changed

6 files changed

+37
-31
lines changed

CoreDataMigration-Example/CoreData/Model/CoreDataMigration_Example.xcdatamodeld/CoreDataMigration_Example 4.xcdatamodel/contents

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</entity>
88
<entity name="Post" representedClassName="Post" syncable="YES" codeGenerationType="class">
99
<attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
10-
<attribute name="hidden" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/>
1110
<attribute name="postID" optional="YES" attributeType="String" syncable="YES"/>
11+
<attribute name="softDeleted" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/>
1212
<relationship name="body" maxCount="1" deletionRule="Nullify" destinationEntity="Content" syncable="YES"/>
1313
<relationship name="title" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Content" syncable="YES"/>
1414
</entity>

CoreDataMigration-Example/ViewControllers/Posts/PostsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PostsViewController: UITableViewController {
5454
let context = CoreDataManager.shared.mainContext
5555
let request = NSFetchRequest<Post>.init(entityName: "Post")
5656
let dateSort = NSSortDescriptor(key: "date", ascending: false)
57-
let predicate = NSPredicate(format: "hidden == NO")
57+
let predicate = NSPredicate(format: "softDeleted == NO")
5858

5959
request.sortDescriptors = [dateSort]
6060
request.predicate = predicate

CoreDataMigration-Example/ViewControllers/Viewer/PostViewerViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class PostViewerViewController: UIViewController {
4747
request.predicate = NSPredicate(format: "postID == '\(self.viewModel.postID)'")
4848

4949
let post = try! context.fetch(request).first!
50-
post.hidden = true
50+
post.softDeleted = true
5151

5252
try? context.save()
5353

CoreDataMigration-ExampleTests/Helpers/FileManager+Helper.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,11 @@ extension FileManager {
1212

1313
// MARK: - Temp
1414

15-
static func clearTmpDirectoryContents() {
16-
let tmpDirectoryContents = try! FileManager.default.contentsOfDirectory(atPath: NSTemporaryDirectory())
17-
tmpDirectoryContents.forEach {
18-
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent($0)
19-
try! FileManager.default.removeItem(atPath: fileURL.path)
20-
}
21-
}
22-
23-
static func moveFileFromBundleToTmpDirectory(fileName: String) -> URL {
24-
let destinationURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent(fileName)
25-
let bundleURL = Bundle(for: CoreDataMigratorTests.self).resourceURL!.appendingPathComponent(fileName)
26-
try! FileManager.default.copyItem(at: bundleURL, to: destinationURL)
15+
static func moveFileFromBundleToTempDirectory(filename: String) -> URL {
16+
let destinationURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent(filename)
17+
try? FileManager.default.removeItem(at: destinationURL)
18+
let bundleURL = Bundle(for: CoreDataMigratorTests.self).resourceURL!.appendingPathComponent(filename)
19+
try? FileManager.default.copyItem(at: bundleURL, to: destinationURL)
2720

2821
return destinationURL
2922
}

CoreDataMigration-ExampleTests/Helpers/NSManagedObjectContext+Helper.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ extension NSManagedObjectContext {
2121

2222
self.persistentStoreCoordinator = persistentStoreCoordinator
2323
}
24+
25+
// MARK: - Destroy
26+
27+
func destroyStore() {
28+
persistentStoreCoordinator?.persistentStores.forEach {
29+
try? persistentStoreCoordinator?.remove($0)
30+
try? persistentStoreCoordinator?.destroyPersistentStore(at: $0.url!, ofType: $0.type, options: nil)
31+
}
32+
}
2433
}

CoreDataMigration-ExampleTests/Tests/CoreData/Migration/CoreDataMigratorTests.swift

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,28 @@ class CoreDataMigratorTests: XCTestCase {
1717

1818
// MARK: - Lifecycle
1919

20-
override class func setUp() {
21-
super.setUp()
22-
23-
FileManager.clearTmpDirectoryContents()
24-
}
25-
2620
override func setUp() {
2721
super.setUp()
2822

2923
sut = CoreDataMigrator()
3024
}
3125

3226
override func tearDown() {
33-
FileManager.clearTmpDirectoryContents()
34-
3527
sut = nil
3628

3729
super.tearDown()
3830
}
3931

32+
func tearDownCoreDataStack(context: NSManagedObjectContext) {
33+
context.destroyStore()
34+
}
35+
4036
// MARK: - Tests
4137

4238
// MARK: SingleStepMigrations
4339

4440
func test_individualStepMigration_1to2() {
45-
let sourceURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
41+
let sourceURL = FileManager.moveFileFromBundleToTempDirectory(filename: "CoreDataMigration_Example_1.sqlite")
4642
let toVersion = CoreDataMigrationVersion.version2
4743

4844
sut.migrateStore(at: sourceURL, toVersion: toVersion)
@@ -70,10 +66,12 @@ class CoreDataMigratorTests: XCTestCase {
7066
XCTAssertEqual(migratedHexColor, "1BB732")
7167
XCTAssertEqual(migratedPostID, "FFFECB21-6645-4FDD-B8B0-B960D0E61F5A")
7268
XCTAssertEqual(migratedContent, "Test body")
69+
70+
tearDownCoreDataStack(context: context)
7371
}
7472

7573
func test_individualStepMigration_2to3() {
76-
let sourceURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_2.sqlite")
74+
let sourceURL = FileManager.moveFileFromBundleToTempDirectory(filename: "CoreDataMigration_Example_2.sqlite")
7775
let toVersion = CoreDataMigrationVersion.version3
7876

7977
sut.migrateStore(at: sourceURL, toVersion: toVersion)
@@ -120,10 +118,12 @@ class CoreDataMigratorTests: XCTestCase {
120118
let migratedContents = try? context.fetch(contentRequest)
121119

122120
XCTAssertEqual(migratedContents?.count, 20)
121+
122+
tearDownCoreDataStack(context: context)
123123
}
124124

125125
func test_individualStepMigration_3to4() {
126-
let sourceURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_3.sqlite")
126+
let sourceURL = FileManager.moveFileFromBundleToTempDirectory(filename: "CoreDataMigration_Example_3.sqlite")
127127
let toVersion = CoreDataMigrationVersion.version4
128128

129129
sut.migrateStore(at: sourceURL, toVersion: toVersion)
@@ -145,13 +145,13 @@ class CoreDataMigratorTests: XCTestCase {
145145

146146
let migratedDate = firstMigratedPost?.value(forKey: "date") as? Date
147147
let migratedPostID = firstMigratedPost?.value(forKey: "postID") as? String
148-
let migratedHidden = firstMigratedPost?.value(forKey: "hidden") as? Bool
148+
let migratedSoftDeleted = firstMigratedPost?.value(forKey: "softDeleted") as? Bool
149149
let migratedTitleContent = firstMigratedPost?.value(forKey: "title") as? NSManagedObject
150150
let migratedBodyContent = firstMigratedPost?.value(forKey: "body") as? NSManagedObject
151151

152152
XCTAssertEqual(migratedDate?.timeIntervalSince1970, 1547494150.058821)
153153
XCTAssertEqual(migratedPostID, "FFFECB21-6645-4FDD-B8B0-B960D0E61F5A")
154-
XCTAssertFalse(migratedHidden ?? true)
154+
XCTAssertFalse(migratedSoftDeleted ?? true)
155155
XCTAssertNotNil(migratedTitleContent)
156156
XCTAssertNotNil(migratedBodyContent)
157157

@@ -172,12 +172,14 @@ class CoreDataMigratorTests: XCTestCase {
172172
let migratedContents = try? context.fetch(contentRequest)
173173

174174
XCTAssertEqual(migratedContents?.count, 20)
175+
176+
tearDownCoreDataStack(context: context)
175177
}
176178

177179
// MARK: MultipleStepMigrations
178180

179181
func test_multipleStepMigration_fromVersion1toVersion4() {
180-
let sourceURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
182+
let sourceURL = FileManager.moveFileFromBundleToTempDirectory(filename: "CoreDataMigration_Example_1.sqlite")
181183
let toVersion = CoreDataMigrationVersion.version4
182184

183185
sut.migrateStore(at: sourceURL, toVersion: toVersion)
@@ -195,20 +197,22 @@ class CoreDataMigratorTests: XCTestCase {
195197

196198
XCTAssertEqual(migratedPosts?.count, 10)
197199
XCTAssertEqual(migratedColors?.count, 20)
200+
201+
tearDownCoreDataStack(context: context)
198202
}
199203

200204
// MARK: MigrationRequired
201205

202206
func test_requiresMigration_fromVersion1ToCurrent_true() {
203-
let storeURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
207+
let storeURL = FileManager.moveFileFromBundleToTempDirectory(filename: "CoreDataMigration_Example_1.sqlite")
204208

205209
let requiresMigration = sut.requiresMigration(at: storeURL, toVersion: CoreDataMigrationVersion.latest)
206210

207211
XCTAssertTrue(requiresMigration)
208212
}
209213

210214
func test_requiresMigration_fromVersion3ToVersion3_false() {
211-
let storeURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_3.sqlite")
215+
let storeURL = FileManager.moveFileFromBundleToTempDirectory(filename: "CoreDataMigration_Example_3.sqlite")
212216

213217
let requiresMigration = sut.requiresMigration(at: storeURL, toVersion: .version3)
214218

0 commit comments

Comments
 (0)