Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Refactor tests. #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions example/tests/integration/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.core.urlresolvers import reverse

import pytest
from example.tests.utils import dump_json, redump_json
from example.tests.utils import load_json

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -36,10 +36,9 @@ def test_top_level_meta_for_list_view(blog, client):
}

response = client.get(reverse("blog-list"))
content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert content_dump == expected_dump
assert expected == parsed_content


def test_top_level_meta_for_detail_view(blog, client):
Expand All @@ -64,7 +63,6 @@ def test_top_level_meta_for_detail_view(blog, client):
}

response = client.get(reverse("blog-detail", kwargs={'pk': blog.pk}))
content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert content_dump == expected_dump
assert expected == parsed_content
7 changes: 3 additions & 4 deletions example/tests/integration/test_non_paginated_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from example.views import EntryViewSet
from rest_framework_json_api.pagination import PageNumberPagination

from example.tests.utils import dump_json, redump_json
from example.tests.utils import load_json

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -101,7 +101,6 @@ class NonPaginatedEntryViewSet(EntryViewSet):
response = view(request)
response.render()

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert content_dump == expected_dump
assert expected == parsed_content
7 changes: 3 additions & 4 deletions example/tests/integration/test_pagination.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.core.urlresolvers import reverse

import pytest
from example.tests.utils import dump_json, redump_json
from example.tests.utils import load_json

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -63,7 +63,6 @@ def test_pagination_with_single_entry(single_entry, client):
}

response = client.get(reverse("entry-list"))
content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert content_dump == expected_dump
assert expected == parsed_content
8 changes: 3 additions & 5 deletions example/tests/test_format_keys.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils import encoding

from example.tests import TestBase
from example.tests.utils import dump_json, redump_json
from example.tests.utils import load_json


class FormatKeysSetTests(TestBase):
Expand Down Expand Up @@ -53,7 +52,6 @@ def test_camelization(self):
}
}

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content
7 changes: 3 additions & 4 deletions example/tests/test_generic_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_framework.serializers import ValidationError

from example.tests import TestBase
from example.tests.utils import dump_json, redump_json
from example.tests.utils import load_json


class GenericValidationTest(TestBase):
Expand Down Expand Up @@ -34,7 +34,6 @@ def test_generic_validation_error(self):
}]
}

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content
22 changes: 9 additions & 13 deletions example/tests/test_generic_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.conf import settings

from example.tests import TestBase
from example.tests.utils import dump_json, redump_json
from example.tests.utils import load_json


class GenericViewSet(TestBase):
Expand Down Expand Up @@ -38,10 +38,9 @@ def test_default_rest_framework_behavior(self):
'email': 'miles@example.com'
}

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content


def test_ember_expected_renderer(self):
Expand All @@ -66,10 +65,9 @@ def test_ember_expected_renderer(self):
}
}

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content

def test_default_validation_exceptions(self):
"""
Expand All @@ -96,10 +94,9 @@ def test_default_validation_exceptions(self):
response = self.client.post('/identities', {
'email': 'bar', 'first_name': 'alajflajaljalajlfjafljalj'})

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content

def test_custom_validation_exceptions(self):
"""
Expand All @@ -124,7 +121,6 @@ def test_custom_validation_exceptions(self):
response = self.client.post('/identities', {
'email': 'bar', 'last_name': 'alajflajaljalajlfjafljalj'})

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content
27 changes: 11 additions & 16 deletions example/tests/test_model_viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.conf import settings

from example.tests import TestBase
from example.tests.utils import dump_json, redump_json
from example.tests.utils import dump_json, load_json


class ModelViewSetTests(TestBase):
Expand Down Expand Up @@ -64,10 +64,9 @@ def test_key_in_list_result(self):
}
}

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content

def test_page_two_in_list_result(self):
"""
Expand Down Expand Up @@ -104,10 +103,9 @@ def test_page_two_in_list_result(self):
}
}

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content

def test_page_range_in_list_result(self):
"""
Expand Down Expand Up @@ -155,10 +153,9 @@ def test_page_range_in_list_result(self):
}
}

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content

def test_key_in_detail_result(self):
"""
Expand All @@ -179,10 +176,9 @@ def test_key_in_detail_result(self):
}
}

content_dump = redump_json(response.content)
expected_dump = dump_json(expected)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert expected == parsed_content

def test_patch_requires_id(self):
"""
Expand Down Expand Up @@ -224,10 +220,9 @@ def test_key_in_post(self):
content_type='application/vnd.api+json',
data=dump_json(data))

content_dump = redump_json(response.content)
expected_dump = dump_json(data)
parsed_content = load_json(response.content)

assert expected_dump == content_dump
assert data == parsed_content

# is it updated?
self.assertEqual(
Expand Down
7 changes: 3 additions & 4 deletions example/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from example.models import Blog, Entry, Author

import pytest
from example.tests.utils import dump_json, redump_json
from example.tests.utils import load_json

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -108,7 +108,6 @@ def test_model_serializer_with_implicit_fields(self, comment, client):

assert response.status_code == 200

actual = redump_json(response.content)
expected_json = dump_json(expected)
parsed_content = load_json(response.content)

assert actual == expected_json
assert expected == parsed_content
21 changes: 0 additions & 21 deletions example/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,3 @@ def dump_json(data):
}

return force_bytes(json.dumps(data, **json_kwargs))


def redump_json(data):
'''
The response.content is already a JSON formatted string BUT
we don't know anything about its formatting, in particular,
the indent and separators arguments. DRF has a complex method to
determine what values to use for each argument and unfortunately,
the methods aren't the same in all DRF versions.

So what to do? LOAD the JSON formmated string (response.content)
as a Python object and DUMP it again and hence the name of this function.

This will guarantee that we're comparing two similarly formatted JSON
strings. Only the formatting similarity is guaranteed. As for the content,
that's what the tests are for!
'''

data = json.loads(force_text(data))

return dump_json(data)