Exemplo n.º 1
0
        /*
           This helper function Deletes all records from both database tables using the database
           functions only.  This is designed to be used to reset the state of the database until the
           Delete functionality is available in the ContentProvider.
         */
        public void DeleteAllRecordsFromDB()
        {
            var dbHelper = new WeatherDbOpenHelper (context);
            SQLiteDatabase db = dbHelper.WritableDatabase;

            db.Delete (WeatherContractOpen.WeatherEntryOpen.TABLE_NAME, null, null);
            db.Delete (WeatherContractOpen.LocationEntryOpen.TABLE_NAME, null, null);
            db.Close ();
        }
Exemplo n.º 2
0
        public void Create_Database()
        {
            HashSet<String> tableNameHashSet = new HashSet<String> ();
            tableNameHashSet.Add (WeatherContractOpen.LocationEntryOpen.TABLE_NAME);
            tableNameHashSet.Add (WeatherContractOpen.WeatherEntryOpen.TABLE_NAME);

            context.DeleteDatabase (WeatherDbOpenHelper.DATABASE_NAME);
            SQLiteDatabase db = new WeatherDbOpenHelper (
                                    this.context).WritableDatabase;
            Assert.AreEqual (true, db.IsOpen);

            // have we created the tables we want?
            c = db.RawQuery ("SELECT name FROM sqlite_master WHERE type='table'", null);

            Assert.IsTrue (c.MoveToFirst (), "Error: This means that the database has not been created correctly");

            // verify that the tables have been created
            do {
                tableNameHashSet.Remove (c.GetString (0));
            } while(c.MoveToNext ());

            // if this fails, it means that your database doesn't contain both the location entry
            // and weather entry tables
            Assert.IsTrue (tableNameHashSet.Count == 0, "Error: Your database was created without both the location entry and weather entry tables");

            // now, do our tables contain the correct columns?
            c = db.RawQuery ("PRAGMA table_info(" + WeatherContractOpen.LocationEntryOpen.TABLE_NAME + ")",
                null);

            Assert.IsTrue (c.MoveToFirst (), "Error: This means that we were unable to query the database for table information.");

            // Build a HashSet of all of the column names we want to look for
            HashSet<String> locationColumnHashSet = new HashSet<String> ();
            locationColumnHashSet.Add (WeatherContractOpen.LocationEntryOpen._ID);
            locationColumnHashSet.Add (WeatherContractOpen.LocationEntryOpen.COLUMN_CITY_NAME);
            locationColumnHashSet.Add (WeatherContractOpen.LocationEntryOpen.COLUMN_COORD_LAT);
            locationColumnHashSet.Add (WeatherContractOpen.LocationEntryOpen.COLUMN_COORD_LONG);
            locationColumnHashSet.Add (WeatherContractOpen.LocationEntryOpen.COLUMN_LOCATION_SETTING);

            int columnNameIndex = c.GetColumnIndex ("name");
            do {
                String columnName = c.GetString (columnNameIndex);
                locationColumnHashSet.Remove (columnName);
            } while(c.MoveToNext ());

            // if this fails, it means that your database doesn't contain all of the required location
            // entry columns
            Assert.IsTrue (locationColumnHashSet.Count == 0, "Error: The database doesn't contain all of the required location entry columns");
            db.Close ();
        }
Exemplo n.º 3
0
 /*
 Students: We've coded this for you.  We just create a new WeatherDbHelper for later use
 here.
  */
 public override bool OnCreate()
 {
     openHelper = new WeatherDbOpenHelper (context);
     return true;
 }
Exemplo n.º 4
0
        public void testWeatherTable()
        {
            // First insert the location, and then use the locationRowId to insert
            // the weather. Make sure to cover as many failure cases as you can.

            // Instead of rewriting all of the code we've already written in testLocationTable
            // we can move this code to insertLocation and then call insertLocation from both
            // tests. Why move it? We need the code to return the ID of the inserted location
            // and our testLocationTable can only return void because it's a test.

            // First step: Get reference to writable database
            var dbHelper = new WeatherDbOpenHelper (context);
            var db = dbHelper.WritableDatabase;

            // Create ContentValues of what you want to insert
            // (you can use the createWeatherValues TestUtilities function if you wish)

            // Insert ContentValues into database and get a row ID back
            TestUtilitiesOpen.insertWeatherValues (context);

            // Query the database and receive a Cursor back
            var rowInfo = db.Query (WeatherContractOpen.WeatherEntryOpen.TABLE_NAME, null, null, null, null, null, null);
            // Move the cursor to a valid database row
            Assert.IsTrue (rowInfo.MoveToFirst (), "No rows were inserted.");
            // Validate data in resulting Cursor with the original ContentValues
            // (you can use the validateCurrentRecord function in TestUtilities to validate the
            // query if you like)
            TestUtilitiesOpen.validateCurrentRecord ("Weather values not valid", rowInfo, TestUtilitiesOpen.createWeatherValues (1));

            Assert.IsFalse (rowInfo.MoveToNext (), "Error: More than one record was inserted.");
            // Finally, close the cursor and database
            rowInfo.Close ();
            db.Close ();
        }
