Skip to content

Commit

Permalink
close City-of-Helsinki#38 + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luopio committed Mar 21, 2017
1 parent e7d899d commit 30af784
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
9 changes: 8 additions & 1 deletion services/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import re
import logging

from django.conf import settings
from django.utils import translation
Expand Down Expand Up @@ -52,6 +53,8 @@ def register_view(klass, name, base_name=None):

LANGUAGES = [x[0] for x in settings.LANGUAGES]

logger = logging.getLogger(__name__)

class MPTTModelSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
super(MPTTModelSerializer, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -619,6 +622,7 @@ def to_representation(self, search_result):

KML_REGEXP = re.compile(settings.KML_REGEXP)


class SearchViewSet(munigeo_api.GeoModelAPIView, viewsets.ViewSetMixin, generics.ListAPIView):
serializer_class = SearchSerializer
renderer_classes = DEFAULT_RENDERERS + [KmlRenderer]
Expand All @@ -641,7 +645,10 @@ def list(self, request, *args, **kwargs):
setattr(self, key, {})
fields = [x.strip().split('.') for x in specs[key].split(',') if x]
for f in fields:
getattr(self, key).setdefault(f[0], []).append(f[1])
try:
getattr(self, key).setdefault(f[0], []).append(f[1])
except IndexError:
logger.warning("Field '%s' given in unsupported non-dot-format: '%s'" % (key, specs[key]))
else:
setattr(self, key, None)

Expand Down
32 changes: 32 additions & 0 deletions smbackend/test/haystack_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
def read_config(name):
return json.load(open(
os.path.join(
'smbackend',
'elasticsearch/{}.json'.format(name))))

TEST_INDEX = {
'default': {
'ENGINE': 'multilingual_haystack.backends.MultilingualSearchEngine',
},
'default-fi': {
'ENGINE': 'multilingual_haystack.backends.LanguageSearchEngine',
'BASE_ENGINE': 'multilingual_haystack.custom_elasticsearch_search_backend.CustomEsSearchEngine',
'URL': 'http://localhost:9200/',
'INDEX_NAME': 'servicemap-fi-test',
'MAPPINGS': read_config('mappings_finnish')['modelresult']['properties'],
'SETTINGS': read_config('settings_finnish')
},
'default-sv': {
'ENGINE': 'multilingual_haystack.backends.LanguageSearchEngine',
'BASE_ENGINE': 'multilingual_haystack.custom_elasticsearch_search_backend.CustomEsSearchEngine',
'URL': 'http://localhost:9200/',
'INDEX_NAME': 'servicemap-sv-test',
},
'default-en': {
'ENGINE': 'multilingual_haystack.backends.LanguageSearchEngine',
'BASE_ENGINE': 'multilingual_haystack.custom_elasticsearch_search_backend.CustomEsSearchEngine',
'URL': 'http://localhost:9200/',
'INDEX_NAME': 'servicemap-en-test',
},
}
19 changes: 16 additions & 3 deletions smbackend/test/test_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import django.utils.translation
from django.core.management import call_command
from django.test import Client
from django.test import override_settings
from haystack.query import SearchQuerySet

Expand All @@ -15,10 +16,22 @@ def test_search(haystack_test, db_content):

# Check that we find all we need via search
# (for some reason Units are not indexed with working `object` reference, works with Services though)
assert db_content['unit'].name in SearchQuerySet().all().filter(text='kirjasto')[0].text
assert SearchQuerySet().all().filter(text='kirjasto')[1].object.name == db_content['service'].name
assert db_content['unit'].name in SearchQuerySet().filter(text='kirjasto')[0].text
assert SearchQuerySet().filter(text='kirjasto')[1].object.name == db_content['service'].name

# No results for something that is not there
assert len(SearchQuerySet().all().filter(text='sairaala')) == 0
assert len(SearchQuerySet().filter(text='sairaala')) == 0


@pytest.mark.django_db
def test_search_filters(haystack_test, db_content):
call_command('update_index', interactive=False, verbosity=0)
c = Client()
resp = c.get('/v1/search/', {'q': 'kirjasto'})
assert resp.status_code == 200
assert db_content['unit'].name in str(resp.content)
resp = c.get('/v1/search/', {'q': 'kirjasto', 'only': 'services.service'})
assert db_content['unit'].name not in str(resp.content)
# check for #38, don't fail if dot-format not used in only
resp = c.get('/v1/search/', {'q': 'kirjasto', 'only': 'unit'})
assert db_content['unit'].name not in str(resp.content) # won't return any content, fail with a log entry

0 comments on commit 30af784

Please sign in to comment.