Skip to content
Snippets Groups Projects
Commit c6307403 authored by jan.bednarik's avatar jan.bednarik
Browse files

Search of reports tests.

parent 6b2bbcdc
No related branches found
No related tags found
No related merge requests found
...@@ -8,10 +8,10 @@ HIGHLIGHT_PARAMS = { ...@@ -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', fields = ['title', 'body', 'received_benefit', 'provided_benefit',
'our_participants', 'other_participants'] 'our_participants', 'other_participants']
s = ReportDoc.search(using=es, index=index) s = ReportDoc.search()
if query != '': if query != '':
s = s.query('multi_match', query=query, fields=fields) s = s.query('multi_match', query=query, fields=fields)
if highlight: if highlight:
...@@ -21,8 +21,8 @@ def query_reports(query, paginator, *, es, index, highlight=False): ...@@ -21,8 +21,8 @@ def query_reports(query, paginator, *, es, index, highlight=False):
return s.execute() return s.execute()
def reports_by_author(author_id, paginator, *, es, index): def reports_by_author(author_id, paginator):
s = ReportDoc.search(using=es, index=index) s = ReportDoc.search()
s = s.filter('term', author_id=author_id) s = s.filter('term', author_id=author_id)
s = s.sort('-published') s = s.sort('-published')
s = s[paginator.slice_from:paginator.slice_to] s = s[paginator.slice_from:paginator.slice_to]
......
...@@ -119,7 +119,6 @@ STATIC_URL = '/static/' ...@@ -119,7 +119,6 @@ STATIC_URL = '/static/'
LOGGING = { LOGGING = {
'version': 1, 'version': 1,
'disable_existing_loggers': False,
'handlers': { 'handlers': {
'console': { 'console': {
'class': 'logging.StreamHandler', 'class': 'logging.StreamHandler',
......
...@@ -8,7 +8,7 @@ from openlobby.core.documents import ReportDoc ...@@ -8,7 +8,7 @@ from openlobby.core.documents import ReportDoc
pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] 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) author = User.objects.create(id=1, is_author=False)
date = arrow.get(2018, 1, 1).datetime date = arrow.get(2018, 1, 1).datetime
Report.objects.create(author=author, date=date, body='Lorem ipsum.') Report.objects.create(author=author, date=date, body='Lorem ipsum.')
...@@ -16,7 +16,7 @@ def test_report_marks_user_as_author_on_save(): ...@@ -16,7 +16,7 @@ def test_report_marks_user_as_author_on_save():
assert user.is_author assert user.is_author
def test_report_is_saved_in_elasticsearch(): def test_report__is_saved_in_elasticsearch():
author = User.objects.create(id=6) author = User.objects.create(id=6)
date = arrow.get(2018, 1, 1).datetime date = arrow.get(2018, 1, 1).datetime
published = arrow.get(2018, 1, 2).datetime published = arrow.get(2018, 1, 2).datetime
...@@ -47,3 +47,20 @@ def test_report_is_saved_in_elasticsearch(): ...@@ -47,3 +47,20 @@ def test_report_is_saved_in_elasticsearch():
assert doc.our_participants == 'me' assert doc.our_participants == 'me'
assert doc.other_participants == 'them' assert doc.other_participants == 'them'
assert doc.extra == {'a': 3} 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
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]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment