Esempio n. 1
0
  /*
     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;
  }
Esempio n. 2
0
  /*
     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();
  }
Esempio n. 3
0
  /*
     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();
  }
Esempio n. 4
0
  /*
     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();
  }
Esempio n. 5
0
  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();
  }
Esempio n. 6
0
  /*
     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);
  }
Esempio n. 7
0
  // Make sure we can still delete after adding/updating stuff
  //
  // Student: Uncomment this test after you have completed writing the insert functionality
  // in your provider.  It relies on insertions with testInsertReadProvider, so insert and
  // query functionality must also be complete before this test can be used.
  public void testInsertReadProvider() {
    ContentValues testValues = TestUtilities.createNorthPoleLocationValues();

    // Register a content observer for our insert.  This time, directly with the content resolver
    TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver();
    mContext.getContentResolver().registerContentObserver(LocationEntry.CONTENT_URI, true, tco);
    Uri locationUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, testValues);

    // Did our content observer get called?  Students:  If this fails, your insert location
    // isn't calling getContext().getContentResolver().notifyChange(uri, null);
    tco.waitForNotificationOrFail();
    mContext.getContentResolver().unregisterContentObserver(tco);

    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(
        "testInsertReadProvider. Error validating LocationEntry.", cursor, testValues);

    // Fantastic.  Now that we have a location, add some weather!
    ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId);
    // The TestContentObserver is a one-shot class
    tco = TestUtilities.getTestContentObserver();

    mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, tco);

    Uri weatherInsertUri =
        mContext.getContentResolver().insert(WeatherEntry.CONTENT_URI, weatherValues);
    assertTrue(weatherInsertUri != null);

    // Did our content observer get called?  Students:  If this fails, your insert weather
    // in your ContentProvider isn't calling
    // getContext().getContentResolver().notifyChange(uri, null);
    tco.waitForNotificationOrFail();
    mContext.getContentResolver().unregisterContentObserver(tco);

    // A cursor is your primary interface to the query results.
    Cursor weatherCursor =
        mContext
            .getContentResolver()
            .query(
                WeatherEntry.CONTENT_URI, // 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
                );

    TestUtilities.validateCursor(
        "testInsertReadProvider. Error validating WeatherEntry insert.",
        weatherCursor,
        weatherValues);

    // Add the location values in with the weather data so that we can make
    // sure that the join worked and we actually get all the values back
    weatherValues.putAll(testValues);

    // Get the joined Weather and Location data
    weatherCursor =
        mContext
            .getContentResolver()
            .query(
                WeatherEntry.weatherWithLocationUri(TestUtilities.TEST_LOCATION),
                null, // leaving "columns" null just returns all the columns.
                null, // cols for "where" clause
                null, // values for "where" clause
                null // sort order
                );
    TestUtilities.validateCursor(
        "testInsertReadProvider.  Error validating joined Weather and Location Data.",
        weatherCursor,
        weatherValues);

    // Get the joined Weather and Location data with a start date
    //        weatherCursor = mContext.getContentResolver().query(
    //                WeatherEntry.buildWeatherLocationWithStartDate(
    //                        TestUtilities.TEST_LOCATION, TestUtilities.TEST_DATE),
    //                null, // leaving "columns" null just returns all the columns.
    //                null, // cols for "where" clause
    //                null, // values for "where" clause
    //                null  // sort order
    //        );
    TestUtilities.validateCursor(
        "testInsertReadProvider.  Error validating joined Weather and Location Data with start date.",
        weatherCursor,
        weatherValues);

    // Get the joined Weather data for a specific date
    //
    TestUtilities.validateCursor(
        "testInsertReadProvider.  Error validating joined Weather and Location data for a specific date.",
        weatherCursor,
        weatherValues);
  }