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));
   }
 }
 /**
  * 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);
 }