/** * <em>Note:</em> This test <i>does not</i> check {@code insert} function of {@code * ContentProvider}. The test only asserts that all columns were created with correct data type. * So, in this test, we use pure {@code insert} function of {@code SQLiteDatabase} */ @Test public void checkInsertIntoPhotosTable() { ContentValues testValue = TestUtilities.createPhotoValues(); long rowId = db.insert(ArticleDatabase.Tables.PHOTOS, null, testValue); assertTrue(rowId != -1); cursor = db.query( ArticleDatabase.Tables.PHOTOS, null, // column null, // selections null, // selection args null, // groupBy null, // having null); // orderBy assertTrue("Error: No records returned from Photos table", cursor.moveToFirst()); TestUtilities.validateCurrentRecord("Error: Photos query validation failed", cursor, testValue); assertFalse("Error: More than one record return from Photos query", cursor.moveToNext()); }
/** * Tests articles_photos_delete trigger should be created and work correctly Explains about * articles_photos_delete: When we delete an article, articles_photos_delete trigger also delete * related photos. */ @Test public void articlesPhotosDeleteTrigger_shouldBeCreated() { ContentValues articleValues = TestUtilities.createArticleValues(); ContentValues photoValues = TestUtilities.createPhotoValues(); int articleId = articleValues.getAsInteger(ArticleContract.Articles.ARTICLE_ID); int photoId = photoValues.getAsInteger(ArticleContract.Photos.PHOTO_ID); // Insert an articles and a related photo into db long rowId = db.insert(ArticleDatabase.Tables.ARTICLES, null, articleValues); assertThat("Unable to Insert an article into the DB", rowId != -1); rowId = db.insert(ArticleDatabase.Tables.PHOTOS, null, photoValues); assertThat("Unable to Insert a photo into the DB", rowId != -1); // Query an article to assert that it was already inserted in DB cursor = db.query( ArticleDatabase.Tables.ARTICLES, null, ArticleContract.Articles.ARTICLE_ID + "=?", new String[] {String.valueOf(articleId)}, null, null, null); TestUtilities.validateCursor(null, cursor, articleValues); // Query a photo to assert that it was already inserted in DB cursor = db.query( ArticleDatabase.Tables.PHOTOS, null, ArticleContract.Photos.PHOTO_ID + "=?", new String[] {String.valueOf(photoId)}, null, null, null); TestUtilities.validateCursor(null, cursor, photoValues); // Finally, delete the article to check that the photo also deletes in behind int count = db.delete( ArticleDatabase.Tables.ARTICLES, ArticleContract.Articles.ARTICLE_ID + "=?", new String[] {String.valueOf(articleId)}); assertThat(count, is(1)); cursor = db.query( ArticleDatabase.Tables.ARTICLES, null, ArticleContract.Articles.ARTICLE_ID + "=?", new String[] {String.valueOf(articleId)}, null, null, null); assertThat("Error: Delete an article unsuccessful", !cursor.moveToFirst()); cursor = db.query( ArticleDatabase.Tables.PHOTOS, null, ArticleContract.Photos.PHOTO_ID + "=?", new String[] {String.valueOf(photoId)}, null, null, null); assertThat("Error: articles_photos_delete trigger didn't work.", !cursor.moveToFirst()); }