Disclaimer - I have used GreenDAO ORM for creating and managing the sqlite db from within my android app.
For my android app, while writing unit tests, I wish to tell the app to switch to using a certain db which is different from the normal one in order to make sure that the tests do not contaminate the real db. Correspondingly I have used appropriate functions to create new db and switch to it like this
public class DbTests extends ApplicationTestCase<MyApp> {
private static final String TAG = "DbTests";
private MyApp mApplication;
private Context mContext;
public DbTests() {
super(MyApp.class);
}
@Override
protected void setUp() throws Exception {
Log.d(TAG, "in setUp");
super.setUp();
createApplication();
mApplication = getApplication();
Log.d(TAG, "setUp done");
}
@Override
public void tearDown() throws Exception {
super.tearDown();
}
private void setUpFreshInstallNoDataCase(boolean val) {
assertNotNull(mApplication);
LocalDataHelpers.setupLocalData(mApplication, InitialConditions.FreshInstallNoData);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void testFreshInstallNoDataCase() {
Log.d(TAG, "testFreshInstallNoDataCase");
setUpFreshInstallNoDataCase(true);
mContext = mApplication.getApplicationContext();
assertEquals(0, PersonRepository.getAllPersons(mContext).size());
}
}
public class LocalDataHelpers {
public static final String TAG = "LocalDataHelpers";
static MyApp globalApplication;
public static void setupLocalData(MyApp app, String condition) {
globalApplication = app;
if(globalApplication.daoSession != null){
Log.d(TAG, "daoSession not null");
globalApplication.daoSession.clear();
globalApplication.setupDatabase(condition);
}
}
}
In my application onCreate and setupDatabase are like this
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();
EventBus.getDefault().register(this);
EventBus.getDefault().post(new PersonEvents.FetchPersonsFromServer());
setupDatabase(InitialConditions.DefaultSetUp);
}
private void setupDatabase(String condition) {
DaoMaster.DevOpenHelper helper = null;
SQLiteDatabase db = null;
DaoMaster daoMaster = null;
if (condition.equals(InitialConditions.DefaultSetUp)) {
helper = new DaoMaster.DevOpenHelper(this, "example-db", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
}
else if (condition.equals(InitialConditions.FreshInstallNoData)) {
helper = new DaoMaster.DevOpenHelper(this, "freshInstallNoData-db", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
//clear all possible data here by dropping all tables and recreate
Log.d(TAG, "Dropping all tables and recreating them");
DaoMaster.dropAllTables(db, true);
DaoMaster.createAllTables(db, false);
}
if (helper != null && db != null && daoMaster != null) {
daoSession.clear();
daoSession = daoMaster.newSession();
}
else {
Log.e(TAG, "setupDatabase Error : condition " + condition + ", either helper or db or daomaster is null");
}
List<Pair<String, String>> dbs = getDaoSession().getDatabase().getAttachedDbs();
for (Pair<String, String> pair : dbs) {
Log.d(TAG, "setupDatabase <" + pair.first + ", " + pair.second + ">");
}
}
The onEvent for FetchPersonsFromServer gets a list of persons and saves them to the db using PersonDao.
Now If I clear all app data from my device and run the test directly, as expected the test passes. However if I run the app normally, and then run tests, the assert statement fails at
assertEquals(0, PersonRepository.getAllPersons(mContext).size());
with the size as that of the default db value.
I am going crazy trying to figure out why. The log statements in setupDatabase show that the correct db name is showing up ie freshdbinstall-db...
Are two dbs not allowed in android? Logically there should be no issues? Is it GreenDao which doesn't allow it? Am I making some basic mistakes...
Please help.
Aucun commentaire:
Enregistrer un commentaire