3
3
4
4
from example .tests .utils import load_json
5
5
6
- from rest_framework .test import APITestCase
7
6
from example import models , serializers , views
8
7
pytestmark = pytest .mark .django_db
9
8
@@ -13,46 +12,69 @@ class JSONAPIMeta:
13
12
resource_name = "resource_name_from_JSONAPIMeta"
14
13
15
14
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
+
16
37
@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 ):
19
41
models .Comment .__bases__ += (_PatchedModel ,)
20
- response = self . client .get (reverse ("comment-list" ))
42
+ response = client .get (reverse ("comment-list" ))
21
43
data = load_json (response .content )['data' ][0 ]
22
44
# name should be super-author instead of model name RenamedAuthor
23
45
assert (data .get ('type' ) == 'resource_name_from_JSONAPIMeta' ), (
24
46
'resource_name from model incorrect on list' )
25
47
26
48
# Precedence tests
27
- def test_resource_name_precendence (self ):
49
+ def test_resource_name_precendence (self , client ):
28
50
# default
29
- response = self . client .get (reverse ("comment-list" ))
51
+ response = client .get (reverse ("comment-list" ))
30
52
data = load_json (response .content )['data' ][0 ]
31
53
assert (data .get ('type' ) == 'comments' ), (
32
54
'resource_name from model incorrect on list' )
33
55
34
56
# model > default
35
57
models .Comment .__bases__ += (_PatchedModel ,)
36
- response = self . client .get (reverse ("comment-list" ))
58
+ response = client .get (reverse ("comment-list" ))
37
59
data = load_json (response .content )['data' ][0 ]
38
60
assert (data .get ('type' ) == 'resource_name_from_JSONAPIMeta' ), (
39
61
'resource_name from model incorrect on list' )
40
62
41
63
# serializer > model
42
64
serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
43
- response = self . client .get (reverse ("comment-list" ))
65
+ response = client .get (reverse ("comment-list" ))
44
66
data = load_json (response .content )['data' ][0 ]
45
67
assert (data .get ('type' ) == 'resource_name_from_serializer' ), (
46
68
'resource_name from serializer incorrect on list' )
47
69
48
70
# view > serializer > model
49
71
views .CommentViewSet .resource_name = 'resource_name_from_view'
50
- response = self . client .get (reverse ("comment-list" ))
72
+ response = client .get (reverse ("comment-list" ))
51
73
data = load_json (response .content )['data' ][0 ]
52
74
assert (data .get ('type' ) == 'resource_name_from_view' ), (
53
75
'resource_name from view incorrect on list' )
54
76
55
- def tearDown (self ):
77
+ def teardown_method (self , method ):
56
78
models .Comment .__bases__ = (models .Comment .__bases__ [0 ],)
57
79
try :
58
80
delattr (serializers .CommentSerializer .Meta , "resource_name" )
@@ -65,69 +87,49 @@ def tearDown(self):
65
87
66
88
67
89
@pytest .mark .usefixtures ("single_entry" )
68
- class ResourceNameConsistencyTest ( APITestCase ) :
90
+ class TestResourceNameConsistency :
69
91
70
92
# 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" ))
73
95
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 ):
75
97
models .Comment .__bases__ += (_PatchedModel ,)
76
98
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" ))
78
100
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 ):
80
102
serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
81
103
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" ))
83
105
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 ):
85
107
models .Comment .__bases__ += (_PatchedModel ,)
86
108
serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
87
109
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" ))
89
111
90
112
# 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 )
93
115
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 ):
95
117
serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
96
118
97
- self . _check_resource_and_relationship_comment_type_match ()
119
+ _check_resource_and_relationship_comment_type_match (client )
98
120
99
- def test_resource_and_relationship_type_match_with_JSONAPIMeta (self ):
121
+ def test_resource_and_relationship_type_match_with_JSONAPIMeta (self , client ):
100
122
models .Comment .__bases__ += (_PatchedModel ,)
101
123
102
- self . _check_resource_and_relationship_comment_type_match ()
124
+ _check_resource_and_relationship_comment_type_match (client )
103
125
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 ):
105
127
models .Comment .__bases__ += (_PatchedModel ,)
106
128
serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
107
129
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 )
129
131
130
- def tearDown (self ):
132
+ def teardown_method (self , method ):
131
133
models .Comment .__bases__ = (models .Comment .__bases__ [0 ],)
132
134
try :
133
135
delattr (serializers .CommentSerializer .Meta , "resource_name" )
0 commit comments