public void testRawQuery() { badDatabase.persist( new TestModel().setFirstName("Sam").setLastName("Bosley").setBirthday(testDate)); Cursor cursor = null; try { // Sanity check that there is only one row in the table assertEquals(1, badDatabase.countAll(TestModel.class)); // Test that raw query binds arguments correctly--if the argument // is bound as a String, the result will be empty cursor = badDatabase.rawQuery("select * from testModels where abs(_id) = ?", new Object[] {1}); assertEquals(1, cursor.getCount()); } finally { if (cursor != null) { cursor.close(); } } }
public void testTryAddColumn() { StringProperty goodProperty = new StringProperty(TestModel.TABLE, "good_column"); badDatabase.tryAddColumn( goodProperty); // don't care about the result, just that it didn't throw final StringProperty badProperty = new StringProperty(TestViewModel.VIEW, "bad_column"); testThrowsException( new Runnable() { @Override public void run() { badDatabase.tryAddColumn(badProperty); } }, IllegalArgumentException.class); }
private void testRecreateDuringMigration(boolean upgrade) { // insert some data to check for later badDatabase.persist(new Employee().setName("Alice")); badDatabase.persist(new Employee().setName("Bob")); badDatabase.persist(new Employee().setName("Cindy")); assertEquals(3, badDatabase.countAll(Employee.class)); testMigrationFailureCalled(upgrade, false, true); // verify the db was recreated with the appropriate version and no previous data SQLiteDatabaseWrapper db = badDatabase.getDatabase(); assertEquals(badDatabase.getVersion(), db.getVersion()); assertEquals(0, badDatabase.countAll(Employee.class)); }
private void testMigrationFailureCalled( boolean upgrade, boolean shouldThrow, boolean shouldRecreate) { badDatabase.setShouldThrowDuringMigration(shouldThrow); badDatabase.setShouldRecreate(shouldRecreate); // set version manually SQLiteDatabaseWrapper db = badDatabase.getDatabase(); final int version = db.getVersion(); final int previousVersion = upgrade ? version - 1 : version + 1; db.setVersion(previousVersion); // close and reopen to trigger an upgrade/downgrade badDatabase.onTablesCreatedCalled = false; badDatabase.close(); badDatabase.getDatabase(); assertTrue(upgrade ? badDatabase.onUpgradeCalled : badDatabase.onDowngradeCalled); if (shouldRecreate) { assertTrue(badDatabase.onTablesCreatedCalled); } else { assertTrue(badDatabase.onMigrationFailedCalled); assertEquals(previousVersion, badDatabase.migrationFailedOldVersion); assertEquals(version, badDatabase.migrationFailedNewVersion); } }
@Override protected void tearDownDatabase() { super.tearDownDatabase(); badDatabase.clear(); }
@Override protected void setupDatabase() { super.setupDatabase(); badDatabase = new BadDatabase(getContext()); badDatabase.getDatabase(); // init }