diff --git a/test-requirements.in b/test-requirements.in index ad6877d850d8d9f29e0bc10d9091da0171b71941..cb81176cfa60348834779739835bbab1dd78f186 100644 --- a/test-requirements.in +++ b/test-requirements.in @@ -2,4 +2,5 @@ pytest pytest-django pytest-env pytest-factoryboy +pytest-freezegun snapshottest diff --git a/test-requirements.txt b/test-requirements.txt index fa4ed41d8358e7e21ff6029f87aa07783e726cef..2cdcddcd067b279b9b3f67b150101aadfe03d542 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -8,6 +8,7 @@ atomicwrites==1.2.1 # via pytest attrs==18.2.0 # via pytest factory-boy==2.11.1 # via pytest-factoryboy faker==0.9.2 # via factory-boy +freezegun==0.3.11 # via pytest-freezegun inflection==0.3.1 # via pytest-factoryboy more-itertools==4.3.0 # via pytest pluggy==0.8.0 # via pytest @@ -15,9 +16,10 @@ py==1.7.0 # via pytest pytest-django==3.4.3 pytest-env==0.6.2 pytest-factoryboy==2.0.1 +pytest-freezegun==0.2.0 pytest==3.9.1 -python-dateutil==2.7.3 # via faker -six==1.11.0 # via faker, more-itertools, pytest, python-dateutil, snapshottest +python-dateutil==2.7.3 # via faker, freezegun +six==1.11.0 # via faker, freezegun, more-itertools, pytest, python-dateutil, snapshottest snapshottest==0.5.0 termcolor==1.1.0 # via snapshottest text-unidecode==1.2 # via faker diff --git a/tests/mutations/snapshots/snap_test_create_report.py b/tests/mutations/snapshots/snap_test_create_report.py index 3b84514580a928f1ea6fc35de37eb1007c62385f..d25b85d9cb74303ccb7ef0e2ea8f8d202a974a1f 100644 --- a/tests/mutations/snapshots/snap_test_create_report.py +++ b/tests/mutations/snapshots/snap_test_create_report.py @@ -40,14 +40,14 @@ snapshots['test_full_report 1'] = { }, 'body': 'I visited Tesla factory and talked with Elon Musk.', 'date': '2018-01-01 00:00:00+00:00', - 'edited': '__STRIPPED__', + 'edited': '2018-01-01 01:02:03+00:00', 'extra': None, 'id': '__STRIPPED__', 'isDraft': False, 'otherParticipants': 'Elon Musk', 'ourParticipants': 'me', 'providedBenefit': 'nothing', - 'published': '__STRIPPED__', + 'published': '2018-01-01 01:02:03+00:00', 'receivedBenefit': 'Tesla Model S', 'title': 'Free Tesla' } @@ -68,17 +68,51 @@ snapshots['test_is_draft 1'] = { }, 'body': 'Niel deGrasse Tyson just visited me...', 'date': '2018-01-03 00:00:00+00:00', - 'edited': '__STRIPPED__', + 'edited': '2018-01-04 00:07:07+00:00', 'extra': None, 'id': '__STRIPPED__', 'isDraft': True, 'otherParticipants': 'Neil deGrasse Tyson', 'ourParticipants': 'myself', 'providedBenefit': 'coffee', - 'published': '__STRIPPED__', + 'published': '2018-01-04 00:07:07+00:00', 'receivedBenefit': 'touch of the God', 'title': 'Visited by old friend' } } } } + +snapshots['test_full_report 2'] = { + 'author_id': 42, + 'body': 'I visited Tesla factory and talked with Elon Musk.', + 'date': '2018-01-01T00:00:00+00:00', + 'edited': '2018-01-01T01:02:03+00:00', + 'extra': None, + 'id': '__STRIPPED__', + 'is_draft': False, + 'other_participants': 'Elon Musk', + 'our_participants': 'me', + 'provided_benefit': 'nothing', + 'published': '2018-01-01T01:02:03+00:00', + 'received_benefit': 'Tesla Model S', + 'superseded_by_id': None, + 'title': 'Free Tesla' +} + +snapshots['test_is_draft 2'] = { + 'author_id': 42, + 'body': 'Niel deGrasse Tyson just visited me...', + 'date': '2018-01-03T00:00:00+00:00', + 'edited': '2018-01-04T00:07:07+00:00', + 'extra': None, + 'id': '__STRIPPED__', + 'is_draft': True, + 'other_participants': 'Neil deGrasse Tyson', + 'our_participants': 'myself', + 'provided_benefit': 'coffee', + 'published': '2018-01-04T00:07:07+00:00', + 'received_benefit': 'touch of the God', + 'superseded_by_id': None, + 'title': 'Visited by old friend' +} diff --git a/tests/mutations/test_create_report.py b/tests/mutations/test_create_report.py index 51f75020266383c70cde349f0783101ef399ed43..69fdc0f63fa4c2609bcf23a0c0567192024249c8 100644 --- a/tests/mutations/test_create_report.py +++ b/tests/mutations/test_create_report.py @@ -4,7 +4,7 @@ import re from openlobby.core.models import Report -from ..utils import strip_value +from ..utils import strip_value, dates_to_iso pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures("django_es")] @@ -49,6 +49,7 @@ def test_unauthorized(call_api, snapshot): snapshot.assert_match(response) +@pytest.mark.freeze_time("2018-01-01T01:02:03") def test_full_report(call_api, snapshot, author_fix): date = arrow.get(2018, 1, 1) title = "Free Tesla" @@ -69,27 +70,14 @@ def test_full_report(call_api, snapshot, author_fix): response = call_api(query, input, author_fix) - published = strip_value(response, "data", "createReport", "report", "published") - edited = strip_value(response, "data", "createReport", "report", "edited") - id = strip_value(response, "data", "createReport", "report", "id") assert re.match(r"\w+", id) - snapshot.assert_match(response) - report = Report.objects.get() - assert report.author_id == author_fix.id - assert report.date == date.datetime - assert report.published == arrow.get(published).datetime - assert report.edited == arrow.get(edited).datetime - assert report.title == title - assert report.body == body - assert report.received_benefit == received_benefit - assert report.provided_benefit == provided_benefit - assert report.our_participants == our_participants - assert report.other_participants == other_participants - assert report.extra is None - assert report.is_draft is False + assert Report.objects.count() == 1 + report = Report.objects.all().values()[0] + id = strip_value(report, "id") + snapshot.assert_match(dates_to_iso(report)) def test_input_sanitization(call_api, author_fix): @@ -114,6 +102,7 @@ def test_input_sanitization(call_api, author_fix): assert report.other_participants == "you!" +@pytest.mark.freeze_time("2018-01-04T00:07:07") def test_is_draft(call_api, snapshot, author_fix): date = arrow.get(2018, 1, 3) title = "Visited by old friend" @@ -135,24 +124,11 @@ def test_is_draft(call_api, snapshot, author_fix): response = call_api(query, input, author_fix) - published = strip_value(response, "data", "createReport", "report", "published") - edited = strip_value(response, "data", "createReport", "report", "edited") - id = strip_value(response, "data", "createReport", "report", "id") assert re.match(r"\w+", id) - snapshot.assert_match(response) - report = Report.objects.get() - assert report.author_id == author_fix.id - assert report.date == date.datetime - assert report.published == arrow.get(published).datetime - assert report.edited == arrow.get(edited).datetime - assert report.title == title - assert report.body == body - assert report.received_benefit == received_benefit - assert report.provided_benefit == provided_benefit - assert report.our_participants == our_participants - assert report.other_participants == other_participants - assert report.extra is None - assert report.is_draft is True + assert Report.objects.count() == 1 + report = Report.objects.all().values()[0] + id = strip_value(report, "id") + snapshot.assert_match(dates_to_iso(report)) diff --git a/tests/snapshots/snap_test_models.py b/tests/snapshots/snap_test_models.py new file mode 100644 index 0000000000000000000000000000000000000000..86e2375fdb3e32385cdea03c5547514406d274af --- /dev/null +++ b/tests/snapshots/snap_test_models.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +# snapshottest: v1 - https://goo.gl/zC4yUc +from __future__ import unicode_literals + +from snapshottest import Snapshot + + +snapshots = Snapshot() + +snapshots['test_report__is_saved_in_elasticsearch 1'] = 'foo' diff --git a/tests/utils.py b/tests/utils.py index 21ad1269293e5d8b466dc2b0289c405dcaf4b115..357c2647b8417e551b803cdb6ce940f55d5d1f67 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,7 +1,4 @@ from datetime import datetime -import json - -from openlobby.core.auth import create_access_token def strip_value(data, *path):