How to migrate unit test database between versions

I have an Android app with a database with version 1.

I added a data column to one table and migrated to the new version.

The problem is how to do unit tests for this. I needed to check if the old data was inserted correctly into the new structure, and the new column was added and filled correctly.

How am I best to do this? Thanks in advance.

+5
source share
2 answers

You need to test 2 scenarios:

  • The user installs the new version directly.
  • The user is on an older version and upgrades to the new version.

Testing the second scenario is usually difficult.

What I usually do to test scenario 2:

  • Disable the application from your mobile device.
  • I am switching to version 1 release code version - Note that I am using GIT, and therefore it is very easy to switch to older versions.
  • Launch the app on your device using Android Studio.
  • Create some test data.
  • Switch to release 2 code.
  • Start debugging in Android Studio. At this point, there are break points in your database update methods to ensure a smooth database update.
  • Continue testing.
0
source

You can create a test case that creates an old version of your database and then starts your migration. The following is an example.

public class DbHelperTest extends AndroidTestCase { private SQLiteDatabase db; @Override public void setUp() throws Exception { super.setUp(); mContext = new RenamingDelegatingContext(getContext(), "db_helper_test_"); SQLiteDatabase.CursorFactory cursorFactory = new SQLiteDatabase.CursorFactory() { @Override public Cursor newCursor(final SQLiteDatabase db, final SQLiteCursorDriver masterQuery, final String editTable, final SQLiteQuery query) { return new SQLiteCursor(db, masterQuery, editTable, query); } }; db = SQLiteDatabase.create(cursorFactory); createV14Db(db); } @Override public void tearDown() throws Exception { super.tearDown(); db.close(); } private void createV14Db(SQLiteDatabase db) { // Create tables and indices db.execSQL(...); // Create some data db.execSQL(...); } public void testDbUpgrade() { DbHelper dbHelper = new DbHelper(mContext); dbHelper.onUpgrade(db, 14, 100); // Check that things have been upgraded correctly } } 
0
source

All Articles