d47e534f35afa729b8942693864007a6fd41ddca
[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 uk.me.njae.sunshine.data.WeatherContract.LocationEntry;
10 import uk.me.njae.sunshine.data.WeatherContract.WeatherEntry;
11 import uk.me.njae.sunshine.data.WeatherDbHelper;
12
13 public class TestDb extends AndroidTestCase {
14
15 public static final String LOG_TAG = TestDb.class.getSimpleName();
16
17 public void testCreateDb() throws Throwable {
18 mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME);
19 SQLiteDatabase db = new WeatherDbHelper(
20 this.mContext).getWritableDatabase();
21 assertEquals(true, db.isOpen());
22 db.close();
23 }
24
25 public void testInsertReadDb() {
26
27 // Test data we're going to insert into the DB to see if it works.
28 String testLocationSetting = "99705";
29 String testCityName = "North Pole";
30 double testLatitude = 64.7488;
31 double testLongitude = -147.353;
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 values = new ContentValues();
40 values.put(LocationEntry.COLUMN_LOCATION_SETTING, testLocationSetting);
41 values.put(LocationEntry.COLUMN_CITY_NAME, testCityName);
42 values.put(LocationEntry.COLUMN_COORD_LAT, testLatitude);
43 values.put(LocationEntry.COLUMN_COORD_LONG, testLongitude);
44
45 long locationRowId;
46 locationRowId = db.insert(LocationEntry.TABLE_NAME, null, values);
47
48 // Verify we got a row back.
49 assertTrue(locationRowId != -1);
50 Log.d(LOG_TAG, "New row id: " + locationRowId);
51
52 // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made
53 // the round trip.
54
55 // Specify which columns you want.
56 String[] columns = {
57 LocationEntry._ID,
58 LocationEntry.COLUMN_LOCATION_SETTING,
59 LocationEntry.COLUMN_CITY_NAME,
60 LocationEntry.COLUMN_COORD_LAT,
61 LocationEntry.COLUMN_COORD_LONG
62 };
63
64 // A cursor is your primary interface to the query results.
65 Cursor cursor = db.query(
66 LocationEntry.TABLE_NAME, // Table to Query
67 columns,
68 null, // Columns for the "where" clause
69 null, // Values for the "where" clause
70 null, // columns to group by
71 null, // columns to filter by row groups
72 null // sort order
73 );
74
75 // If possible, move to the first row of the query results.
76 if (cursor.moveToFirst()) {
77 // Get the value in each column by finding the appropriate column index.
78 int locationIndex = cursor.getColumnIndex(LocationEntry.COLUMN_LOCATION_SETTING);
79 String location = cursor.getString(locationIndex);
80
81 int nameIndex = cursor.getColumnIndex((LocationEntry.COLUMN_CITY_NAME));
82 String name = cursor.getString(nameIndex);
83
84 int latIndex = cursor.getColumnIndex((LocationEntry.COLUMN_COORD_LAT));
85 double latitude = cursor.getDouble(latIndex);
86
87 int longIndex = cursor.getColumnIndex((LocationEntry.COLUMN_COORD_LONG));
88 double longitude = cursor.getDouble(longIndex);
89
90 // Hooray, data was returned! Assert that it's the right data, and that the database
91 // creation code is working as intended.
92 // Then take a break. We both know that wasn't easy.
93 assertEquals(testCityName, name);
94 assertEquals(testLocationSetting, location);
95 assertEquals(testLatitude, latitude);
96 assertEquals(testLongitude, longitude);
97
98 // Fantastic. Now that we have a location, add some weather!
99 } else {
100 // That's weird, it works on MY machine...
101 fail("No values returned :(");
102 }
103
104 // Fantastic. Now that we have a location, add some weather!
105 ContentValues weatherValues = new ContentValues();
106 weatherValues.put(WeatherEntry.COLUMN_LOC_KEY, locationRowId);
107 weatherValues.put(WeatherEntry.COLUMN_DATETEXT, "20141205");
108 weatherValues.put(WeatherEntry.COLUMN_DEGREES, 1.1);
109 weatherValues.put(WeatherEntry.COLUMN_HUMIDITY, 1.2);
110 weatherValues.put(WeatherEntry.COLUMN_PRESSURE, 1.3);
111 weatherValues.put(WeatherEntry.COLUMN_MAX_TEMP, 75);
112 weatherValues.put(WeatherEntry.COLUMN_MIN_TEMP, 65);
113 weatherValues.put(WeatherEntry.COLUMN_SHORT_DESC, "Asteroids");
114 weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED, 5.5);
115 weatherValues.put(WeatherEntry.COLUMN_WEATHER_ID, 321);
116
117 long weatherRowId = db.insert(WeatherEntry.TABLE_NAME, null, weatherValues);
118 assertTrue(weatherRowId != -1);
119
120 // A cursor is your primary interface to the query results.
121 Cursor weatherCursor = db.query(
122 WeatherEntry.TABLE_NAME, // Table to Query
123 null, // leaving "columns" null just returns all the columns.
124 null, // cols for "where" clause
125 null, // values for "where" clause
126 null, // columns to group by
127 null, // columns to filter by row groups
128 null // sort order
129 );
130
131 if (!weatherCursor.moveToFirst()) {
132 fail("No weather data returned!");
133 }
134
135 assertEquals(weatherCursor.getInt(
136 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_LOC_KEY)), locationRowId);
137 assertEquals(weatherCursor.getString(
138 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_DATETEXT)), "20141205");
139 assertEquals(weatherCursor.getDouble(
140 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_DEGREES)), 1.1);
141 assertEquals(weatherCursor.getDouble(
142 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_HUMIDITY)), 1.2);
143 assertEquals(weatherCursor.getDouble(
144 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_PRESSURE)), 1.3);
145 assertEquals(weatherCursor.getInt(
146 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_MAX_TEMP)), 75);
147 assertEquals(weatherCursor.getInt(
148 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_MIN_TEMP)), 65);
149 assertEquals(weatherCursor.getString(
150 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_SHORT_DESC)), "Asteroids");
151 assertEquals(weatherCursor.getDouble(
152 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_WIND_SPEED)), 5.5);
153 assertEquals(weatherCursor.getInt(
154 weatherCursor.getColumnIndex(WeatherEntry.COLUMN_WEATHER_ID)), 321);
155
156 weatherCursor.close();
157 dbHelper.close();
158 }
159 }