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 static final String TEST_LOCATION
= "99705";
21 static final String TEST_DATE
= "20141205";
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());
31 public void testInsertReadDb() {
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();
38 // Create a new map of values, where column names are the keys
39 ContentValues locationTestValues
= createNorthPoleLocationValues();
42 locationRowId
= db
.insert(LocationEntry
.TABLE_NAME
, null, locationTestValues
);
44 // Verify we got a row back.
45 assertTrue(locationRowId
!= -1);
46 Log
.d(LOG_TAG
, "New row id: " + locationRowId
);
48 // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made
51 // A cursor is your primary interface to the query results.
52 Cursor cursor
= db
.query(
53 LocationEntry
.TABLE_NAME
, // Table to Query
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
62 validateCursor(cursor
, locationTestValues
);
64 // Fantastic. Now that we have a location, add some weather!
66 ContentValues weatherTestValues
= createWeatherValues(locationRowId
);
68 long weatherRowId
= db
.insert(WeatherEntry
.TABLE_NAME
, null, weatherTestValues
);
69 assertTrue(weatherRowId
!= -1);
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
82 validateCursor(weatherCursor
, weatherTestValues
);
84 weatherCursor
.close();
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);
101 return weatherValues
;
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);
115 static void validateCursor(Cursor valueCursor
, ContentValues expectedValues
) {
117 assertTrue(valueCursor
.moveToFirst());
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
));