Skip to content

Instantly share code, notes, and snippets.

@AntonioDiaz
Last active December 29, 2016 09:58
Show Gist options
  • Save AntonioDiaz/48b28d3d6f4a976f7e08e8d33b08fe83 to your computer and use it in GitHub Desktop.
Save AntonioDiaz/48b28d3d6f4a976f7e08e8d33b08fe83 to your computer and use it in GitHub Desktop.
Testing db in android app.
package com.example.android.sunshine.app.data;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;
import java.util.HashSet;
public class TestDb extends AndroidTestCase {
public static final String LOG_TAG = TestDb.class.getSimpleName();
// Since we want each test to start with a clean slate
void deleteTheDatabase() {
mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME);
}
public void setUp() {
deleteTheDatabase();
}
public void testCreateDb() throws Throwable { .. }
public void testLocationTable() { .. }
public void testUpdateLocationAux(){
WeatherDbHelper weatherDbHelper = new WeatherDbHelper(this.mContext);
SQLiteDatabase db = weatherDbHelper.getWritableDatabase();
/** insert value */
ContentValues values = TestUtilities.createNorthPoleLocationValues();
long newId = db.insert(LocationEntry.TABLE_NAME, null, values);
String whereClause = "_id = ? ";
String [] whereArgs = new String[] {Long.toString(newId)};
Cursor cursorAux = db.query(WeatherContract.LocationEntry.TABLE_NAME, null, whereClause, whereArgs, null, null, null);
assertEquals("Testing insert...", 1, cursorAux.getCount());
TestUtilities.validateCursor("testUpdateLocation. Error validating location entry update.", cursorAux, values);
cursorAux.close();
db.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment