Done lesson 4a: testing
[Sunshine.git] / app / src / androidTest / java / uk / me / njae / sunshine / TestDb.java
1 package uk.me.njae.sunshine;
2
3 import android.content.ContentValues;
4 import android.database.Cursor;
5 import android.database.sqlite.SQLiteDatabase;
6 import android.test.AndroidTestCase;
7 import android.util.Log;
8
9 import java.util.Map;
10 import java.util.Set;
11
12 import uk.me.njae.sunshine.data.WeatherContract.LocationEntry;
13 import uk.me.njae.sunshine.data.WeatherContract.WeatherEntry;
14 import uk.me.njae.sunshine.data.WeatherDbHelper;
15
16 public class TestDb extends AndroidTestCase {
17
18 public static final String LOG_TAG = TestDb.class.getSimpleName();
19
20 public void testCreateDb() throws Throwable {
21 mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME);
22 SQLiteDatabase db = new WeatherDbHelper(
23 this.mContext).getWritableDatabase();
24 assertEquals(true, db.isOpen());
25 db.close();
26 }
27
28 public void testInsertReadDb() {
29
30 // Test data we're going to insert into the DB to see if it works.
31 String testLocationSetting = "99705";
32 String testCityName = "North Pole";
33 double testLatitude = 64.7488;
34 double testLongitude = -147.353;
35
36 // If there's an error in those massive SQL table creation Strings,
37 // errors will be thrown here when you try to get a writable database.
38 WeatherDbHelper dbHelper = new WeatherDbHelper(mContext);
39 SQLiteDatabase db = dbHelper.getWritableDatabase();
40
41 // Create a new map of values, where column names are the keys
42 ContentValues locationTestValues = createNorthPoleLocationValues();
43
44 long locationRowId;
45 locationRowId = db.insert(LocationEntry.TABLE_NAME, null, locationTestValues);
46
47 // Verify we got a row back.
48 assertTrue(locationRowId != -1);
49 Log.d(LOG_TAG, "New row id: " + locationRowId);
50
51 // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made
52 // the round trip.
53
54 // A cursor is your primary interface to the query results.
55 Cursor cursor = db.query(
56 LocationEntry.TABLE_NAME, // Table to Query
57 null, // All columns
58 null, // Columns for the "where" clause
59 null, // Values for the "where" clause
60 null, // columns to group by
61 null, // columns to filter by row groups
62 null // sort order
63 );
64
65 validateCursor(cursor, locationTestValues);
66
67 // Fantastic. Now that we have a location, add some weather!
68
69 ContentValues weatherTestValues = createWeatherValues(locationRowId);
70
71 long weatherRowId = db.insert(WeatherEntry.TABLE_NAME, null, weatherTestValues);
72 assertTrue(weatherRowId != -1);
73
74 // A cursor is your primary interface to the query results.
75 Cursor weatherCursor = db.query(
76 WeatherEntry.TABLE_NAME, // Table to Query
77 null, // leaving "columns" null just returns all the columns.
78 null, // cols for "where" clause
79 null, // values for "where" clause
80 null, // columns to group by
81 null, // columns to filter by row groups
82 null // sort order
83 );
84
85 validateCursor(weatherCursor, weatherTestValues);
86
87 weatherCursor.close();
88 dbHelper.close();
89 }
90
91 static ContentValues createWeatherValues(long locationRowId) {
92 ContentValues weatherValues = new ContentValues();
93 weatherValues.put(WeatherEntry.COLUMN_LOC_KEY, locationRowId);
94 weatherValues.put(WeatherEntry.COLUMN_DATETEXT, "20141205");
95 weatherValues.put(WeatherEntry.COLUMN_DEGREES, 1.1);
96 weatherValues.put(WeatherEntry.COLUMN_HUMIDITY, 1.2);
97 weatherValues.put(WeatherEntry.COLUMN_PRESSURE, 1.3);
98 weatherValues.put(WeatherEntry.COLUMN_MAX_TEMP, 75);
99 weatherValues.put(WeatherEntry.COLUMN_MIN_TEMP, 65);
100 weatherValues.put(WeatherEntry.COLUMN_SHORT_DESC, "Asteroids");
101 weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED, 5.5);
102 weatherValues.put(WeatherEntry.COLUMN_WEATHER_ID, 321);
103
104 return weatherValues;
105 }
106
107 static ContentValues createNorthPoleLocationValues() {
108 // Create a new map of values, where column names are the keys
109 ContentValues testValues = new ContentValues();
110 testValues.put(LocationEntry.COLUMN_LOCATION_SETTING, "99705");
111 testValues.put(LocationEntry.COLUMN_CITY_NAME, "North Pole");
112 testValues.put(LocationEntry.COLUMN_COORD_LAT, 64.7488);
113 testValues.put(LocationEntry.COLUMN_COORD_LONG, -147.353);
114
115 return testValues;
116 }
117
118 static void validateCursor(Cursor valueCursor, ContentValues expectedValues) {
119
120 assertTrue(valueCursor.moveToFirst());
121
122 Set<Map.Entry<String, Object>> valueSet = expectedValues.valueSet();
123 for (Map.Entry<String, Object> entry : valueSet) {
124 String columnName = entry.getKey();
125 int idx = valueCursor.getColumnIndex(columnName);
126 assertFalse(idx == -1);
127 String expectedValue = entry.getValue().toString();
128 assertEquals(expectedValue, valueCursor.getString(idx));
129 }
130 valueCursor.close();
131 }
132
133 }