|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.example.android.sunshine.app.data; |
| 17 | + |
| 18 | +import android.test.AndroidTestCase; |
| 19 | + |
| 20 | +public class TestDb extends AndroidTestCase { |
| 21 | + |
| 22 | + public static final String LOG_TAG = TestDb.class.getSimpleName(); |
| 23 | + |
| 24 | + // Since we want each test to start with a clean slate |
| 25 | + void deleteTheDatabase() { |
| 26 | + mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME); |
| 27 | + } |
| 28 | + |
| 29 | + /* |
| 30 | + This function gets called before each test is executed to delete the database. This makes |
| 31 | + sure that we always have a clean test. |
| 32 | + */ |
| 33 | + public void setUp() { |
| 34 | + deleteTheDatabase(); |
| 35 | + } |
| 36 | + |
| 37 | + /* |
| 38 | + Students: Uncomment this test once you've written the code to create the Location |
| 39 | + table. Note that you will have to have chosen the same column names that I did in |
| 40 | + my solution for this test to compile, so if you haven't yet done that, this is |
| 41 | + a good time to change your column names to match mine. |
| 42 | +
|
| 43 | + Note that this only tests that the Location table has the correct columns, since we |
| 44 | + give you the code for the weather table. This test does not look at the |
| 45 | + */ |
| 46 | +// public void testCreateDb() throws Throwable { |
| 47 | +// // build a HashSet of all of the table names we wish to look for |
| 48 | +// // Note that there will be another table in the DB that stores the |
| 49 | +// // Android metadata (db version information) |
| 50 | +// final HashSet<String> tableNameHashSet = new HashSet<String>(); |
| 51 | +// tableNameHashSet.add(WeatherContract.LocationEntry.TABLE_NAME); |
| 52 | +// tableNameHashSet.add(WeatherContract.WeatherEntry.TABLE_NAME); |
| 53 | +// |
| 54 | +// mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME); |
| 55 | +// SQLiteDatabase db = new WeatherDbHelper( |
| 56 | +// this.mContext).getWritableDatabase(); |
| 57 | +// assertEquals(true, db.isOpen()); |
| 58 | +// |
| 59 | +// // have we created the tables we want? |
| 60 | +// Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); |
| 61 | +// |
| 62 | +// assertTrue("Error: This means that the database has not been created correctly", |
| 63 | +// c.moveToFirst()); |
| 64 | +// |
| 65 | +// // verify that the tables have been created |
| 66 | +// do { |
| 67 | +// tableNameHashSet.remove(c.getString(0)); |
| 68 | +// } while( c.moveToNext() ); |
| 69 | +// |
| 70 | +// // if this fails, it means that your database doesn't contain both the location entry |
| 71 | +// // and weather entry tables |
| 72 | +// assertTrue("Error: Your database was created without both the location entry and weather entry tables", |
| 73 | +// tableNameHashSet.isEmpty()); |
| 74 | +// |
| 75 | +// // now, do our tables contain the correct columns? |
| 76 | +// c = db.rawQuery("PRAGMA table_info(" + WeatherContract.LocationEntry.TABLE_NAME + ")", |
| 77 | +// null); |
| 78 | +// |
| 79 | +// assertTrue("Error: This means that we were unable to query the database for table information.", |
| 80 | +// c.moveToFirst()); |
| 81 | +// |
| 82 | +// // Build a HashSet of all of the column names we want to look for |
| 83 | +// final HashSet<String> locationColumnHashSet = new HashSet<String>(); |
| 84 | +// locationColumnHashSet.add(WeatherContract.LocationEntry._ID); |
| 85 | +// locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_CITY_NAME); |
| 86 | +// locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_COORD_LAT); |
| 87 | +// locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_COORD_LONG); |
| 88 | +// locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING); |
| 89 | +// |
| 90 | +// int columnNameIndex = c.getColumnIndex("name"); |
| 91 | +// do { |
| 92 | +// String columnName = c.getString(columnNameIndex); |
| 93 | +// locationColumnHashSet.remove(columnName); |
| 94 | +// } while(c.moveToNext()); |
| 95 | +// |
| 96 | +// // if this fails, it means that your database doesn't contain all of the required location |
| 97 | +// // entry columns |
| 98 | +// assertTrue("Error: The database doesn't contain all of the required location entry columns", |
| 99 | +// locationColumnHashSet.isEmpty()); |
| 100 | +// db.close(); |
| 101 | +// } |
| 102 | + |
| 103 | + /* |
| 104 | + Students: Here is where you will build code to test that we can insert and query the |
| 105 | + location database. We've done a lot of work for you. You'll want to look in TestUtilities |
| 106 | + where you can uncomment out the "createNorthPoleLocationValues" function. You can |
| 107 | + also make use of the ValidateCurrentRecord function from within TestUtilities. |
| 108 | + */ |
| 109 | + public void testLocationTable() { |
| 110 | + // First step: Get reference to writable database |
| 111 | + |
| 112 | + // Create ContentValues of what you want to insert |
| 113 | + // (you can use the createNorthPoleLocationValues if you wish) |
| 114 | + |
| 115 | + // Insert ContentValues into database and get a row ID back |
| 116 | + |
| 117 | + // Query the database and receive a Cursor back |
| 118 | + |
| 119 | + // Move the cursor to a valid database row |
| 120 | + |
| 121 | + // Validate data in resulting Cursor with the original ContentValues |
| 122 | + // (you can use the validateCurrentRecord function in TestUtilities to validate the |
| 123 | + // query if you like) |
| 124 | + |
| 125 | + // Finally, close the cursor and database |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + /* |
| 130 | + Students: Here is where you will build code to test that we can insert and query the |
| 131 | + database. We've done a lot of work for you. You'll want to look in TestUtilities |
| 132 | + where you can use the "createWeatherValues" function. You can |
| 133 | + also make use of the validateCurrentRecord function from within TestUtilities. |
| 134 | + */ |
| 135 | + public void testWeatherTable() { |
| 136 | + // First insert the location, and then use the locationRowId to insert |
| 137 | + // the weather. Make sure to cover as many failure cases as you can. |
| 138 | + |
| 139 | + // Instead of rewriting all of the code we've already written in testLocationTable |
| 140 | + // we can move this code to insertLocation and then call insertLocation from both |
| 141 | + // tests. Why move it? We need the code to return the ID of the inserted location |
| 142 | + // and our testLocationTable can only return void because it's a test. |
| 143 | + |
| 144 | + // First step: Get reference to writable database |
| 145 | + |
| 146 | + // Create ContentValues of what you want to insert |
| 147 | + // (you can use the createWeatherValues TestUtilities function if you wish) |
| 148 | + |
| 149 | + // Insert ContentValues into database and get a row ID back |
| 150 | + |
| 151 | + // Query the database and receive a Cursor back |
| 152 | + |
| 153 | + // Move the cursor to a valid database row |
| 154 | + |
| 155 | + // Validate data in resulting Cursor with the original ContentValues |
| 156 | + // (you can use the validateCurrentRecord function in TestUtilities to validate the |
| 157 | + // query if you like) |
| 158 | + |
| 159 | + // Finally, close the cursor and database |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + /* |
| 164 | + Students: This is a helper method for the testWeatherTable quiz. You can move your |
| 165 | + code from testLocationTable to here so that you can call this code from both |
| 166 | + testWeatherTable and testLocationTable. |
| 167 | + */ |
| 168 | + public long insertLocation() { |
| 169 | + return -1L; |
| 170 | + } |
| 171 | +} |
0 commit comments