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

Commit dd092ef

Browse files
author
Lyla
committed
4.06 Test read and write from the weather table
1 parent bf7b04b commit dd092ef

1 file changed

Lines changed: 71 additions & 45 deletions

File tree

  • app/src/androidTest/java/com/example/android/sunshine/app/data

app/src/androidTest/java/com/example/android/sunshine/app/data/TestDb.java

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import android.database.sqlite.SQLiteDatabase;
2121
import android.test.AndroidTestCase;
2222

23-
import junit.framework.Test;
24-
2523
import java.util.HashSet;
2624

2725
public class TestDb extends AndroidTestCase {
@@ -114,13 +112,83 @@ public void testCreateDb() throws Throwable {
114112
also make use of the ValidateCurrentRecord function from within TestUtilities.
115113
*/
116114
public void testLocationTable() {
115+
insertLocation();
116+
}
117+
118+
/*
119+
Students: Here is where you will build code to test that we can insert and query the
120+
database. We've done a lot of work for you. You'll want to look in TestUtilities
121+
where you can use the "createWeatherValues" function. You can
122+
also make use of the validateCurrentRecord function from within TestUtilities.
123+
*/
124+
public void testWeatherTable() {
125+
// First insert the location, and then use the locationRowId to insert
126+
// the weather. Make sure to cover as many failure cases as you can.
127+
128+
// Instead of rewriting all of the code we've already written in testLocationTable
129+
// we can move this code to insertLocation and then call insertLocation from both
130+
// tests. Why move it? We need the code to return the ID of the inserted location
131+
// and our testLocationTable can only return void because it's a test.
132+
133+
long locationRowId = insertLocation();
134+
135+
// Make sure we have a valid row ID.
136+
assertFalse("Error: Location Not Inserted Correctly", locationRowId == -1L);
117137

118138
// First step: Get reference to writable database
119139
// If there's an error in those massive SQL table creation Strings,
120140
// errors will be thrown here when you try to get a writable database.
121141
WeatherDbHelper dbHelper = new WeatherDbHelper(mContext);
122142
SQLiteDatabase db = dbHelper.getWritableDatabase();
123143

144+
// Second Step (Weather): Create weather values
145+
ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId);
146+
147+
// Third Step (Weather): Insert ContentValues into database and get a row ID back
148+
long weatherRowId = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValues);
149+
assertTrue(weatherRowId != -1);
150+
151+
// Fourth Step: Query the database and receive a Cursor back
152+
// A cursor is your primary interface to the query results.
153+
Cursor weatherCursor = db.query(
154+
WeatherContract.WeatherEntry.TABLE_NAME, // Table to Query
155+
null, // leaving "columns" null just returns all the columns.
156+
null, // cols for "where" clause
157+
null, // values for "where" clause
158+
null, // columns to group by
159+
null, // columns to filter by row groups
160+
null // sort order
161+
);
162+
163+
// Move the cursor to the first valid database row and check to see if we have any rows
164+
assertTrue( "Error: No Records returned from location query", weatherCursor.moveToFirst() );
165+
166+
// Fifth Step: Validate the location Query
167+
TestUtilities.validateCurrentRecord("testInsertReadDb weatherEntry failed to validate",
168+
weatherCursor, weatherValues);
169+
170+
// Move the cursor to demonstrate that there is only one record in the database
171+
assertFalse( "Error: More than one record returned from weather query",
172+
weatherCursor.moveToNext() );
173+
174+
// Sixth Step: Close cursor and database
175+
weatherCursor.close();
176+
dbHelper.close();
177+
}
178+
179+
180+
/*
181+
Students: This is a helper method for the testWeatherTable quiz. You can move your
182+
code from testLocationTable to here so that you can call this code from both
183+
testWeatherTable and testLocationTable.
184+
*/
185+
public long insertLocation() {
186+
// First step: Get reference to writable database
187+
// If there's an error in those massive SQL table creation Strings,
188+
// errors will be thrown here when you try to get a writable database.
189+
WeatherDbHelper dbHelper = new WeatherDbHelper(mContext);
190+
SQLiteDatabase db = dbHelper.getWritableDatabase();
191+
124192
// Second Step: Create ContentValues of what you want to insert
125193
// (you can use the createNorthPoleLocationValues if you wish)
126194
ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
@@ -164,48 +232,6 @@ public void testLocationTable() {
164232
// Sixth Step: Close Cursor and Database
165233
cursor.close();
166234
db.close();
167-
}
168-
169-
/*
170-
Students: Here is where you will build code to test that we can insert and query the
171-
database. We've done a lot of work for you. You'll want to look in TestUtilities
172-
where you can use the "createWeatherValues" function. You can
173-
also make use of the validateCurrentRecord function from within TestUtilities.
174-
*/
175-
public void testWeatherTable() {
176-
// First insert the location, and then use the locationRowId to insert
177-
// the weather. Make sure to cover as many failure cases as you can.
178-
179-
// Instead of rewriting all of the code we've already written in testLocationTable
180-
// we can move this code to insertLocation and then call insertLocation from both
181-
// tests. Why move it? We need the code to return the ID of the inserted location
182-
// and our testLocationTable can only return void because it's a test.
183-
184-
// First step: Get reference to writable database
185-
186-
// Create ContentValues of what you want to insert
187-
// (you can use the createWeatherValues TestUtilities function if you wish)
188-
189-
// Insert ContentValues into database and get a row ID back
190-
191-
// Query the database and receive a Cursor back
192-
193-
// Move the cursor to a valid database row
194-
195-
// Validate data in resulting Cursor with the original ContentValues
196-
// (you can use the validateCurrentRecord function in TestUtilities to validate the
197-
// query if you like)
198-
199-
// Finally, close the cursor and database
200-
}
201-
202-
203-
/*
204-
Students: This is a helper method for the testWeatherTable quiz. You can move your
205-
code from testLocationTable to here so that you can call this code from both
206-
testWeatherTable and testLocationTable.
207-
*/
208-
public long insertLocation() {
209-
return -1L;
235+
return locationRowId;
210236
}
211237
}

0 commit comments

Comments
 (0)