-
Notifications
You must be signed in to change notification settings - Fork 75
Fix update behaviour in content provider implementation #62
Changes from 3 commits
360e1ec
e1d5273
42db835
93f6626
344a624
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,22 @@ | ||
|
|
||
| package novoda.lib.sqliteprovider; | ||
|
|
||
| import android.content.ContentValues; | ||
| import android.database.Cursor; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.net.Uri; | ||
| import android.test.AndroidTestCase; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelper; | ||
|
|
||
| public class ContentProviderTest extends AndroidTestCase { | ||
|
|
||
| private static final String TABLE_NAME = "test"; | ||
| private static final String COLUMN_PRIMARY_KEY = "column_primary_key"; | ||
| private static final String COLUMN2 = "column2"; | ||
|
|
||
| public ContentProviderTest() { | ||
| super(); | ||
| } | ||
|
|
@@ -18,4 +28,46 @@ public void testMapping() throws Exception { | |
| assertTrue(cursor.getColumnIndexOrThrow("childs__id") > -1); | ||
| assertTrue(cursor.getColumnIndexOrThrow("parents_name") > -1); | ||
| } | ||
|
|
||
| public void test_GivenATableWithData_When_UpdatingNonexistentRow_Then_NumberOfAffectedRowsShouldBeZero() throws Exception { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| givenATableWithData(); | ||
|
|
||
| ContentValues values = new ContentValues(1); | ||
| values.put(COLUMN2, 1); | ||
|
|
||
| int numRows = new ExtendedSQLiteOpenHelper(getContext()) | ||
| .getWritableDatabase() | ||
| .update(TABLE_NAME, values, COLUMN_PRIMARY_KEY + "=?", new String[]{"2"}); | ||
|
|
||
| assertEquals(0, numRows); | ||
|
|
||
| numRows = getContext().getContentResolver().update( | ||
| Uri.parse("content://novoda.lib.sqliteprovider.test/" + TABLE_NAME), | ||
| values, | ||
| COLUMN_PRIMARY_KEY + "=?", | ||
| new String[]{"2"}); | ||
|
|
||
| assertEquals(0, numRows); | ||
| } | ||
|
|
||
| private void givenATableWithData() throws IOException { | ||
| ExtendedSQLiteOpenHelper helper = new ExtendedSQLiteOpenHelper(getContext()); | ||
| SQLiteDatabase db = helper.getWritableDatabase(); | ||
| db.execSQL("CREATE TABLE IF NOT EXISTS `" + TABLE_NAME + "` (" + | ||
| "`" + COLUMN_PRIMARY_KEY + "` INTEGER PRIMARY KEY," + | ||
| "`" + COLUMN2 + "` INTEGER UNIQUE)"); | ||
|
|
||
| ContentValues values = new ContentValues(2); | ||
| values.put(COLUMN_PRIMARY_KEY, 1); | ||
| values.put(COLUMN2, 1); | ||
| helper.getWritableDatabase().insert(TABLE_NAME, null, values); | ||
| } | ||
|
|
||
| @Override | ||
| protected void tearDown() throws Exception { | ||
| super.tearDown(); | ||
|
||
| ExtendedSQLiteOpenHelper helper = new ExtendedSQLiteOpenHelper(getContext()); | ||
| SQLiteDatabase db = helper.getWritableDatabase(); | ||
| db.execSQL("DROP TABLE IF EXISTS `" + TABLE_NAME + "`"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,7 +143,7 @@ private void onQueryOf(String tableName, CursorOperations cursorOperations) { | |
| } | ||
| } | ||
|
|
||
| private static interface CursorOperations { | ||
| private interface CursorOperations { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| void doOperationsOn(Cursor cursor); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ | |
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.DatabaseUtils; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
| import android.net.Uri; | ||
|
|
@@ -96,14 +95,12 @@ private Uri insertSilently(Uri uri, ContentValues values) { | |
| protected int updateInTransaction(Uri uri, ContentValues values, String selection, String[] selectionArgs) { | ||
| ContentValues insertValues = (values != null) ? new ContentValues(values) : new ContentValues(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i know this was already here but afaik we don't need to do this and can actually pass the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following exception is thrown if I let null in there:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That means that the null guard is useless, as using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I think that by "null is a possible value" they mean that a null value in the map is possible, not that the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, maybe it's that 👍 weird choice of words, still. |
||
|
|
||
| int rowId = getWritableDatabase().update(UriUtils.getItemDirID(uri), insertValues, selection, selectionArgs); | ||
| int rowsAffected = getWritableDatabase().update(UriUtils.getItemDirID(uri), insertValues, selection, selectionArgs); | ||
|
|
||
| if (rowId > 0) { | ||
| Uri insertUri = ContentUris.withAppendedId(uri, rowId); | ||
| notifyUriChange(insertUri); | ||
| return rowId; | ||
| if (rowsAffected > 0) { | ||
| notifyUriChange(uri); | ||
| } | ||
| throw new SQLException("Failed to update row into " + uri + " because it does not exists."); | ||
| return rowsAffected; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have a better name or is there a specific reason for this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No specific reason for this one but we couldn't find a better name because the data there is really not relevant for the test.
Suggestions welcome!
A_COLUMNANOTHER_COLUMNA_COLUMN_HAS_NO_NAME?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so if it's not relevant you can name it
ANY_COLUMNorANY_COLUMN_NAMEThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for
A_COLUMN_HAS_NO_NAME😂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why didn't I think of that before? 👍 for
A_COLUMN_HAS_NO_NAMEThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 93f6626. The column has a name 😛