Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 836391f

Browse files
Use py.test client over drf client to fix Django 1.7 tests
1 parent bd5a955 commit 836391f

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

‎example/tests/integration/test_model_resource_name.py

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from example.tests.utils import load_json
55

6-
from rest_framework.test import APITestCase
76
from example import models, serializers, views
87
pytestmark = pytest.mark.django_db
98

@@ -13,46 +12,69 @@ class JSONAPIMeta:
1312
resource_name = "resource_name_from_JSONAPIMeta"
1413

1514

15+
def _check_resource_and_relationship_comment_type_match(django_client):
16+
entry_response = django_client.get(reverse("entry-list"))
17+
comment_response = django_client.get(reverse("comment-list"))
18+
19+
comment_resource_type = load_json(comment_response.content).get('data')[0].get('type')
20+
comment_relationship_type = load_json(entry_response.content).get(
21+
'data')[0].get('relationships').get('comments').get('data')[0].get('type')
22+
23+
assert comment_resource_type == comment_relationship_type, "The resource type seen in the relationships and head resource do not match"
24+
25+
26+
def _check_relationship_and_included_comment_type_are_the_same(django_client, url):
27+
response = django_client.get(url + "?include=comments")
28+
data = load_json(response.content).get('data')[0]
29+
comment = load_json(response.content).get('included')[0]
30+
31+
comment_relationship_type = data.get('relationships').get('comments').get('data')[0].get('type')
32+
comment_included_type = comment.get('type')
33+
34+
assert comment_relationship_type == comment_included_type, "The resource type seen in the relationships and included do not match"
35+
36+
1637
@pytest.mark.usefixtures("single_entry")
17-
class ModelResourceNameTests(APITestCase):
18-
def test_model_resource_name_on_list(self):
38+
class TestModelResourceName:
39+
40+
def test_model_resource_name_on_list(self, client):
1941
models.Comment.__bases__ += (_PatchedModel,)
20-
response = self.client.get(reverse("comment-list"))
42+
response = client.get(reverse("comment-list"))
2143
data = load_json(response.content)['data'][0]
2244
# name should be super-author instead of model name RenamedAuthor
2345
assert (data.get('type') == 'resource_name_from_JSONAPIMeta'), (
2446
'resource_name from model incorrect on list')
2547

2648
# Precedence tests
27-
def test_resource_name_precendence(self):
49+
def test_resource_name_precendence(self, client):
2850
# default
29-
response = self.client.get(reverse("comment-list"))
51+
response = client.get(reverse("comment-list"))
3052
data = load_json(response.content)['data'][0]
3153
assert (data.get('type') == 'comments'), (
3254
'resource_name from model incorrect on list')
3355

3456
# model > default
3557
models.Comment.__bases__ += (_PatchedModel,)
36-
response = self.client.get(reverse("comment-list"))
58+
response = client.get(reverse("comment-list"))
3759
data = load_json(response.content)['data'][0]
3860
assert (data.get('type') == 'resource_name_from_JSONAPIMeta'), (
3961
'resource_name from model incorrect on list')
4062

4163
# serializer > model
4264
serializers.CommentSerializer.Meta.resource_name = "resource_name_from_serializer"
43-
response = self.client.get(reverse("comment-list"))
65+
response = client.get(reverse("comment-list"))
4466
data = load_json(response.content)['data'][0]
4567
assert (data.get('type') == 'resource_name_from_serializer'), (
4668
'resource_name from serializer incorrect on list')
4769

4870
# view > serializer > model
4971
views.CommentViewSet.resource_name = 'resource_name_from_view'
50-
response = self.client.get(reverse("comment-list"))
72+
response = client.get(reverse("comment-list"))
5173
data = load_json(response.content)['data'][0]
5274
assert (data.get('type') == 'resource_name_from_view'), (
5375
'resource_name from view incorrect on list')
5476

55-
def tearDown(self):
77+
def teardown_method(self, method):
5678
models.Comment.__bases__ = (models.Comment.__bases__[0],)
5779
try:
5880
delattr(serializers.CommentSerializer.Meta, "resource_name")
@@ -65,69 +87,49 @@ def tearDown(self):
6587

6688

6789
@pytest.mark.usefixtures("single_entry")
68-
class ResourceNameConsistencyTest(APITestCase):
90+
class TestResourceNameConsistency:
6991

7092
# Included rename tests
71-
def test_type_match_on_included_and_inline_base(self):
72-
self._check_relationship_and_included_comment_type_are_the_same(reverse("entry-list"))
93+
def test_type_match_on_included_and_inline_base(self, client):
94+
_check_relationship_and_included_comment_type_are_the_same(client, reverse("entry-list"))
7395

