Skip to content
Browse files

Add tests to check inserting and updating with nullable fields

  • Loading branch information...
1 parent 5731097 commit 15df3662aa443e3549dbd5bee2fe92bab34bc0e5 Dmitrii Nikitin committed
View
3 ...ava/com/pushtorefresh/storio/contentresolver/integration/IntegrationSQLiteOpenHelper.java
@@ -17,7 +17,8 @@ public IntegrationSQLiteOpenHelper(@NonNull Context context) {
public void onCreate(@NonNull SQLiteDatabase db) {
db.execSQL("create table " + TABLE_TEST_ITEMS + "("
+ TestItem.COLUMN_ID + " INTEGER PRIMARY KEY, "
- + TestItem.COLUMN_VALUE + " TEXT NOT NULL"
+ + TestItem.COLUMN_VALUE + " TEXT NOT NULL, "
+ + TestItem.COLUMN_OPTIONAL_VALUE + " TEXT"
+ ");");
}
View
103 .../src/test/java/com/pushtorefresh/storio/contentresolver/integration/PutOperationTest.java
@@ -479,4 +479,107 @@ public void updateObjectAsSingle() {
changesTestSubscriber.assertNoErrors();
changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
+
+ @Test
+ public void insertOneWithNullField() {
+ TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
+
+ storIOContentResolver
+ .observeChangesOfUri(TestItem.CONTENT_URI)
+ .take(1)
+ .subscribe(changesTestSubscriber);
+
+ TestItem testItem = TestItem.create(null, "value", null); // optional value is null
+
+ PutResult insertResult = storIOContentResolver
+ .put()
+ .object(testItem)
+ .prepare()
+ .executeAsBlocking();
+
+ assertThat(insertResult.wasInserted()).isTrue();
+
+ Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
+
+ Assertions.assertThat(cursor).hasCount(1);
+
+ cursor.moveToFirst();
+
+ assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
+
+ cursor.close();
+
+ changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
+ changesTestSubscriber.assertNoErrors();
+ changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
+ }
+
+ @Test
+ public void updateNullFieldToNotNull() {
+ TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
+
+ storIOContentResolver
+ .observeChangesOfUri(TestItem.CONTENT_URI)
+ .take(2)
+ .subscribe(changesTestSubscriber);
+
+ Uri insertedUri =
+ contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value", null).toContentValues()); // firstly, optional value is null
+
+ TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value", "optionalValue"); // change to not null
+
+ PutResult updateResult = storIOContentResolver
+ .put()
+ .object(testItem)
+ .prepare()
+ .executeAsBlocking();
+
+ assertThat(updateResult.wasUpdated()).isTrue();
+
+ Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
+
+ Assertions.assertThat(cursor).hasCount(1);
+
+ cursor.moveToFirst();
+
+ assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
+
+ changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
+ changesTestSubscriber.assertNoErrors();
+ changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
+ }
+
+ @Test
+ public void updateNotNullFieldToNull() {
+ TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
+
+ storIOContentResolver
+ .observeChangesOfUri(TestItem.CONTENT_URI)
+ .take(2)
+ .subscribe(changesTestSubscriber);
+
+ Uri insertedUri = contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value", "optionalValue").toContentValues());
+
+ TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value", null); // optional value changes to null
+
+ PutResult updateResult = storIOContentResolver
+ .put()
+ .object(testItem)
+ .prepare()
+ .executeAsBlocking();
+
+ assertThat(updateResult.wasUpdated()).isTrue();
+
+ Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
+
+ Assertions.assertThat(cursor).hasCount(1);
+
+ cursor.moveToFirst();
+
+ assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
+
+ changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
+ changesTestSubscriber.assertNoErrors();
+ changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
+ }
}
View
38 ...resolver/src/test/java/com/pushtorefresh/storio/contentresolver/integration/TestItem.java
@@ -30,15 +30,22 @@
@NonNull
static final String COLUMN_VALUE = "value";
+ @NonNull
+ static final String COLUMN_OPTIONAL_VALUE = "optional_value";
+
@Nullable
private final Long id;
@NonNull
private final String value;
- private TestItem(@Nullable Long id, @NonNull String value) {
+ @Nullable
+ private final String optionalValue;
+
+ private TestItem(@Nullable Long id, @NonNull String value, @Nullable String optionalValue) {
this.id = id;
this.value = value;
+ this.optionalValue = optionalValue;
}
@Nullable
@@ -51,6 +58,11 @@ public String value() {
return value;
}
+ @Nullable
+ public String optionalValue() {
+ return optionalValue;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -59,17 +71,16 @@ public boolean equals(Object o) {
TestItem testItem = (TestItem) o;
if (id != null ? !id.equals(testItem.id) : testItem.id != null) return false;
- return value.equals(testItem.value);
- }
+ if (!value.equals(testItem.value)) return false;
+ return !(optionalValue != null ? !optionalValue.equals(testItem.optionalValue) : testItem.optionalValue != null);
- public boolean equalsWithoutId(@NonNull TestItem object) {
- return value.equals(object.value);
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + value.hashCode();
+ result = 31 * result + (optionalValue != null ? optionalValue.hashCode() : 0);
return result;
}
@@ -78,29 +89,42 @@ public String toString() {
return "TestItem{" +
"id=" + id +
", value='" + value + '\'' +
+ ", optionalValue='" + optionalValue + '\'' +
'}';
}
+ public boolean equalsWithoutId(@NonNull TestItem another) {
+ if (!value.equals(another.value)) return false;
+ return !(optionalValue != null ? !optionalValue.equals(another.optionalValue) : another.optionalValue != null);
+ }
+
@NonNull
ContentValues toContentValues() {
final ContentValues cv = new ContentValues(2);
cv.put(COLUMN_ID, id);
cv.put(COLUMN_VALUE, value);
+ cv.put(COLUMN_OPTIONAL_VALUE, optionalValue);
return cv;
}
@NonNull
static TestItem create(@Nullable Long id, @NonNull String value) {
- return new TestItem(id, value);
+ return create(id, value, null);
+ }
+
+ @NonNull
+ static TestItem create(@Nullable Long id, @NonNull String value, @Nullable String optionalValue) {
+ return new TestItem(id, value, optionalValue);
}
@NonNull
static TestItem fromCursor(@NonNull Cursor cursor) {
return create(
cursor.getLong(cursor.getColumnIndex(COLUMN_ID)),
- cursor.getString(cursor.getColumnIndex(COLUMN_VALUE))
+ cursor.getString(cursor.getColumnIndex(COLUMN_VALUE)),
+ cursor.getString(cursor.getColumnIndex(COLUMN_OPTIONAL_VALUE))
);
}
View
19 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/integration/InsertTest.java
@@ -76,4 +76,23 @@ public void insertAndDeleteTwice() {
cursorAfterDelete.close();
}
}
+
+ @Test
+ public void insertOneWithNullField() {
+ User user = User.newInstance(null, "user@example.com", null); // phone is null
+ putUserBlocking(user);
+
+ final Cursor cursor = db.query(UserTableMeta.TABLE, null, null, null, null, null, null);
+
+ // asserting that values was really inserted to db
+ assertThat(cursor.getCount()).isEqualTo(1);
+ assertThat(cursor.moveToFirst()).isTrue();
+
+ final User insertedUser = UserTableMeta.GET_RESOLVER.mapFromCursor(cursor);
+
+ assertThat(insertedUser.id()).isNotNull();
+ assertThat(user.equalsExceptId(insertedUser)).isTrue();
+
+ cursor.close();
+ }
}
View
2 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/integration/RxQueryTest.java
@@ -255,7 +255,7 @@ public void queryOneNonExistedObjectObservable() {
@Test
public void queryOneExistedObjectTableUpdate() {
- User expectedUser = new User(null, "such@email.com");
+ User expectedUser = User.newInstance(null, "such@email.com");
putUsersBlocking(3);
final Observable<User> userObservable = storIOSQLite
View
74 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/integration/UpdateTest.java
@@ -1,6 +1,7 @@
package com.pushtorefresh.storio.sqlite.integration;
import android.database.Cursor;
+import android.support.annotation.NonNull;
import com.pushtorefresh.storio.sqlite.BuildConfig;
import com.pushtorefresh.storio.sqlite.operations.put.PutResult;
@@ -22,38 +23,47 @@
@Test
public void updateOne() {
- final User userForInsert = TestFactory.newUser();
-
- final PutResult insertResult = storIOSQLite
- .put()
- .object(userForInsert)
- .prepare()
- .executeAsBlocking();
-
- assertThat(insertResult.wasInserted()).isTrue();
+ final User userForInsert = putUserBlocking();
final User userForUpdate = User.newInstance(
userForInsert.id(), // using id of inserted user
"new@email.com" // new value
);
- final PutResult updateResult = storIOSQLite
- .put()
- .object(userForUpdate)
- .prepare()
- .executeAsBlocking();
+ updateUserBlocking(userForUpdate);
+ checkOnlyOneItemInStorage(userForUpdate); // update should not add new rows!
+ }
- assertThat(updateResult.wasUpdated()).isTrue();
+ @Test
+ public void updateNullFieldToNotNull() {
+ final User userForInsert = User.newInstance(null, "user@email.com", null); // phone is null
- final Cursor cursor = db.query(UserTableMeta.TABLE, null, null, null, null, null, null);
+ putUserBlocking(userForInsert);
- assertThat(cursor.getCount()).isEqualTo(1); // update should not add new rows!
- assertThat(cursor.moveToFirst()).isTrue();
+ final User userForUpdate = User.newInstance(
+ userForInsert.id(),
+ userForInsert.email(),
+ "1-999-547867" // phone not null
+ );
- final User updatedUser = UserTableMeta.GET_RESOLVER.mapFromCursor(cursor);
- assertThat(updatedUser).isEqualTo(userForUpdate);
+ updateUserBlocking(userForUpdate);
+ checkOnlyOneItemInStorage(userForUpdate);
+ }
- cursor.close();
+ @Test
+ public void updateNotNullFieldToNull() {
+ final User userForInsert = User.newInstance(null, "user@email.com", "1-999-547867"); // phone not null
+
+ putUserBlocking(userForInsert);
+
+ final User userForUpdate = User.newInstance(
+ userForInsert.id(),
+ userForInsert.email(),
+ null // phone is null
+ );
+
+ updateUserBlocking(userForUpdate);
+ checkOnlyOneItemInStorage(userForUpdate);
}
@Test
@@ -93,4 +103,26 @@ public void updateCollection() {
cursor.close();
}
+
+ private void updateUserBlocking(@NonNull final User userForUpdate) {
+ final PutResult updateResult = storIOSQLite
+ .put()
+ .object(userForUpdate)
+ .prepare()
+ .executeAsBlocking();
+
+ assertThat(updateResult.wasUpdated()).isTrue();
+ }
+
+ private void checkOnlyOneItemInStorage(@NonNull final User user) {
+ final Cursor cursor = db.query(UserTableMeta.TABLE, null, null, null, null, null, null);
+
+ assertThat(cursor.getCount()).isEqualTo(1);
+ assertThat(cursor.moveToFirst()).isTrue();
+
+ final User updatedUser = UserTableMeta.GET_RESOLVER.mapFromCursor(cursor);
+ assertThat(updatedUser).isEqualTo(user);
+
+ cursor.close();
+ }
}
View
32 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/integration/User.java
@@ -5,19 +5,27 @@
public class User implements Comparable<User> {
+ @Nullable
+ private Long id;
@NonNull
private final String email;
@Nullable
- private Long id;
+ private final String phone;
- User(@Nullable Long id, @NonNull String email) {
+ User(@Nullable Long id, @NonNull String email, @Nullable String phone) {
this.id = id;
this.email = email;
+ this.phone = phone;
}
@NonNull
public static User newInstance(@Nullable Long id, @NonNull String email) {
- return new User(id, email);
+ return newInstance(id, email, null);
+ }
+
+ @NonNull
+ public static User newInstance(@Nullable Long id, @NonNull String email, @Nullable String phone) {
+ return new User(id, email, phone);
}
@Nullable
@@ -30,6 +38,11 @@ public String email() {
return email;
}
+ @Nullable
+ public String phone() {
+ return phone;
+ }
+
public void setId(@Nullable Long id) {
this.id = id;
}
@@ -42,26 +55,31 @@ public boolean equals(Object o) {
User user = (User) o;
if (id != null ? !id.equals(user.id) : user.id != null) return false;
- return email.equals(user.email);
+ if (!email.equals(user.email)) return false;
+ return !(phone != null ? !phone.equals(user.phone) : user.phone != null);
+
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + email.hashCode();
+ result = 31 * result + (phone != null ? phone.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "User{" +
- "email='" + email + '\'' +
- ", id=" + id +
+ "id=" + id +
+ ", email='" + email + '\'' +
+ ", phone='" + phone + '\'' +
'}';
}
public boolean equalsExceptId(@NonNull User another) {
- return email.equals(another.email);
+ if (!email.equals(another.email)) return false;
+ return !(phone != null ? !phone.equals(another.phone) : another.phone != null);
}
@Override
View
8 storio-sqlite/src/test/java/com/pushtorefresh/storio/sqlite/integration/UserTableMeta.java
@@ -23,11 +23,13 @@
static final String TABLE = "users";
static final String COLUMN_ID = "_id";
static final String COLUMN_EMAIL = "email";
+ static final String COLUMN_PHONE = "phone";
// We all will be very old when Java will support string interpolation :(
static final String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY, " +
- COLUMN_EMAIL + " TEXT NOT NULL" +
+ COLUMN_EMAIL + " TEXT NOT NULL, " +
+ COLUMN_PHONE + " TEXT" + // optional
");";
static final Query QUERY_ALL = Query.builder()
@@ -64,6 +66,7 @@ protected ContentValues mapToContentValues(@NonNull User user) {
contentValues.put(COLUMN_ID, user.id());
contentValues.put(COLUMN_EMAIL, user.email());
+ contentValues.put(COLUMN_PHONE, user.phone());
return contentValues;
}
@@ -86,7 +89,8 @@ public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull User ob
public User mapFromCursor(@NonNull Cursor cursor) {
return User.newInstance(
cursor.getLong(cursor.getColumnIndex(COLUMN_ID)),
- cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL))
+ cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL)),
+ cursor.getString(cursor.getColumnIndex(COLUMN_PHONE))
);
}
};

0 comments on commit 15df366

Please sign in to comment.
Something went wrong with that request. Please try again.