public long insertReview(long movieId) { MovieDbHelper dbHelper = new MovieDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues testValues = TestUtilities.createReviewBladerunnerValues(movieId); long reviewRowId = db.insert(MoviesContract.ReviewEntry.TABLE_NAME, null, testValues); assertTrue(reviewRowId != -1); Cursor cursor = db.query( MoviesContract.ReviewEntry.TABLE_NAME, // Table to Query null, // all columns null, // Columns for the "where" clause null, // Values for the "where" clause null, // columns to group by null, // columns to filter by row groups null // sort order ); assertTrue("Error: No Records returned from review query", cursor.moveToFirst()); TestUtilities.validateCurrentRecord( "Error: Review Query Validation Failed", cursor, testValues); // Move the cursor to demonstrate that there is only one record in the database assertFalse("Error: More than one record returned from review query", cursor.moveToNext()); // Sixth Step: Close Cursor and Database cursor.close(); db.close(); return reviewRowId; }
public long insertMovieXXX(int movieId) { // First step: Get reference to writable database // If there's an error in those massive SQL table creation Strings, // errors will be thrown here when you try to get a writable database. MovieDbHelper dbHelper = new MovieDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); // Second Step: Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) ContentValues testValues; if (movieId == 1) { testValues = TestUtilities.createMovieBladerunnerValues(); } else { testValues = TestUtilities.createMovieCasablancaValues(); } // Third Step: Insert ContentValues into database and get a row ID back long locationRowId; locationRowId = db.insert(MoviesContract.MovieEntry.TABLE_NAME, null, testValues); // Verify we got a row back. assertTrue(locationRowId != -1); // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made // the round trip. // Fourth Step: Query the database and receive a Cursor back // A cursor is your primary interface to the query results. Cursor cursor = db.query( MoviesContract.MovieEntry.TABLE_NAME, // Table to Query null, // all columns null, // Columns for the "where" clause null, // Values for the "where" clause null, // columns to group by null, // columns to filter by row groups null // sort order ); // Move the cursor to a valid database row and check to see if we got any records back // from the query assertTrue("Error: No Records returned from movie query", cursor.moveToFirst()); // Fifth Step: Validate data in resulting Cursor with the original ContentValues // (you can use the validateCurrentRecord function in TestUtilities to validate the // query if you like) TestUtilities.validateCurrentRecord("Error: Movie Query Validation Failed", cursor, testValues); // Move the cursor to demonstrate that there is only one record in the database assertFalse("Error: More than one record returned from movie query", cursor.moveToNext()); // Sixth Step: Close Cursor and Database cursor.close(); db.close(); return locationRowId; }
public void testTrailerTable() { // First Step : Delete the database to create an empty database for the testcase deleteMovieDatabae(); // Second Step : Get a reference to WritableDatabase // If there's an error in those massive SQL table creation Strings, // errors will be thrown here when you try to get a writable database. MovieDbHelper dbHelper = new MovieDbHelper(mContext); SQLiteDatabase database = dbHelper.getWritableDatabase(); assertEquals(true, database.isOpen()); // Third Step : Create a movie record using Contentvalues ContentValues testValue = TestUtilites.createTrailerRecord(); // Fourth Step : Insert the ContentValue into the database and get a Row Id back long movieRowId; movieRowId = database.insert(MovieContract.TrailerEntry.TABLE_NAME, null, testValue); // Verify we got a row back assertTrue("Error: Failure to insert the record", movieRowId != -1); // Fifith Step : Querry the database to receive a cursor back Cursor cursor = database.query( MovieContract.TrailerEntry.TABLE_NAME, // Table to query null, // all columns null, // columns for the where clause null, // values for the where clause null, // columns to group by null, // columns to filter by row groups null // sort order ); // Move the cursor to valid database row and check to see if we get any record back assertTrue("Error: No records return from the location query", cursor.moveToFirst()); // Sixth step : validate the record in Cursor with original content values TestUtilites.validateCurrentRecord("Error: Movie querry validation failed", cursor, testValue); // Move the cursor to the next record to demonstrate there is only one record in the database assertFalse("Error: More than one record returned from the querry", cursor.moveToNext()); cursor.close(); database.close(); }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); int rowsUpdated = db.update(MovieDbContract.MovieEntry.TABLE_NAME, values, selection, selectionArgs); if (rowsUpdated != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsUpdated; }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor retCursor; switch (sUriMatcher.match(uri)) { case MOVIES: retCursor = mOpenHelper .getReadableDatabase() .query( MovieDbContract.MovieEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; case MOVIE_BY_ID: String movieIdSetting = MovieDbContract.MovieEntry.getDbIdFromUri(uri); retCursor = mOpenHelper .getReadableDatabase() .query( MovieDbContract.MovieEntry.TABLE_NAME, projection, sIdSettingSelection, new String[] {movieIdSetting}, null, null, sortOrder); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } retCursor.setNotificationUri(getContext().getContentResolver(), uri); return retCursor; }
@Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); Uri returnUri; long _id = db.insert(MovieDbContract.MovieEntry.TABLE_NAME, null, values); if (_id > 0) { returnUri = MovieDbContract.MovieEntry.buildUriDbId(_id); } else { throw new android.database.SQLException("Failed to insert row into " + uri); } getContext().getContentResolver().notifyChange(uri, null); return returnUri; }
/* This test uses the database directly to insert and then uses the ContentProvider to read out the data. Uncomment this test to see if your movie queries are performing correctly. */ public void testBasicMovieQueries() { // insert our test records into the database MovieDbHelper dbHelper = new MovieDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues testValues = TestUtilities.createMovie(); long movieRowId = TestUtilities.insertMovieValues(mContext); // Test the basic content provider query Cursor movieCursor = mContext.getContentResolver().query(MovieEntry.CONTENT_URI, null, null, null, null); // Make sure we get the correct cursor out of the database TestUtilities.validateCursor("testBasicMovieQueries, movie query", movieCursor, testValues); // Has the NotificationUri been set correctly? --- we can only test this easily against API // level 19 or greater because getNotificationUri was added in API level 19. if (Build.VERSION.SDK_INT >= 19) { assertEquals( "Error: Movie Query did not properly set NotificationUri", movieCursor.getNotificationUri(), MovieEntry.CONTENT_URI); } }
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if (null == selection) { // this makes delete all rows return the number of rows deleted selection = "1"; } int rowsDeleted = db.delete(MovieDbContract.MovieEntry.TABLE_NAME, selection, selectionArgs); // Because a null deletes all rows if (rowsDeleted != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsDeleted; }
// You do not need to call this method. This is a method specifically to assist the testing // framework in running smoothly. You can read more at: // http://developer.android.com/reference/android/content/ContentProvider.html#shutdown() @Override @TargetApi(11) public void shutdown() { mOpenHelper.close(); super.shutdown(); }