74-
def test_type_match_on_included_and_inline_with_JSONAPIMeta(self):
96+
def test_type_match_on_included_and_inline_with_JSONAPIMeta(self, client):
7597
models.Comment.__bases__ += (_PatchedModel,)
7698

77-
self._check_relationship_and_included_comment_type_are_the_same(reverse("entry-list"))
99+
_check_relationship_and_included_comment_type_are_the_same(client, reverse("entry-list"))
78100

79-
def test_type_match_on_included_and_inline_with_serializer_resource_name(self):
101+
def test_type_match_on_included_and_inline_with_serializer_resource_name(self, client):
80102
serializers.CommentSerializer.Meta.resource_name = "resource_name_from_serializer"
81103

82-
self._check_relationship_and_included_comment_type_are_the_same(reverse("entry-list"))
104+
_check_relationship_and_included_comment_type_are_the_same(client, reverse("entry-list"))
83105

84-
def test_type_match_on_included_and_inline_with_serializer_resource_name_and_JSONAPIMeta(self):
106+
def test_type_match_on_included_and_inline_with_serializer_resource_name_and_JSONAPIMeta(self, client):
85107
models.Comment.__bases__ += (_PatchedModel,)
86108
serializers.CommentSerializer.Meta.resource_name = "resource_name_from_serializer"
87109

88-
self._check_relationship_and_included_comment_type_are_the_same(reverse("entry-list"))
110+
_check_relationship_and_included_comment_type_are_the_same(client, reverse("entry-list"))
89111

90112
# Relation rename tests
91-
def test_resource_and_relationship_type_match(self):
92-
self._check_resource_and_relationship_comment_type_match()
113+
def test_resource_and_relationship_type_match(self, client):
114+
_check_resource_and_relationship_comment_type_match(client)
93115

94-
def test_resource_and_relationship_type_match_with_serializer_resource_name(self):
116+
def test_resource_and_relationship_type_match_with_serializer_resource_name(self, client):
95117
serializers.CommentSerializer.Meta.resource_name = "resource_name_from_serializer"
96118

97-
self._check_resource_and_relationship_comment_type_match()
119+
_check_resource_and_relationship_comment_type_match(client)
98120

99-
def test_resource_and_relationship_type_match_with_JSONAPIMeta(self):
121+
def test_resource_and_relationship_type_match_with_JSONAPIMeta(self, client):
100122
models.Comment.__bases__ += (_PatchedModel,)
101123

102-
self._check_resource_and_relationship_comment_type_match()
124+
_check_resource_and_relationship_comment_type_match(client)
103125

104-
def test_resource_and_relationship_type_match_with_serializer_resource_name_and_JSONAPIMeta(self):
126+
def test_resource_and_relationship_type_match_with_serializer_resource_name_and_JSONAPIMeta(self, client):
105127
models.Comment.__bases__ += (_PatchedModel,)
106128
serializers.CommentSerializer.Meta.resource_name = "resource_name_from_serializer"
107129

108-
self._check_resource_and_relationship_comment_type_match()
109-
110-
def _check_resource_and_relationship_comment_type_match(self):
111-
entry_response = self.client.get(reverse("entry-list"))
112-
comment_response = self.client.get(reverse("comment-list"))
113-
114-
comment_resource_type = load_json(comment_response.content).get('data')[0].get('type')
115-
comment_relationship_type = load_json(entry_response.content).get(
116-
'data')[0].get('relationships').get('comments').get('data')[0].get('type')
117-
118-
assert comment_resource_type == comment_relationship_type, "The resource type seen in the relationships and head resource do not match"
119-
120-
def _check_relationship_and_included_comment_type_are_the_same(self, url):
121-
response = self.client.get(url + "?include=comments")
122-
data = load_json(response.content).get('data')[0]
123-
comment = load_json(response.content).get('included')[0]
124-
125-
comment_relationship_type = data.get('relationships').get('comments').get('data')[0].get('type')
126-
comment_included_type = comment.get('type')
127-
128-
assert comment_relationship_type == comment_included_type, "The resource type seen in the relationships and included do not match"
130+
_check_resource_and_relationship_comment_type_match(client)
129131

130-
def tearDown(self):
132+
def teardown_method(self, method):
131133
models.Comment.__bases__ = (models.Comment.__bases__[0],)
132134
try:
133135
delattr(serializers.CommentSerializer.Meta, "resource_name")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /