### Question:
SchemaManager extends SQLiteOpenHelper { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { ensureConfigured(db); upgrade(db, oldVersion, newVersion); } @Inject  SchemaManager(
      Context context,
      @Named("SQLITE_DB_NAME") String dbName,
      @Named("SCHEMA_VERSION") int schemaVersion); @Override void onConfigure(SQLiteDatabase db); @Override void onCreate(SQLiteDatabase db); @Override void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion); @Override void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion); @Override void onOpen(SQLiteDatabase db);  }### Answer:
@Test public void upgradingV1ToLatest_emptyDatabase_allowsPersistsAfterUpgrade() { int oldVersion = 1; int newVersion = SCHEMA_VERSION; SchemaManager schemaManager = new SchemaManager(ApplicationProvider.getApplicationContext(), DB_NAME, oldVersion); SQLiteEventStore store = new SQLiteEventStore(clock, new UptimeClock(), CONFIG, schemaManager); schemaManager.onUpgrade(schemaManager.getWritableDatabase(), oldVersion, newVersion); PersistedEvent newEvent1 = store.persist(CONTEXT1, EVENT1); assertThat(store.loadBatch(CONTEXT1)).containsExactly(newEvent1); }
@Test public void upgradingV1ToLatest_nonEmptyDB_isLossless() { int oldVersion = 1; int newVersion = SCHEMA_VERSION; SchemaManager schemaManager = new SchemaManager(ApplicationProvider.getApplicationContext(), DB_NAME, oldVersion); SQLiteEventStore store = new SQLiteEventStore(clock, new UptimeClock(), CONFIG, schemaManager); PersistedEvent event1 = simulatedPersistOnV1Database(schemaManager, CONTEXT1, EVENT1); schemaManager.onUpgrade(schemaManager.getWritableDatabase(), oldVersion, newVersion); assertThat(store.loadBatch(CONTEXT1)).containsExactly(event1); }
@Test public void upgradingV3ToV4_nonEmptyDB_isLossless() { int oldVersion = 3; int newVersion = 4; SchemaManager schemaManager = new SchemaManager(ApplicationProvider.getApplicationContext(), DB_NAME, oldVersion); SQLiteEventStore store = new SQLiteEventStore(clock, new UptimeClock(), CONFIG, schemaManager); PersistedEvent event1 = simulatedPersistOnV1Database(schemaManager, CONTEXT1, EVENT1); schemaManager.onUpgrade(schemaManager.getWritableDatabase(), oldVersion, newVersion); assertThat(store.loadBatch(CONTEXT1)).containsExactly(event1); long inlineRows = store .getDb() .compileStatement("SELECT COUNT(*) from events where inline = 1") .simpleQueryForLong(); assertThat(inlineRows).isEqualTo(1); }
@Test public void upgrade_toANonExistentVersion_fails() { int oldVersion = 1; int nonExistentVersion = 1000; SchemaManager schemaManager = new SchemaManager(ApplicationProvider.getApplicationContext(), DB_NAME, oldVersion); Assert.assertThrows( IllegalArgumentException.class, () -> schemaManager.onUpgrade( schemaManager.getWritableDatabase(), oldVersion, nonExistentVersion)); }