Search code examples
iosobjective-cunit-testingcore-data-migration

Generate Data of Previous Version of xcdatamodel for Core Data Migration Testing


In our existing app, we have many different version of xcdatamodel:

+ TheApp.xcdatamodel
   TheApp.1.0.xcdatamodel
   TheApp.1.1.xcdatamodel
   TheApp.2.0.xcdatamodel
   ...

We know that the traditional way of doing database migration testing is what have been proposed in this question: How to Test Core Data Migration With an App Already in the App Store? In short, it works like the following:

  1. install a old version of the app;
  2. create some data in the old version of the app;
  3. install the new version on top it;
  4. see if everything is migrated properly.

We have been using this migration testing method for all our previous version of the app. Our QAs will perform the above steps and then judge by themselves that whether migration is successful or not.

However, in the most recent upgrade, we have changed a lot in our data model. It doesn't sound like such a good idea to ask the QAs to remember what have been created in the old version of the app and know what have been missing or not during migration. Therefore, we would like to see whether it is possible to write unit testing for the database migration by the developers ourself.

So one of the first step is to generate test data. Notice that we can see all the previous version of the xcdatamodel from within our Xcode project, it seems that it is possible. In a nutshell, the question:

Is it possible to generate test data of previous version of xcdatamodel programmatically from within our current version of the app?

Please let me know what you think. Suggestions are also acceptable.


Solution

  • This is how we do it: we need to first get an URL to any model you would like to work with, and then create a managedObjectModel from it.

    let oldModelUrl = NSBundle.mainBundle().URLForResource("CoreDataExample.momd/CoreDataExample",
    withExtension: "mom")!
    let oldManagedObjectModel = NSManagedObjectModel.init(contentsOfURL: oldModelUrl)
    

    Referece: https://medium.com/@yzhong.cs/1d9f941b3168.