|
| 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