From 46cc11db2a95a910c4c60b697ff86afe6f70af80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Thu, 1 Feb 2018 00:45:41 +0100 Subject: [PATCH] Test get Report from Node interface. --- openlobby/core/api/schema.py | 4 +-- openlobby/core/api/types.py | 2 +- tests/dummy.py | 18 +++++++++++-- tests/schema/snapshots/snap_test_node.py | 23 +++++++++++++++++ tests/schema/test_node.py | 33 +++++++++++++++++++++++- 5 files changed, 74 insertions(+), 6 deletions(-) diff --git a/openlobby/core/api/schema.py b/openlobby/core/api/schema.py index d191bc5..d206bdf 100644 --- a/openlobby/core/api/schema.py +++ b/openlobby/core/api/schema.py @@ -23,7 +23,7 @@ class SearchReportsConnection(relay.Connection): node = types.Report -def get_authors(ids): +def _get_authors_cache(ids): users = User.objects.filter(id__in=ids) return {u.id: types.User.from_db(u) for u in users} @@ -78,7 +78,7 @@ class Query: edges = [] if len(response) > 0: - authors = get_authors(ids=[r.author_id for r in response]) + authors = _get_authors_cache(ids=[r.author_id for r in response]) for i, report in enumerate(response): cursor = paginator.get_edge_cursor(i + 1) node = types.Report.from_es(report, author=authors[report.author_id]) diff --git a/openlobby/core/api/types.py b/openlobby/core/api/types.py index 75113a8..a077f03 100644 --- a/openlobby/core/api/types.py +++ b/openlobby/core/api/types.py @@ -50,7 +50,7 @@ class Report(graphene.ObjectType): @classmethod def get_node(cls, info, id): try: - report = ReportDoc.get(id, using=info.context['es'], index=info.context['index']) + report = ReportDoc.get(id) except NotFoundError: return None diff --git a/tests/dummy.py b/tests/dummy.py index a6ab298..4ffd4d1 100644 --- a/tests/dummy.py +++ b/tests/dummy.py @@ -3,6 +3,16 @@ import arrow from openlobby.core.models import Report, User +authors = [ + { + 'id': 1, + 'username': 'Wolf', + 'openid_uid': 'Wolf', + 'first_name': 'Winston', + 'last_name': 'Wolfe', + }, +] + reports = [ { 'id': 1, @@ -42,7 +52,11 @@ reports = [ def prepare_reports(): - author = User.objects.create(id=1, username='Wolf', openid_uid='Wolf', - first_name='Winston', last_name='Wolfe') + author = User.objects.create(**authors[0]) for report in reports: Report.objects.create(author=author, **report) + + +def prepare_report(): + author = User.objects.create(**authors[0]) + Report.objects.create(author=author, **reports[0]) diff --git a/tests/schema/snapshots/snap_test_node.py b/tests/schema/snapshots/snap_test_node.py index 65af548..f35ffd4 100644 --- a/tests/schema/snapshots/snap_test_node.py +++ b/tests/schema/snapshots/snap_test_node.py @@ -33,3 +33,26 @@ snapshots['test_author__returns_only_if_is_author 1'] = { 'node': None } } + +snapshots['test_report 1'] = { + 'data': { + 'node': { + 'author': { + 'extra': None, + 'firstName': 'Winston', + 'id': 'QXV0aG9yOjE=', + 'lastName': 'Wolfe' + }, + 'body': 'Long story short: we got the Ring!', + 'date': '2018-01-01 00:00:00+00:00', + 'extra': None, + 'id': 'UmVwb3J0OjE=', + 'otherParticipants': 'Saruman', + 'ourParticipants': 'Frodo, Gandalf', + 'providedBenefit': '', + 'published': '2018-01-02 00:00:00+00:00', + 'receivedBenefit': 'The Ring', + 'title': 'The Fellowship of the Ring' + } + } +} diff --git a/tests/schema/test_node.py b/tests/schema/test_node.py index eeaa5e2..18fde4c 100644 --- a/tests/schema/test_node.py +++ b/tests/schema/test_node.py @@ -3,8 +3,10 @@ from graphql_relay import to_global_id from openlobby.core.models import OpenIdClient, User +from ..dummy import prepare_report -pytestmark = pytest.mark.django_db + +pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] def test_login_shortcut(client, snapshot): @@ -59,3 +61,32 @@ def test_author__returns_only_if_is_author(client, snapshot): }} """.format(id=to_global_id('Author', 7))}) snapshot.assert_match(res.json()) + + +def test_report(client, snapshot): + prepare_report() + res = client.post('/graphql', {'query': """ + query {{ + node (id:"{id}") {{ + ... on Report {{ + id + date + published + title + body + receivedBenefit + providedBenefit + ourParticipants + otherParticipants + extra + author {{ + id + firstName + lastName + extra + }} + }} + }} + }} + """.format(id=to_global_id('Report', 1))}) + snapshot.assert_match(res.json()) -- GitLab