コード例 #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));
  }
コード例 #2
0
  public void testCustomMigrationException() {
    TestDatabase database = new TestDatabase(getContext());
    SQLiteDatabaseWrapper db = database.getDatabase();
    // force a downgrade
    final int version = db.getVersion();
    final int previousVersion = version + 1;
    db.setVersion(previousVersion);
    database.close();
    database.getDatabase();

    try {
      assertTrue(database.caughtCustomMigrationException);
    } finally {
      database.clear(); // clean up since this is the only test using it
    }
  }
コード例 #3
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);
    }
  }