Merge "Add more relation tests" into androidx-master-dev
diff --git a/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/PojoWithNullRelationKeyTest.java b/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/PojoWithNullRelationKeyTest.java
index ea71c298..58940ebf 100644
--- a/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/PojoWithNullRelationKeyTest.java
+++ b/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/PojoWithNullRelationKeyTest.java
@@ -17,6 +17,7 @@
package androidx.room.integration.testapp.test;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import androidx.room.Dao;
@@ -74,22 +75,70 @@
// Pojo with null relationship key doesn't have any children.
assertThat(loaded.get(0).parentEntity.parentId, is(0));
assertThat(loaded.get(0).childrenEntities.size(), is(0));
+ assertThat(loaded.get(0).parentEntity.name, is("parent 0"));
// Pojo with non-null relationship has 1 child.
assertThat(loaded.get(1).parentEntity.parentId, is(1));
assertThat(loaded.get(1).childrenEntities.size(), is(1));
+ assertThat(loaded.get(1).parentEntity.name, is("parent 1"));
+ assertThat(loaded.get(1).childrenEntities.get(0).name, is("child 0"));
+
+ List<ParentWithChildren> withOneChild = mDao.getAllParentWithChildren();
+ assertThat(withOneChild.size(), is(2));
+ assertThat(withOneChild.get(0).parentEntity.parentId, is(0));
+ }
+
+ @Test
+ public void parentWithOneOptionalChild() {
+ ParentEntity hasNoChild = createParent(0, null);
+ ParentEntity hasChild = createParent(1, 0);
+
+ mDao.insert(hasNoChild);
+ mDao.insert(hasChild);
+
+ ChildEntity child = createChild(0);
+
+ mDao.insert(child);
+
+ ParentWithChild resultWithoutChild = mDao.findParentsWithChild(0);
+ assertThat(resultWithoutChild.parentEntity.parentId, is(0));
+ assertThat(resultWithoutChild.parentEntity.name, is("parent 0"));
+ assertThat(resultWithoutChild.parentEntity.relationId, is(nullValue()));
+ assertThat(resultWithoutChild.childEntity, is(nullValue()));
+
+ ParentWithChild resultWithChild = mDao.findParentsWithChild(1);
+ assertThat(resultWithChild.parentEntity.parentId, is(1));
+ assertThat(resultWithChild.parentEntity.name, is("parent 1"));
+ assertThat(resultWithChild.parentEntity.relationId, is(0));
+ assertThat(resultWithChild.childEntity.name, is("child 0"));
+ assertThat(resultWithChild.childEntity.childId, is(0L));
+ }
+
+ @Test // exact repro for b/148240972
+ public void relationIsNullWithConflictingColumn() {
+ ParentEntity parent = createParent(1, 0);
+ parent.name = "hasName";
+ mDao.insert(parent);
+ ParentWithChild read = mDao.findParentsWithChild(1);
+ assertThat(read.parentEntity.name, is("hasName"));
+
+ List<ParentWithChild> readList = mDao.getAllParentWithChild();
+ assertThat(readList.size(), is(1));
+ assertThat(readList.get(0).parentEntity.name, is("hasName"));
}
private static ParentEntity createParent(int id, Integer relationId) {
ParentEntity item = new ParentEntity();
item.parentId = id;
item.relationId = relationId;
+ item.name = "parent " + id;
return item;
}
private static ChildEntity createChild(int id) {
ChildEntity item = new ChildEntity();
item.childId = id;
+ item.name = "child " + id;
return item;
}
@@ -110,6 +159,14 @@
@Transaction
@Query("SELECT * FROM ParentEntity")
List<ParentWithChildren> getAllParentWithChildren();
+
+ @Transaction
+ @Query("SELECT * FROM ParentEntity WHERE parentId = :parentId")
+ ParentWithChild findParentsWithChild(int parentId);
+
+ @Transaction
+ @Query("SELECT * FROM ParentEntity")
+ List<ParentWithChild> getAllParentWithChild();
}
@Entity
@@ -117,12 +174,14 @@
@PrimaryKey
public int parentId;
public Integer relationId;
+ public String name;
}
@Entity
static class ChildEntity {
@PrimaryKey
public long childId;
+ public String name;
}
static class ParentWithChildren {
@@ -131,4 +190,11 @@
@Relation(parentColumn = "relationId", entityColumn = "childId")
public List<ChildEntity> childrenEntities;
}
+
+ static class ParentWithChild {
+ @Embedded
+ public ParentEntity parentEntity;
+ @Relation(parentColumn = "relationId", entityColumn = "childId")
+ public ChildEntity childEntity;
+ }
}