Exemplo n.º 1
0
  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));
  }
Exemplo n.º 2
0
  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);
    }
  }
Exemplo n.º 3
0
 @Override
 protected void setupDatabase() {
   super.setupDatabase();
   badDatabase = new BadDatabase(getContext());
   badDatabase.getDatabase(); // init
 }