Fixing bug when selecting neighbors in an empty queryset of related elements

remotes/origin/enhancement/email-actions
Alejandro Alonso 2014-10-20 08:44:56 +02:00 committed by David Barragán Merino
parent 184f97dde1
commit a5c922be9e
2 changed files with 13 additions and 1 deletions

View File

@ -87,7 +87,7 @@ def get_neighbors(obj, results_set=None):
:return: Tuple `<left neighbor>, <right neighbor>`. Left and right neighbors can be `None`. :return: Tuple `<left neighbor>, <right neighbor>`. 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() results_set = type(obj).objects.get_queryset()
try: try:
left = _left_candidates(obj, results_set).reverse()[0] left = _left_candidates(obj, results_set).reverse()[0]

View File

@ -137,6 +137,18 @@ class TestIssues:
assert neighbors.left == issue3 assert neighbors.left == issue3
assert neighbors.right == issue1 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): def test_ordering_by_severity(self):
project = f.ProjectFactory.create() project = f.ProjectFactory.create()
severity1 = f.SeverityFactory.create(project=project, order=1) severity1 = f.SeverityFactory.create(project=project, order=1)