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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With