Skip to content

Commit

Permalink
Merge pull request #17 from Jakeway/bug_fix_total_surplus_inf_loop
Browse files Browse the repository at this point in the history
Bug fix total surplus inf loop
  • Loading branch information
Jakeway authored Jun 28, 2018
2 parents 7de9c80 + 24bb400 commit 9d741b6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion autocompleter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = (0, 7, 0)
VERSION = (0, 7, 1)

from autocompleter.registry import registry, signal_registry
from autocompleter.base import AutocompleterBase, AutocompleterModelProvider, AutocompleterDictProvider, Autocompleter
Expand Down
11 changes: 8 additions & 3 deletions autocompleter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,14 @@ def suggest(self, term, facets=[]):
# If there are extra result slots available, go through each provider that
# needs extra results, and hand them out until there are no more to give
while total_surplus > 0:
# Can happen if no provider has a deficit and the total number of results
# from all providers < MAX_RESULTS
if len(provider_deficits) == 0:
# Check if there are any providers that still need extra results, and if not exit the loop,
# else we get caught in an infinite loop
provider_with_deficit_exists = False
for provider_name in provider_deficits:
deficit = provider_deficits[provider_name]
if deficit > 0:
provider_with_deficit_exists = True
if not provider_with_deficit_exists:
break
for provider_name in provider_deficits:
deficit = provider_deficits[provider_name]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='django-autocompleter',
version="0.7.0",
version="0.7.1",
description='A redis-backed autocompletor for Django projects',
author='Ara Anjargolian',
author_email='[email protected]',
Expand Down
17 changes: 17 additions & 0 deletions test_project/test_app/tests/test_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,23 @@ def test_max_results_handles_surplus(self):

registry.del_autocompleter_setting('ind_stock', 'MAX_RESULTS')

def test_max_results_handles_deficit_less_than_surplus(self):
"""
MAX_RESULTS stops trying to hand out surplus matches when provider's deficits are met
"""
# Previous code would have failed on this test case because of an infinite while loop that
# failed to break when all provider's deficits were met. The setup requires
# there to be at least one provider which will have a deficit less than the total surplus.
# In this test case, the total surplus will be 3 (since stock has no matches) and the deficit
# for indicators will be 2 (since there are 5 total matches for indicators and 3 slots are
# reserved initially)
registry.set_autocompleter_setting('ind_stock', 'MAX_RESULTS', 6)
matches = self.autocomp.suggest('S&P')
self.assertEqual(5, len(matches['ind']))
self.assertEqual(0, len(matches['stock']))

registry.del_autocompleter_setting('ind_stock', 'MAX_RESULTS')

def test_max_results_has_hard_limit(self):
"""
Suggest respects MAX_RESULTS over giving every provider at least 1 result
Expand Down

0 comments on commit 9d741b6

Please sign in to comment.