@Test public void createMethodTest() { Note note = new Note(); note.id = 2; note.body = "Body Text"; note.author = "John Doe"; Dao<Integer, Note> dao = dbLite.getDao(Note.class); long id = dao.create(note); Assert.assertTrue(id > 0); }
@Test public void isExistMethodTest() { ContentValues values = new ContentValues(); values.put("id", 1); values.put("body", "text"); long id = db.insert("Note", null, values); Assert.assertTrue("Note instance not created", id > 0); Note note = new Note(); note.id = 1; Dao<Integer, Note> dao = dbLite.getDao(Note.class); Assert.assertTrue("Note instance not found", dao.isExist(note)); }
@Test public void batchCreateOverridableDateValueIsSaved() { Note note = new Note(); note.id = 1; note.date = new Date(); List<Note> notes = new ArrayList<Note>(); notes.add(note); Dao<Integer, Note> dao = dbLite.getDao(Note.class); dao.batchCreateOverridable(notes); Cursor cursor = db.query("Note", null, null, null, null, null, null); cursor.moveToFirst(); long time = cursor.getLong(cursor.getColumnIndex("date")); Assert.assertEquals(note.date.getTime(), time); }
@Test public void deleteMethodTest() { Dao<Integer, Note> dao = dbLite.getDao(Note.class); ContentValues values = new ContentValues(); values.put("id", 4); values.put("body", "text"); values.put("author", "john doe"); values.put("date", new Date().toString()); values.put("sent", true); long id = db.insert("Note", null, values); Assert.assertTrue(id > 0); Note note = new Note(); note.id = 4; int rows = dao.delete(note); Assert.assertTrue(rows > 0); }
@SuppressWarnings("deprecation") @Test public void updateMethodContentValueExtractsDate() { Note note = new Note(); note.id = 1; note.date = new Date(2013, 11, 2); ContentValues values = new ContentValues(); values.put("id", 1); values.put("date", new Date().getTime()); long id = db.insert("Note", null, values); Assert.assertEquals(1, id); Dao<Integer, Note> dao = dbLite.getDao(Note.class); int rowAffected = dao.update(note, "id=?", note.id); Assert.assertTrue("No rows affected, so Date not updated", rowAffected > 0); }
@Test public void batchCreateFailsWhenAnyInsertOperationFail() { List<Note> notes = new ArrayList<Note>(); Note note = new Note(); note.id = 1; notes.add(note); Note note2 = new Note(); note2.id = 1; notes.add(note2); // both elements have same primary key,therefore should fail Dao<Integer, Note> dao = dbLite.getDao(Note.class); boolean isSuccessful = dao.batchCreate(notes); Cursor cursor = db.rawQuery("SELECT * FROM Note WHERE id=?", new String[] {"1"}); Assert.assertFalse("Batch Create Did Not Fail", isSuccessful); Assert.assertFalse("Batch Create Did Not Fail", cursor.moveToFirst()); }
@Test public void batchCreateOverridableTest() { List<Note> notes = new ArrayList<Note>(); Note note = new Note(); note.id = 1; note.author = "john doe"; notes.add(note); Note note2 = new Note(); note2.id = 2; note2.author = "jane doe"; notes.add(note2); Dao<Integer, Note> dao = dbLite.getDao(Note.class); dao.batchCreateOverridable(notes); Cursor cursor = db.query("Note", null, null, null, null, null, null); int numInserted = 0; while (cursor.moveToNext()) ++numInserted; Assert.assertEquals(2, numInserted); }
@Test public void batchCreateMethodTest() { List<Note> notes = new ArrayList<Note>(); Note note = new Note(); note.id = 1; notes.add(note); Note note2 = new Note(); note2.id = 2; notes.add(note2); Dao<Integer, Note> dao = dbLite.getDao(Note.class); boolean result = dao.batchCreate(notes); Cursor cursor = db.query("Note", null, null, null, null, null, null); while (cursor.moveToNext()) { Note actual = new Note(); actual.id = cursor.getInt(cursor.getColumnIndex("id")); Assert.assertEquals(actual.id, notes.get(cursor.getPosition()).id); } Assert.assertTrue(result); }
@Test public void updateMethodTest() { ContentValues values = new ContentValues(); values.put("id", 1); values.put("author", "john doe"); long id = db.insert("Note", null, values); Assert.assertTrue("Insertion failed", id > 0); Note note = new Note(); note.id = 1; note.author = "new author"; Dao<Integer, Note> dao = dbLite.getDao(Note.class); dao.update(note, "id=?", note.id); Cursor cursor = db.rawQuery("SELECT * FROM Note WHERE id=?", new String[] {Long.toString(id)}); if (cursor.moveToFirst()) { Note actual = new Note(); actual.id = cursor.getInt(cursor.getColumnIndex("id")); actual.author = cursor.getString(cursor.getColumnIndex("author")); Assert.assertEquals(note.author, actual.author); } }