Last active
October 19, 2019 05:59
-
-
Save Monntay/e56c555c8f5910cf8ad154bb93e77722 to your computer and use it in GitHub Desktop.
CoreData Heavy Migration Test 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // we will test the migration from version 1 to 2. the names of a person changed. | |
| func testHeavyWeightMigration() { | |
| // MARK: 1 read and load the old model | |
| let oldModelURL = Bundle(for: AppDelegate.self).url(forResource: "CoreDataMigrationGuide.momd/CoreDataMigrationGuide 3", withExtension: "mom")! | |
| let oldManagedObjectModel = NSManagedObjectModel(contentsOf: oldModelURL) | |
| XCTAssertNotNil(oldManagedObjectModel) | |
| let coordinator = NSPersistentStoreCoordinator(managedObjectModel: oldManagedObjectModel!) | |
| try! coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil) | |
| let managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) | |
| managedObjectContext.persistentStoreCoordinator = coordinator | |
| // MARK: 2 - adding a person to the old db | |
| let personTeacher = NSEntityDescription.insertNewObject(forEntityName: "Teacher", into: managedObjectContext) | |
| personTeacher.setValue("lastname_", forKey: "lastName") | |
| personTeacher.setValue("firstname_", forKey: "firstName") | |
| personTeacher.setValue("44", forKey: "age") | |
| let personStudent = NSEntityDescription.insertNewObject(forEntityName: "Student", into: managedObjectContext) | |
| personStudent.setValue("lastname_", forKey: "lastName") | |
| personStudent.setValue("firstname_", forKey: "firstName") | |
| personStudent.setValue("8", forKey: "age") | |
| try! managedObjectContext.save() | |
| // MARK: 3 - migrate the store to the new model version | |
| let newModelURL = Bundle(for: AppDelegate.self).url(forResource: "CoreDataMigrationGuide.momd/CoreDataMigrationGuide 4", withExtension: "mom")! | |
| let newManagedObjectModel = NSManagedObjectModel(contentsOf: newModelURL) | |
| let mappingModel = NSMappingModel(from: nil, forSourceModel: oldManagedObjectModel, destinationModel: newManagedObjectModel) | |
| let migrationManager = NSMigrationManager(sourceModel: oldManagedObjectModel!, destinationModel: newManagedObjectModel!) | |
| try! migrationManager.migrateStore(from: self.url, sourceType: NSSQLiteStoreType, options: nil, with: mappingModel, toDestinationURL: self.newUrl, destinationType: NSSQLiteStoreType, destinationOptions: nil) | |
| let newCoordinbator = NSPersistentStoreCoordinator(managedObjectModel: newManagedObjectModel!) | |
| try! newCoordinbator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: self.newUrl, options: nil) | |
| let newManagedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) | |
| newManagedObjectContext.persistentStoreCoordinator = newCoordinbator | |
| // MARK: 4 - test the migration | |
| let newStudentRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Student") | |
| let newStudent = try! newManagedObjectContext.fetch(newStudentRequest) as! [NSManagedObject] | |
| XCTAssertEqual(newStudent.count, 1) | |
| XCTAssertEqual(newStudent.first?.value(forKey: "firstName") as? String, "firstname_") | |
| XCTAssertEqual(newStudent.first?.value(forKey: "lastName") as? String, "lastname_") | |
| XCTAssertEqual(newStudent.first?.value(forKey: "age") as? String, "8") | |
| XCTAssertEqual(newStudent.first?.value(forKey: "schoolClass") as? String, "3a") | |
| let newTeacherRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Teacher") | |
| let newTeacher = try! newManagedObjectContext.fetch(newTeacherRequest) as! [NSManagedObject] | |
| XCTAssertEqual(newTeacher.count, 1) | |
| XCTAssertEqual(newTeacher.first?.value(forKey: "firstName") as? String, "firstname_") | |
| XCTAssertEqual(newTeacher.first?.value(forKey: "lastName") as? String, "lastname_") | |
| XCTAssertEqual(newTeacher.first?.value(forKey: "age") as? String, "44") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment