diff --git a/app/src/androidTest/java/org/tasks/data/TagDataDaoTest.java b/app/src/androidTest/java/org/tasks/data/TagDataDaoTest.java index 9852b988d..beb74e1dc 100644 --- a/app/src/androidTest/java/org/tasks/data/TagDataDaoTest.java +++ b/app/src/androidTest/java/org/tasks/data/TagDataDaoTest.java @@ -6,7 +6,6 @@ import static java.util.Collections.singletonList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.tasks.makers.TagDataMaker.NAME; -import static org.tasks.makers.TagDataMaker.UID; import static org.tasks.makers.TagDataMaker.newTagData; import static org.tasks.makers.TagMaker.TAGDATA; import static org.tasks.makers.TagMaker.TAGUID; @@ -105,7 +104,6 @@ public class TagDataDaoTest extends InjectingTestCase { newTag(1, "tag1", "tag2"); newTag(2, "tag2", "tag3"); - assertEquals( newHashSet("tag1", "tag3"), tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).first); } @@ -123,8 +121,7 @@ public class TagDataDaoTest extends InjectingTestCase { newTag(1, "tag1", "tag2"); newTag(2, "tag2", "tag3"); - assertEquals( - newHashSet("tag2"), tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).second); + assertEquals(newHashSet("tag2"), tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).second); } @Test @@ -135,6 +132,26 @@ public class TagDataDaoTest extends InjectingTestCase { assertTrue(tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).second.isEmpty()); } + @Test + public void getSelectionsWithNoTags() { + newTag(1); + + Pair, Set> selections = tagDataDao.getTagSelections(ImmutableList.of(1L)); + assertTrue(selections.first.isEmpty()); + assertTrue(selections.second.isEmpty()); + } + + @Test + public void noCommonSelectionsWhenOneTaskHasNoTags() { + newTag(1, "tag1"); + newTag(2); + + Pair, Set> selections = + tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)); + assertEquals(newHashSet("tag1"), selections.first); + assertTrue(selections.second.isEmpty()); + } + private void newTag(long taskId, String... tags) { Task task = newTask(with(ID, taskId)); taskDao.createNew(task); diff --git a/app/src/main/java/org/tasks/data/TagDataDao.java b/app/src/main/java/org/tasks/data/TagDataDao.java index 0194cfb38..d234489b5 100644 --- a/app/src/main/java/org/tasks/data/TagDataDao.java +++ b/app/src/main/java/org/tasks/data/TagDataDao.java @@ -5,6 +5,7 @@ import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Sets.difference; import static com.google.common.collect.Sets.newHashSet; +import static java.util.Collections.emptySet; import static org.tasks.db.DbUtils.MAX_SQLITE_ARGS; import static org.tasks.db.DbUtils.batch; @@ -76,11 +77,12 @@ public abstract class TagDataDao { public Pair, Set> getTagSelections(List tasks) { List allTags = getAllTags(tasks); - Collection> tags = Collections2.transform(allTags, t -> newHashSet(t.split(","))); + Collection> tags = + Collections2.transform(allTags, t -> t == null ? emptySet() : newHashSet(t.split(","))); Set partialTags = newHashSet(concat(tags)); Set commonTags = null; if (tags.isEmpty()) { - commonTags = Collections.emptySet(); + commonTags = emptySet(); } else { for (Set s : tags) { if (commonTags == null) { @@ -95,8 +97,8 @@ public abstract class TagDataDao { } @Query( - "SELECT IFNULL(GROUP_CONCAT(DISTINCT(tag_uid)), '') FROM tasks" - + " INNER JOIN tags ON tags.task = tasks._id" + "SELECT GROUP_CONCAT(DISTINCT(tag_uid)) FROM tasks" + + " LEFT JOIN tags ON tags.task = tasks._id" + " WHERE tasks._id IN (:tasks)" + " GROUP BY tasks._id") abstract List getAllTags(List tasks);