Exemplo n.º 5
0
        public void testLocationTable()
        {
            // First step: Get reference to writable database
            var dbHelper = new WeatherDbOpenHelper (context);
            var db = dbHelper.WritableDatabase;
            // Create ContentValues of what you want to insert
            // (you can use the createNorthPoleLocationValues if you wish)
            var expectedValues = TestUtilitiesOpen.createNorthPoleLocationValues ();
            // Insert ContentValues into database and get a row ID back
            var rowId = TestUtilitiesOpen.insertNorthPoleLocationValues (context);
            Assert.IsTrue (rowId > 0, "Row was not inserted.");
            // Query the database and receive a Cursor back
            var rowInfo = db.Query (WeatherContractOpen.LocationEntryOpen.TABLE_NAME, null, null, null, null, null, null);
            // Move the cursor to a valid database row
            Assert.IsTrue (rowInfo.MoveToFirst (), "No data found.");
            // Validate data in resulting Cursor with the original ContentValues
            // (you can use the validateCurrentRecord function in TestUtilities to validate the
            // query if you like)
            TestUtilitiesOpen.validateCurrentRecord ("Record is not valid", rowInfo, expectedValues);

            //Ensure only one record exist
            Assert.IsFalse (rowInfo.MoveToNext (), "More than one record was returned.");
            // Finally, close the cursor and database
            rowInfo.Close ();
            db.Close ();
        }
Exemplo n.º 6
0
        public void testBasicWeatherQuery()
        {
            // insert our test records into the database
            var dbHelper = new WeatherDbOpenHelper (context);
            var db = dbHelper.WritableDatabase;

            ContentValues testValues = TestUtilitiesOpen.createNorthPoleLocationValues ();
            long locationRowId = TestUtilitiesOpen.insertNorthPoleLocationValues (context);

            // Fantastic.  Now that we have a location, add some weather!
            ContentValues weatherValues = TestUtilitiesOpen.createWeatherValues (locationRowId);

            long weatherRowId = db.Insert (WeatherApp.WeatherContractOpen.WeatherEntryOpen.TABLE_NAME, null, weatherValues);
            Assert.IsTrue (weatherRowId != -1, "Unable to Insert WeatherEntry into the Database");

            db.Close ();

            // Test the basic content provider Query
            var weather = context.ContentResolver.Query (
                              WeatherApp.WeatherContractOpen.WeatherEntryOpen.CONTENT_URI,
                              null,
                              null,
                              null,
                              null
                          );
            Assert.IsTrue (weather.MoveToFirst (), "Cursor is empty");
            // Make sure we get the correct cursor out of the database
            TestUtilitiesOpen.validateCurrentRecord ("testBasicWeatherQuery", weather, weatherValues);
        }
Exemplo n.º 7
0
        public void testBasicLocationQueries()
        {
            // insert our test records into the database
            var dbHelper = new WeatherDbOpenHelper (context);
            SQLiteDatabase db = dbHelper.WritableDatabase;

            ContentValues testValues = TestUtilitiesOpen.createNorthPoleLocationValues ();
            long locationRowId = TestUtilitiesOpen.insertNorthPoleLocationValues (context);

            // Test the basic content provider Query
            var location = context.ContentResolver.Query (
                               WeatherApp.WeatherContractOpen.LocationEntryOpen.CONTENT_URI,
                               null,
                               null,
                               null,
                               null
                           );
            Assert.IsTrue (location.MoveToFirst (), "Cursor is empty");

            // Make sure we get the correct cursor out of the database
            TestUtilitiesOpen.validateCurrentRecord ("testBasicLocationQueries, location Query", location, testValues);

            // Has the NotificationUri been set correctly? --- we can only test this easily against API
            // level 19 or greater because getNotificationUri was added in API level 19.
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) {
                Assert.AreEqual (location.NotificationUri.ToString (), WeatherApp.WeatherContractOpen.LocationEntryOpen.CONTENT_URI.ToString (), "Error: Location Query did not properly set NotificationUri");
            }
        }