Fix selections that include task with no tags

gtask_related_email
Alex Baker 5 years ago
parent 274d98160a
commit c8e313c67a

@ -6,7 +6,6 @@ import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.tasks.makers.TagDataMaker.NAME; 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.TagDataMaker.newTagData;
import static org.tasks.makers.TagMaker.TAGDATA; import static org.tasks.makers.TagMaker.TAGDATA;
import static org.tasks.makers.TagMaker.TAGUID; import static org.tasks.makers.TagMaker.TAGUID;
@ -105,7 +104,6 @@ public class TagDataDaoTest extends InjectingTestCase {
newTag(1, "tag1", "tag2"); newTag(1, "tag1", "tag2");
newTag(2, "tag2", "tag3"); newTag(2, "tag2", "tag3");
assertEquals( assertEquals(
newHashSet("tag1", "tag3"), tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).first); newHashSet("tag1", "tag3"), tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).first);
} }
@ -123,8 +121,7 @@ public class TagDataDaoTest extends InjectingTestCase {
newTag(1, "tag1", "tag2"); newTag(1, "tag1", "tag2");
newTag(2, "tag2", "tag3"); newTag(2, "tag2", "tag3");
assertEquals( assertEquals(newHashSet("tag2"), tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).second);
newHashSet("tag2"), tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).second);
} }
@Test @Test
@ -135,6 +132,26 @@ public class TagDataDaoTest extends InjectingTestCase {
assertTrue(tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).second.isEmpty()); assertTrue(tagDataDao.getTagSelections(ImmutableList.of(1L, 2L)).second.isEmpty());
} }
@Test
public void getSelectionsWithNoTags() {
newTag(1);
Pair<Set<String>, Set<String>> 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<String>, Set<String>> selections =
tagDataDao.getTagSelections(ImmutableList.of(1L, 2L));
assertEquals(newHashSet("tag1"), selections.first);
assertTrue(selections.second.isEmpty());
}
private void newTag(long taskId, String... tags) { private void newTag(long taskId, String... tags) {
Task task = newTask(with(ID, taskId)); Task task = newTask(with(ID, taskId));
taskDao.createNew(task); taskDao.createNew(task);

@ -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.Lists.newArrayList;
import static com.google.common.collect.Sets.difference; import static com.google.common.collect.Sets.difference;
import static com.google.common.collect.Sets.newHashSet; 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.MAX_SQLITE_ARGS;
import static org.tasks.db.DbUtils.batch; import static org.tasks.db.DbUtils.batch;
@ -76,11 +77,12 @@ public abstract class TagDataDao {
public Pair<Set<String>, Set<String>> getTagSelections(List<Long> tasks) { public Pair<Set<String>, Set<String>> getTagSelections(List<Long> tasks) {
List<String> allTags = getAllTags(tasks); List<String> allTags = getAllTags(tasks);
Collection<Set<String>> tags = Collections2.transform(allTags, t -> newHashSet(t.split(","))); Collection<Set<String>> tags =
Collections2.transform(allTags, t -> t == null ? emptySet() : newHashSet(t.split(",")));
Set<String> partialTags = newHashSet(concat(tags)); Set<String> partialTags = newHashSet(concat(tags));
Set<String> commonTags = null; Set<String> commonTags = null;
if (tags.isEmpty()) { if (tags.isEmpty()) {
commonTags = Collections.emptySet(); commonTags = emptySet();
} else { } else {
for (Set<String> s : tags) { for (Set<String> s : tags) {
if (commonTags == null) { if (commonTags == null) {
@ -95,8 +97,8 @@ public abstract class TagDataDao {
} }
@Query( @Query(
"SELECT IFNULL(GROUP_CONCAT(DISTINCT(tag_uid)), '') FROM tasks" "SELECT GROUP_CONCAT(DISTINCT(tag_uid)) FROM tasks"
+ " INNER JOIN tags ON tags.task = tasks._id" + " LEFT JOIN tags ON tags.task = tasks._id"
+ " WHERE tasks._id IN (:tasks)" + " WHERE tasks._id IN (:tasks)"
+ " GROUP BY tasks._id") + " GROUP BY tasks._id")
abstract List<String> getAllTags(List<Long> tasks); abstract List<String> getAllTags(List<Long> tasks);

Loading…
Cancel
Save