/** * <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 checkInsertIntoLinkedArticlesTable() { ContentValues testValue = TestUtilities.createLinkedArticleValue(); long rowId = db.insert(ArticleDatabase.Tables.LINKED_ARTICLES, null, testValue); assertTrue(rowId != -1); cursor = db.query( ArticleDatabase.Tables.LINKED_ARTICLES, null, // column null, // selections null, // selection args null, // groupBy null, // having null); // orderBy assertTrue("Error: No records returned from Linked Articles table", cursor.moveToFirst()); TestUtilities.validateCurrentRecord( "Error: Linked Articles query validation failed", cursor, testValue); assertFalse( "Error: More than one record return from Linked Articles query", cursor.moveToNext()); }
/* Students: Here is where you will build code to test that we can insert and query the location database. We've done a lot of work for you. You'll want to look in TestUtilities where you can uncomment out the "createNorthPoleLocationValues" function. You can also make use of the ValidateCurrentRecord function from within TestUtilities. */ public void testLocationTable() { // First step: Get reference to writable database SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) ContentValues values = TestUtilities.createNorthPoleLocationValues(); // Insert ContentValues into database and get a row ID back long row = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, values); assertTrue(row != -1); // Query the database and receive a Cursor back Cursor cursor = db.query(WeatherContract.LocationEntry.TABLE_NAME, null, null, null, null, null, null); // Move the cursor to a valid database row cursor.moveToFirst(); // 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: Location Query validation", cursor, values); // Finally, close the cursor and database cursor.close(); db.close(); }
/* Students: Here is where you will build code to test that we can insert and query the location database. We've done a lot of work for you. You'll want to look in TestUtilities where you can uncomment out the "createNorthPoleLocationValues" function. You can also make use of the ValidateCurrentRecord function from within TestUtilities. */ public long testLocationTable() { // First step: Get reference to writable database SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) TestUtilities tu = new TestUtilities(); ContentValues cv = tu.createNorthPoleLocationValues(); // Insert ContentValues into database and get a row ID back long id = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, cv); assertFalse("Insert failed. ", id == -1); // Query the database and receive a Cursor back Cursor c = db.rawQuery("SELECT * FROM " + WeatherContract.LocationEntry.TABLE_NAME, null); // Move the cursor to a valid database row c.moveToFirst(); // Validate data in resulting Cursor with the original ContentValues // (you can use the validateCurrentRecord function in TestUtilities to validate the // query if you like) tu.validateCurrentRecord("Nije isti location record", c, cv); // Finally, close the cursor and database c.close(); db.close(); return id; }
/* Students: Here is where you will build code to test that we can insert and query the database. We've done a lot of work for you. You'll want to look in TestUtilities where you can use the "createWeatherValues" function. You can also make use of the validateCurrentRecord function from within TestUtilities. */ public long testWeatherTable() { // First insert the location, and then use the locationRowId to insert // the weather. Make sure to cover as many failure cases as you can.+ long locationRowId = insertLocation(); ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); // Instead of rewriting all of the code we've already written in testLocationTable // we can move this code to insertLocation and then call insertLocation from both // tests. Why move it? We need the code to return the ID of the inserted location // and our testLocationTable can only return void because it's a test. SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) // Insert ContentValues into database and get a row ID back long id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValues); assertFalse("Insert failed. ", id == -1); // Query the database and receive a Cursor back Cursor c = db.rawQuery("SELECT * FROM " + WeatherContract.WeatherEntry.TABLE_NAME, null); // Move the cursor to a valid database row c.moveToFirst(); // 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("Nije isti location record", c, weatherValues); // Finally, close the cursor and database c.close(); db.close(); // Finally, close the cursor and database return id; }
public long insertLocation() { // First step: Get reference to writable database SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase(); // Create ContentValues of what you want to insert ContentValues weatherValues = TestUtilities.createNorthPoleLocationValues(); // Insert ContentValues into database and get a row ID back long rowID = TestUtilities.insertNorthPoleLocationValues(this.mContext); assertFalse("Insert failed", rowID == -1); // Query the database and receive a Cursor back Cursor c = db.query(WeatherContract.LocationEntry.TABLE_NAME, null, null, null, null, null, null); // Move the cursor to a valid database row assertTrue("No records freturned from location query", c.moveToFirst()); // 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( "Location data read from DB did not match ContentValues", c, weatherValues); assertFalse("More than one record returned", c.moveToNext()); // Finally, close the cursor and database c.close(); db.close(); return rowID; }
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; }
/* Students: Here is where you will build code to test that we can insert and query the database. We've done a lot of work for you. You'll want to look in TestUtilities where you can use the "createWeatherValues" function. You can also make use of the validateCurrentRecord function from within TestUtilities. */ public void testWeatherTable() { // First insert the location, and then use the locationRowId to insert // the weather. Make sure to cover as many failure cases as you can. // Instead of rewriting all of the code we've already written in testLocationTable // we can move this code to insertLocation and then call insertLocation from both // tests. Why move it? We need the code to return the ID of the inserted location // and our testLocationTable can only return void because it's a test. long locationRowId = insertLocation(); // Make sure we have a valid row ID. assertFalse("Error: Location Not Inserted Correctly", locationRowId == -1L); // 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. WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); // Second Step (Weather): Create weather values ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); // Third Step (Weather): Insert ContentValues into database and get a row ID back long weatherRowId = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValues); assertTrue(weatherRowId != -1); // Fourth Step: Query the database and receive a Cursor back // A cursor is your primary interface to the query results. Cursor weatherCursor = db.query( WeatherContract.WeatherEntry.TABLE_NAME, // Table to Query null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause null, // columns to group by null, // columns to filter by row groups null // sort order ); // Move the cursor to the first valid database row and check to see if we have any rows assertTrue("Error: No Records returned from location query", weatherCursor.moveToFirst()); // Fifth Step: Validate the location Query TestUtilities.validateCurrentRecord( "testInsertReadDb weatherEntry failed to validate", weatherCursor, weatherValues); // Move the cursor to demonstrate that there is only one record in the database assertFalse( "Error: More than one record returned from weather query", weatherCursor.moveToNext()); // Sixth Step: Close cursor and database weatherCursor.close(); dbHelper.close(); }
/* Students: Here is where you will build code to test that we can insert and query the database. We've done a lot of work for you. You'll want to look in TestUtilities where you can use the "createWeatherValues" function. You can also make use of the validateCurrentRecord function from within TestUtilities. */ public void testWeatherTable() { // First insert the location, and then use the locationRowId to insert // the weather. Make sure to cover as many failure cases as you can. // Instead of rewriting all of the code we've already written in testLocationTable // we can move this code to insertLocation and then call insertLocation from both // tests. Why move it? We need the code to return the ID of the inserted location // and our testLocationTable can only return void because it's a test. long locationRowId = insertLocation(); assertFalse("Error: Location Not Inserted Correctly", locationRowId == -1L); // First step: Get reference to writable database WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createWeatherValues TestUtilities function if you wish) ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); // Insert ContentValues into database and get a row ID back long weatherRowId = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValues); assertTrue(weatherRowId != -1); // Query the database and receive a Cursor back Cursor weatherCursor = db.query(WeatherContract.WeatherEntry.TABLE_NAME, null, null, null, null, null, null); // Move the cursor to a valid database row assertTrue("Error: No Records returned from location query", weatherCursor.moveToFirst()); // 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( "testInsertReadDb weatherEntry failed to validate", weatherCursor, weatherValues); assertFalse( "Error: More thant one record returned from weather query", weatherCursor.moveToNext()); // Finally, close the cursor and database weatherCursor.close(); db.close(); }
/* Students: Here is where you will build code to test that we can insert and query the database. We've done a lot of work for you. You'll want to look in TestUtilities where you can use the "createWeatherValues" function. You can also make use of the validateCurrentRecord function from within TestUtilities. */ public void testWeatherTable() { // First insert the location, and then use the locationRowId to insert // the weather. Make sure to cover as many failure cases as you can. SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) ContentValues values = TestUtilities.createNorthPoleLocationValues(); long row = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, values); // Instead of rewriting all of the code we've already written in testLocationTable // we can move this code to insertLocation and then call insertLocation from both // tests. Why move it? We need the code to return the ID of the inserted location // and our testLocationTable can only return void because it's a test. // First step: Get reference to writable database // Create ContentValues of what you want to insert // (you can use the createWeatherValues TestUtilities function if you wish) ContentValues weatherValue = TestUtilities.createWeatherValues(row); // Insert ContentValues into database and get a row ID back long weatherRow = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValue); assertTrue(weatherRow != -1); // Query the database and receive a Cursor back Cursor cursor = db.query(WeatherContract.WeatherEntry.TABLE_NAME, null, null, null, null, null, null); // Move the cursor to a valid database row cursor.moveToFirst(); // 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: Location Query validation", cursor, weatherValue); // Finally, close the cursor and database cursor.close(); db.close(); }
public void testWeatherTable() { // First insert the location, and then use the locationRowId to insert // the weather. Make sure to cover as many failure cases as you can. long location = insertLocation(); // First step: Get reference to writable database SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createWeatherValues TestUtilities function if you wish) ContentValues weatherValues = TestUtilities.createWeatherValues(location); // Insert ContentValues into database and get a row ID back long weatherRowId = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValues); assertFalse("Insert failed", weatherRowId == -1); // Query the database and receive a Cursor back Cursor c = db.query( WeatherContract.WeatherEntry.TABLE_NAME, // Table to Query null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "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 assertTrue("No records freturned from weather query", c.moveToFirst()); // 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( "testInsertReadDb weatherEntry failed to validate", c, weatherValues); // Finally, close the cursor and database c.close(); db.close(); }
/* Students: This is a helper method for the testWeatherTable quiz. You can move your code from testLocationTable to here so that you can call this code from both testWeatherTable and testLocationTable. */ public long insertLocation() { // First step: Get reference to writable database WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); // Insert ContentValues into database and get a row ID back long locationRowId; locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues); assertTrue(locationRowId != -1); // Query the database and receive a Cursor back Cursor cursor = db.query(WeatherContract.LocationEntry.TABLE_NAME, null, null, null, null, null, null); assertTrue("Error: No Records returned from location query", cursor.moveToFirst()); // Move the cursor to a valid database row TestUtilities.validateCurrentRecord( "Error: Location Query Validation Failed", cursor, testValues); // Validate data in resulting Cursor with the original ContentValues // (you can use the validateCurrentRecord function in TestUtilities to validate the // query if you like) assertFalse("Error: More than one record returned from location query", cursor.moveToNext()); // Finally, close the cursor and database cursor.close(); db.close(); return locationRowId; }
// Student: Uncomment this test after you have completed writing the BulkInsert functionality // in your provider. Note that this test will work with the built-in (default) provider // implementation, which just inserts records one-at-a-time, so really do implement the // BulkInsert ContentProvider function. public void testBulkInsert() { // first, let's create a location value ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); Uri locationUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, testValues); long locationRowId = ContentUris.parseId(locationUri); // 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. // A cursor is your primary interface to the query results. Cursor cursor = mContext .getContentResolver() .query( LocationEntry.CONTENT_URI, null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause null // sort order ); TestUtilities.validateCursor( "testBulkInsert. Error validating LocationEntry.", cursor, testValues); // Now we can bulkInsert some weather. In fact, we only implement BulkInsert for weather // entries. With ContentProviders, you really only have to implement the features you // use, after all. ContentValues[] bulkInsertContentValues = createBulkInsertWeatherValues(locationRowId); // Register a content observer for our bulk insert. TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver(); mContext .getContentResolver() .registerContentObserver(WeatherEntry.CONTENT_URI, true, weatherObserver); int insertCount = mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, bulkInsertContentValues); // Students: If this fails, it means that you most-likely are not calling the // getContext().getContentResolver().notifyChange(uri, null); in your BulkInsert // ContentProvider method. weatherObserver.waitForNotificationOrFail(); mContext.getContentResolver().unregisterContentObserver(weatherObserver); assertEquals(insertCount, BULK_INSERT_RECORDS_TO_INSERT); // A cursor is your primary interface to the query results. cursor = mContext .getContentResolver() .query( WeatherEntry.CONTENT_URI, null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause WeatherEntry.DATE + " ASC" // sort order == by DATE ASCENDING ); // we should have as many records in the database as we've inserted assertEquals(cursor.getCount(), BULK_INSERT_RECORDS_TO_INSERT); // and let's make sure they match the ones we created cursor.moveToFirst(); for (int i = 0; i < BULK_INSERT_RECORDS_TO_INSERT; i++, cursor.moveToNext()) { TestUtilities.validateCurrentRecord( "testBulkInsert. Error validating WeatherEntry " + i, cursor, bulkInsertContentValues[i]); } // cursor.close(); }