Skip to content

Instantly share code, notes, and snippets.

@nking
Created September 1, 2011 03:13
Show Gist options
  • Save nking/1185346 to your computer and use it in GitHub Desktop.
Save nking/1185346 to your computer and use it in GitHub Desktop.
Android sqlite database schema version tests
If you want to test upgrades of your database schemas against new releases that have
included database version changes:
To save your database,
adb shell pull /data/data/your.android.package.name/databases/yourprojectname.db \
/yourprojectcvsdir/databases/yourprojectname_version.db
If you're using a ContentProvider such as given by the Android example
http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NotePadProvider.html
make the methods accepting SQLiteDatabase arguments accessible. In the example
below, the extended SQLiteOpenHelper is in a separate class.
Before you test your ContentProvider, run a target to install the older version
database in the adb virtual shell with a location that doesn't conflict with the
expected db location:
adb shell push /yourprojectcvsdir/databases/yourprojectname_version.db \
/data/data/your.android.package.name/files/yourprojectname_version.db
Then in your test class which extends ProviderTestCase2<CProvider>, have a test
method to assert that yourprojectname_version.db is updated in your ContentProvider
(method onUpgrade) and assert your methods...
String dbPath =
"/data/data/your.android.package.name/files/yourprojectname_version.db";
SQLiteDatabase db = null;
try {
ProviderDBHelper dbHelper = new ProviderDBHelper(getContext(), dbPath, version);
db = dbHelper.getWritableDatabase();
CProvider cProvider = getProvider();
// a wrapper to method that iterates over cursor to populate objects, for example
Collection<YourObjects> list = cProvider.getObjects(db);
assertNotNull(list);
assertTrue(list.size() > 0);
} catch (Throwable t) {
fail(t.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment