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