Skip to content

Commit

Permalink
Merge pull request #10 from Jakeway/facets_part_3
Browse files Browse the repository at this point in the history
Facets part 3
  • Loading branch information
Jakeway authored May 23, 2018
2 parents 12b81b5 + 891214b commit 31832f0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
5 changes: 4 additions & 1 deletion autocompleter/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# term returns 2 results for A, ELASTIC_RESULTS will allow B to return up to 6 results
ELASTIC_RESULTS = getattr(settings, 'AUTOCOMPLETER_ELASTIC_RESULTS', False)

# Name of variable autocompleter will look for to populate facet data on a suggest call
FACET_PARAMETER_NAME = getattr(settings, 'AUTOCOMPLETER_FACET_PARAMETER_NAME', 'facets')

# When an autocompleter only has one type of result it can return, this setting determines
# whether the results set is "flattened" so that it no longer uses the data structure needed
# to return multi-type results
Expand Down Expand Up @@ -42,7 +45,7 @@
# Redis connection parameters
REDIS_CONNECTION = getattr(settings, 'AUTOCOMPLETER_REDIS_CONNECTION', {})

# Name of variable autcompleter will look for to grab what term to search on.
# Name of variable autocompleter will look for to grab what term to search on.
SUGGEST_PARAMETER_NAME = getattr(settings, 'AUTOCOMPLETER_SUGGEST_PARAMETER_NAME', 'q')

# Test data for debugging/running tests
Expand Down
7 changes: 6 additions & 1 deletion autocompleter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def get(self, request, name):
if settings.SUGGEST_PARAMETER_NAME in request.GET:
term = request.GET[settings.SUGGEST_PARAMETER_NAME]
ac = Autocompleter(name)
results = ac.suggest(term)
if settings.FACET_PARAMETER_NAME in request.GET:
facets = request.GET[settings.FACET_PARAMETER_NAME]
facets = json.loads(facets)
results = ac.suggest(term, facets=facets)
else:
results = ac.suggest(term)

json_response = json.dumps(results)
return HttpResponse(json_response, content_type='application/json')
Expand Down
49 changes: 49 additions & 0 deletions test_project/test_app/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,52 @@ def test_no_exact_suggest_match(self):

json_response = json.loads(response.content.decode('utf-8'))
self.assertEqual(len(json_response), len(matches_symbol))


class TextFacetSuggestView(AutocompleterTestCase):
fixtures = ['stock_test_data_small.json']

def setUp(self):
super(TextFacetSuggestView, self).setUp()
self.autocomp = Autocompleter('faceted_stock')
self.autocomp.store_all()

def tearDown(self):
self.autocomp.remove_all()

def test_facet_suggest_match(self):
suggest_url = reverse('suggest', kwargs={'name': 'faceted_stock'})

facets = [
{
'type': 'or',
'facets': [{'key': 'sector', 'value': 'Technology'}]
}
]

matches_symbol = self.autocomp.suggest('a', facets=facets)

data = {
settings.SUGGEST_PARAMETER_NAME: 'a',
settings.FACET_PARAMETER_NAME: json.dumps(facets)
}
response = self.client.get(suggest_url, data=data)
self.assertEqual(response.status_code, 200)

json_response = json.loads(response.content.decode('utf-8'))
self.assertEqual(len(json_response), len(matches_symbol))

def test_empty_facet_suggest(self):
suggest_url = reverse('suggest', kwargs={'name': 'faceted_stock'})

matches_symbol = self.autocomp.suggest('a', facets=[])

data = {
settings.SUGGEST_PARAMETER_NAME: 'a',
settings.FACET_PARAMETER_NAME: json.dumps([])
}
response = self.client.get(suggest_url, data=data)
self.assertEqual(response.status_code, 200)

json_response = json.loads(response.content.decode('utf-8'))
self.assertEqual(len(json_response), len(matches_symbol))

0 comments on commit 31832f0

Please sign in to comment.