diff --git a/openlobby/core/search.py b/openlobby/core/search.py index 33490823292ab4526dc0ef26883194daea6cbcac..27ef112755667cba816abd78dbf98d1842e75cc4 100644 --- a/openlobby/core/search.py +++ b/openlobby/core/search.py @@ -8,10 +8,10 @@ HIGHLIGHT_PARAMS = { } -def query_reports(query, paginator, *, es, index, highlight=False): +def query_reports(query, paginator, *, highlight=False): fields = ['title', 'body', 'received_benefit', 'provided_benefit', 'our_participants', 'other_participants'] - s = ReportDoc.search(using=es, index=index) + s = ReportDoc.search() if query != '': s = s.query('multi_match', query=query, fields=fields) if highlight: @@ -21,8 +21,8 @@ def query_reports(query, paginator, *, es, index, highlight=False): return s.execute() -def reports_by_author(author_id, paginator, *, es, index): - s = ReportDoc.search(using=es, index=index) +def reports_by_author(author_id, paginator): + s = ReportDoc.search() s = s.filter('term', author_id=author_id) s = s.sort('-published') s = s[paginator.slice_from:paginator.slice_to] diff --git a/openlobby/settings.py b/openlobby/settings.py index 82c9a962125d6bd4232dc6b87a65b52de4f910cc..e9ad91211957dfcca1d09e887f797808afd49b12 100644 --- a/openlobby/settings.py +++ b/openlobby/settings.py @@ -119,7 +119,6 @@ STATIC_URL = '/static/' LOGGING = { 'version': 1, - 'disable_existing_loggers': False, 'handlers': { 'console': { 'class': 'logging.StreamHandler', diff --git a/tests/test_models.py b/tests/test_models.py index 3b9234a15366aeeb9f5ba532b63a3ff364d62d37..ebd3e5a63a30eeac4dc568c0b0da510c69231228 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -8,7 +8,7 @@ from openlobby.core.documents import ReportDoc pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] -def test_report_marks_user_as_author_on_save(): +def test_report__marks_user_as_author_on_save(): author = User.objects.create(id=1, is_author=False) date = arrow.get(2018, 1, 1).datetime Report.objects.create(author=author, date=date, body='Lorem ipsum.') @@ -16,7 +16,7 @@ def test_report_marks_user_as_author_on_save(): assert user.is_author -def test_report_is_saved_in_elasticsearch(): +def test_report__is_saved_in_elasticsearch(): author = User.objects.create(id=6) date = arrow.get(2018, 1, 1).datetime published = arrow.get(2018, 1, 2).datetime @@ -47,3 +47,20 @@ def test_report_is_saved_in_elasticsearch(): assert doc.our_participants == 'me' assert doc.other_participants == 'them' assert doc.extra == {'a': 3} + + +def test_report__save_works_with_no_extra(): + author = User.objects.create(id=6) + date = arrow.get(2018, 1, 1).datetime + Report.objects.create( + id=7, + author=author, + date=date, + published=date, + body='Lorem ipsum.', + ) + docs = list(ReportDoc.search()) + assert len(docs) == 1 + doc = docs[0] + assert doc.meta.id == '7' + assert doc.extra is None diff --git a/tests/test_search.py b/tests/test_search.py new file mode 100644 index 0000000000000000000000000000000000000000..d604b0e82a25a10198a343a0c284f1178e5d4f92 --- /dev/null +++ b/tests/test_search.py @@ -0,0 +1,88 @@ +import pytest +import arrow + +from openlobby.core.api.paginator import Paginator, encode_cursor +from openlobby.core.models import Report, User +from openlobby.core.search import query_reports + + +pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] + + +reports = [ + { + 'id': 1, + 'date': arrow.get(2018, 1, 1).datetime, + 'published': arrow.get(2018, 1, 2).datetime, + 'title': 'The Fellowship of the Ring', + 'body': 'Long story short: we got the Ring!', + 'received_benefit': 'The Ring', + 'provided_benefit': '', + 'our_participants': 'Frodo, Gandalf', + 'other_participants': 'Saruman', + }, + { + 'id': 2, + 'date': arrow.get(2018, 1, 5).datetime, + 'published': arrow.get(2018, 1, 10).datetime, + 'title': 'The Two Towers', + 'body': 'Another long story.', + 'received_benefit': 'Mithrill Jacket', + 'provided_benefit': '', + 'our_participants': 'Frodo, Gimli, Legolas', + 'other_participants': 'Saruman, Sauron', + }, + { + 'id': 3, + 'date': arrow.get(2018, 1, 7).datetime, + 'published': arrow.get(2018, 1, 8).datetime, + 'title': 'The Return of the King', + 'body': 'Aragorn is the King. And we have lost the Ring.', + 'received_benefit': '', + 'provided_benefit': 'The Ring', + 'our_participants': 'Aragorn', + 'other_participants': 'Sauron', + }, +] + + +def prepare_data(): + author = User.objects.create(id=1, username='Wolf', openid_uid='Wolf', + first_name='Winston', last_name='Wolfe') + for report in reports: + Report.objects.create(author=author, **report) + + +@pytest.mark.parametrize('query, expected_ids', [ + ('', [2, 3, 1]), + ('sauron', [2, 3]), + ('towers', [2]), + ('Aragorn Gandalf', [3, 1]), +]) +def test_query_reports(query, expected_ids): + prepare_data() + paginator = Paginator() + response = query_reports(query, paginator) + assert expected_ids == [int(r.meta.id) for r in response] + + +def test_query_reports__highlight(): + prepare_data() + paginator = Paginator() + query = 'King' + response = query_reports(query, paginator, highlight=True) + doc = response.hits[0] + assert '<mark>King</mark>' in doc.meta.highlight.title[0] + assert '<mark>King</mark>' in doc.meta.highlight.body[0] + + +@pytest.mark.parametrize('first, after, expected_ids', [ + (2, None, [2, 3]), + (2, encode_cursor(1), [3, 1]), +]) +def test_query_reports__pagination(first, after, expected_ids): + prepare_data() + query = '' + paginator = Paginator(first=first, after=after) + response = query_reports(query, paginator) + assert expected_ids == [int(r.meta.id) for r in response]