Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
4.06 Test read and write from the weather table
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent bf7b04b commit dd092ef
Showing 1 changed file with 71 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

import junit.framework.Test;

import java.util.HashSet;

public class TestDb extends AndroidTestCase {
Expand Down Expand Up @@ -114,13 +112,83 @@ public void testCreateDb() throws Throwable {
also make use of the ValidateCurrentRecord function from within TestUtilities.
*/
public void testLocationTable() {
insertLocation();
}

/*
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: 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
// 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: Create ContentValues of what you want to insert
// (you can use the createNorthPoleLocationValues if you wish)
ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
Expand Down Expand Up @@ -164,48 +232,6 @@ public void testLocationTable() {
// Sixth Step: Close Cursor and Database
cursor.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.

// 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)

// Insert ContentValues into database and get a row ID back

// Query the database and receive a Cursor back

// Move the cursor to a valid database row

// Validate data in resulting Cursor with the original ContentValues
// (you can use the validateCurrentRecord function in TestUtilities to validate the
// query if you like)

// Finally, close the cursor and database
}


/*
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() {
return -1L;
return locationRowId;
}
}

1 comment on commit dd092ef

@weibeld
Copy link

@weibeld weibeld commented on dd092ef Oct 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key constraint on the location_id column of the weather table will not be enforced until you call setForeignKeyConstraintsEnabled(true) on the SQLiteDatabase object (see here). A good place to do this is in onConfigure of WeatherDbHelper.

If the foreign key constraints are disabled (as in this code), you could use any invalid value for locationRowId and the insertion of the weather data still succeeds (which shouldn't be the case).

Please sign in to comment.