1 package uk
.me
.njae
.sunshine
;
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
;
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
;
16 public class TestDb
extends AndroidTestCase
{
18 public static final String LOG_TAG
= TestDb
.class.getSimpleName();
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());
28 public void testInsertReadDb() {
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;
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();
41 // Create a new map of values, where column names are the keys
42 ContentValues locationTestValues
= createNorthPoleLocationValues();
45 locationRowId
= db
.insert(LocationEntry
.TABLE_NAME
, null, locationTestValues
);
47 // Verify we got a row back.
48 assertTrue(locationRowId
!= -1);
49 Log
.d(LOG_TAG
, "New row id: " + locationRowId
);
51 // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made
54 // A cursor is your primary interface to the query results.
55 Cursor cursor
= db
.query(
56 LocationEntry
.TABLE_NAME
, // Table to Query
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
65 validateCursor(cursor
, locationTestValues
);
67 // Fantastic. Now that we have a location, add some weather!
69 ContentValues weatherTestValues
= createWeatherValues(locationRowId
);
71 long weatherRowId
= db
.insert(WeatherEntry
.TABLE_NAME
, null, weatherTestValues
);
72 assertTrue(weatherRowId
!= -1);
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
85 validateCursor(weatherCursor
, weatherTestValues
);
87 weatherCursor
.close();
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);
104 return weatherValues
;
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);
118 static void validateCursor(Cursor valueCursor
, ContentValues expectedValues
) {
120 assertTrue(valueCursor
.moveToFirst());
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
));