private void openDatabase() { dbHelper = TestDbFactory.getDatabaseHelper(ctx); SQLiteDatabase db = dbHelper.getWritableDatabase(); assertEquals(TestDatabaseHelper.DB_VERSION, db.getVersion()); // wipe database dbHelper.dropAndCreate(db); }
public void testRestoreWithDefaultValuesForNewFields() { SQLiteDatabase db = dbHelper.getWritableDatabase(); dbHelper.dropAndCreate(db); SimpleEntityTable th = new SimpleEntityTable(); InputStream csvStream = this.getClass().getClassLoader().getResourceAsStream("assets/testDb.v0.SimpleEntity"); new CsvTableReader(th).importFromCsv(db, csvStream); List<SimpleEntity> listAll = dao.listAll(); assertEquals(1, listAll.size()); SimpleEntity newEntity = new SimpleEntity(); newEntity.setId(1); DaoTestCase.assertAllFieldsMatch(newEntity, listAll.get(0)); }
public void testBackupAndRestore() throws IOException { dao.deleteAll(); persistRandomEntities(11); SimpleEntity e = newTestEntity(); dao.insert(e); List<SimpleEntity> before = dao.listAll(); SQLiteDatabase db = dbHelper.getWritableDatabase(); dbHelper.backupAllTablesToCsv(ctx, db, null); dbHelper.dropAndCreate(db); assertEquals(0, dao.listAll().size()); dbHelper.restoreAllTablesFromCsv(ctx, db, null); List<SimpleEntity> after = dao.listAll(); for (int i = 0; i < before.size(); i++) { DaoTestCase.assertAllFieldsMatch(before.get(i), after.get(i)); } }
/** * Read in a CSV file from the prior db version with columns added and dropped. Ensure that the * unchanged columns are matched by name and that new columns have the expected default values. */ public void testReadFromCsv() { SQLiteDatabase db = dbHelper.getWritableDatabase(); dbHelper.dropAndCreate(db); SimpleEntityTable th = new SimpleEntityTable(); InputStream csvStream = this.getClass().getClassLoader().getResourceAsStream("assets/testDb.v1.SimpleEntity"); new CsvTableReader(th).importFromCsv(db, csvStream); List<SimpleEntity> listAll = dao.listAll(); assertEquals(2, listAll.size()); SimpleEntity testEntity = newTestEntity(); testEntity.setId(1); testEntity.setCharField((char) 0); // Expected default value DaoTestCase.assertAllFieldsMatch(testEntity, listAll.get(0)); SimpleEntity newEntity = new SimpleEntity(); newEntity.setId(2); DaoTestCase.assertAllFieldsMatch(newEntity, listAll.get(1)); }
/** * Verify that all columns are correctly escaped and converted to Strings. * * @throws IOException */ public void testWriteToCsv() throws IOException { SQLiteDatabase db = dbHelper.getWritableDatabase(); dbHelper.dropAndCreate(db); SimpleEntity populatedEntity = newTestEntity(); dao.insert(populatedEntity); dao.insert(new SimpleEntity()); // default String timestamp = "." + System.currentTimeMillis(); dbHelper.backupAllTablesToCsv(ctx, db, timestamp); FileInputStream ois = ctx.openFileInput("testDb.v2.SimpleEntity" + timestamp); InputStreamReader isr = new InputStreamReader(ois); BufferedReader reader = new BufferedReader(isr); reader.readLine(); // header row String row1 = reader.readLine(); String expected = "Q0FGRUJBQkU=,1,0,122,3ff9e3779b97f4a8,VALUE1,1.618034,1,75025,12586269025,0,28657,1,89,88,18000000,-401c3910c8d016b0,-0.618034,1836311903,,86267571272,17711,\"Hello, world!\""; assertEquals(expected, row1); String row2 = reader.readLine(); expected = ",0,0,0,0,,0.0,2,0,0,0,0,,,,,,,,,,,"; assertEquals(expected, row2); }