/* 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 location queries are performing correctly. */ public void testBasicLocationQueries() { // insert our test records into the database WeatherDatabaseHelper dbHelper = new WeatherDatabaseHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext); // Test the basic content provider query Cursor locationCursor = mContext.getContentResolver().query(LocationEntry.CONTENT_URI, null, null, null, null); // Make sure we get the correct cursor out of the database TestUtilities.validateCursor( "testBasicLocationQueries, location query", locationCursor, 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: Location Query did not properly set NotificationUri", locationCursor.getNotificationUri(), LocationEntry.CONTENT_URI); } }
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; }
/* This test uses the database directly to insert and then uses the ContentProvider to read out the data. Uncomment this test to see if the basic weather query functionality given in the ContentProvider is working correctly. */ public void testBasicWeatherQuery() { // insert our test records into the database WeatherDatabaseHelper dbHelper = new WeatherDatabaseHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext); // Fantastic. Now that we have a location, add some weather! ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); long weatherRowId = db.insert(WeatherEntry.TABLE_NAME, null, weatherValues); assertTrue("Unable to Insert WeatherEntry into the Database", weatherRowId != -1); db.close(); // Test the basic content provider query Cursor weatherCursor = mContext.getContentResolver().query(WeatherEntry.CONTENT_URI, null, null, null, null); // Make sure we get the correct cursor out of the database TestUtilities.validateCursor("testBasicWeatherQuery", weatherCursor, weatherValues); }