From a5c922be9e434a45f1db5702dfeb67511716d4b1 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Mon, 20 Oct 2014 08:44:56 +0200 Subject: [PATCH] Fixing bug when selecting neighbors in an empty queryset of related elements --- taiga/base/neighbors.py | 2 +- tests/integration/test_neighbors.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/taiga/base/neighbors.py b/taiga/base/neighbors.py index b8368fa6..14223487 100644 --- a/taiga/base/neighbors.py +++ b/taiga/base/neighbors.py @@ -87,7 +87,7 @@ def get_neighbors(obj, results_set=None): :return: Tuple `, `. Left and right neighbors can be `None`. """ - if results_set is None: + if results_set is None or results_set.count() == 0: results_set = type(obj).objects.get_queryset() try: left = _left_candidates(obj, results_set).reverse()[0] diff --git a/tests/integration/test_neighbors.py b/tests/integration/test_neighbors.py index 83adf1be..3281dcee 100644 --- a/tests/integration/test_neighbors.py +++ b/tests/integration/test_neighbors.py @@ -137,6 +137,18 @@ class TestIssues: assert neighbors.left == issue3 assert neighbors.right == issue1 + def test_empty_related_queryset(self): + project = f.ProjectFactory.create() + + issue1 = f.IssueFactory.create(project=project) + issue2 = f.IssueFactory.create(project=project) + issue3 = f.IssueFactory.create(project=project) + + neighbors = n.get_neighbors(issue2, Issue.objects.none()) + + assert neighbors.left == issue3 + assert neighbors.right == issue1 + def test_ordering_by_severity(self): project = f.ProjectFactory.create() severity1 = f.SeverityFactory.create(project=project, order=1)