Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do database migration unit test between releases

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

I added a data column in one table and idi a migration into a new release.

The problem I have is how to do unit tests for this. I needed to check if old data are correct inserted in the new structure and the new column is added and filled in the right way.

How would I best do this? Thanks in advance.

like image 461
IrApp Avatar asked Oct 31 '22 06:10

IrApp


1 Answers

You can create a test case that builds an old version of your database and then run your migration. Here is a stripped down 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
}
}
like image 142
Tad Avatar answered Nov 09 '22 09:11

Tad