11
22package novoda .lib .sqliteprovider ;
33
4+ import android .content .ContentValues ;
45import android .database .Cursor ;
6+ import android .database .sqlite .SQLiteDatabase ;
57import android .net .Uri ;
68import android .test .AndroidTestCase ;
79
10+ import java .io .IOException ;
11+
12+ import novoda .lib .sqliteprovider .sqlite .ExtendedSQLiteOpenHelper ;
13+
814public class ContentProviderTest extends AndroidTestCase {
915
16+ private static final String TABLE_NAME = "test" ;
17+ private static final String COLUMN_PRIMARY_KEY = "column_primary_key" ;
18+ private static final String ANY_COLUMN = "any_column" ;
19+
1020 public ContentProviderTest () {
1121 super ();
1222 }
@@ -18,4 +28,45 @@ public void testMapping() throws Exception {
1828 assertTrue (cursor .getColumnIndexOrThrow ("childs__id" ) > -1 );
1929 assertTrue (cursor .getColumnIndexOrThrow ("parents_name" ) > -1 );
2030 }
31+
32+ public void test_GivenATableWithData_When_UpdatingNonexistentRow_Then_NumberOfAffectedRowsShouldBeZero () throws Exception {
33+ givenATableWithData ();
34+
35+ ContentValues values = new ContentValues (1 );
36+ values .put (ANY_COLUMN , 1 );
37+
38+ int numRows = new ExtendedSQLiteOpenHelper (getContext ())
39+ .getWritableDatabase ()
40+ .update (TABLE_NAME , values , COLUMN_PRIMARY_KEY + "=?" , new String []{"2" });
41+
42+ assertEquals (0 , numRows );
43+
44+ numRows = getContext ().getContentResolver ().update (
45+ Uri .parse ("content://novoda.lib.sqliteprovider.test/" + TABLE_NAME ),
46+ values ,
47+ COLUMN_PRIMARY_KEY + "=?" ,
48+ new String []{"2" });
49+
50+ assertEquals (0 , numRows );
51+ }
52+
53+ private void givenATableWithData () throws IOException {
54+ ExtendedSQLiteOpenHelper helper = new ExtendedSQLiteOpenHelper (getContext ());
55+ SQLiteDatabase db = helper .getWritableDatabase ();
56+ db .execSQL ("CREATE TABLE IF NOT EXISTS `" + TABLE_NAME + "` (" +
57+ "`" + COLUMN_PRIMARY_KEY + "` INTEGER PRIMARY KEY," +
58+ "`" + ANY_COLUMN + "` INTEGER UNIQUE)" );
59+
60+ ContentValues values = new ContentValues (2 );
61+ values .put (COLUMN_PRIMARY_KEY , 1 );
62+ values .put (ANY_COLUMN , 1 );
63+ helper .getWritableDatabase ().insert (TABLE_NAME , null , values );
64+ }
65+
66+ @ Override
67+ protected void tearDown () throws Exception {
68+ ExtendedSQLiteOpenHelper helper = new ExtendedSQLiteOpenHelper (getContext ());
69+ SQLiteDatabase db = helper .getWritableDatabase ();
70+ db .execSQL ("DROP TABLE IF EXISTS `" + TABLE_NAME + "`" );
71+ }
2172}
0 commit comments