33
44from example .tests .utils import load_json
55
6- from rest_framework .test import APITestCase
7- from example import models , serializers
6+ from example import models , serializers , views
87pytestmark = pytest .mark .django_db
98
109
11- def test_model_resource_name_on_list (single_entry , client ):
12- response = client .get (reverse ("renamed-authors-list" ))
13- data = load_json (response .content )['data' ]
14- # name should be super-author instead of model name RenamedAuthor
15- assert [x .get ('type' ) for x in data ] == ['super-author' ], 'List included types are incorrect'
10+ class _PatchedModel :
11+ class JSONAPIMeta :
12+ resource_name = "resource_name_from_JSONAPIMeta"
1613
17- @pytest .mark .usefixtures ("single_entry" )
18- class ResourceNameConsistencyTest (APITestCase ):
19-
20- def test_type_match_on_included_and_inline_base (self ):
21- self ._check_relationship_and_included_comment_type_are_the_same (reverse ("entry-list" ))
2214
23- def test_type_match_on_included_and_inline_with_JSONAPIMeta (self ):
24- models .Comment .__bases__ += (self ._PatchedModel ,)
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" ))
2518
26- self ._check_relationship_and_included_comment_type_are_the_same (reverse ("entry-list" ))
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' )
2722
28- def test_type_match_on_included_and_inline_with_serializer_resource_name (self ):
29- serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
23+ assert comment_resource_type == comment_relationship_type , "The resource type seen in the relationships and head resource do not match"
3024
31- self ._check_relationship_and_included_comment_type_are_the_same (reverse ("entry-list" ))
3225
33- def test_type_match_on_included_and_inline_with_serializer_resource_name_and_JSONAPIMeta (self ):
34- models .Comment .__bases__ += (self ._PatchedModel ,)
35- serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
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 ]
3630
37- self ._check_relationship_and_included_comment_type_are_the_same (reverse ("entry-list" ))
31+ comment_relationship_type = data .get ('relationships' ).get ('comments' ).get ('data' )[0 ].get ('type' )
32+ comment_included_type = comment .get ('type' )
3833
39- def test_resource_and_relationship_type_match (self ):
40- self ._check_resource_and_relationship_comment_type_match ()
34+ assert comment_relationship_type == comment_included_type , "The resource type seen in the relationships and included do not match"
4135
42- def test_resource_and_relationship_type_match_with_serializer_resource_name (self ):
36+
37+ @pytest .mark .usefixtures ("single_entry" )
38+ class TestModelResourceName :
39+
40+ def test_model_resource_name_on_list (self , client ):
41+ models .Comment .__bases__ += (_PatchedModel ,)
42+ response = client .get (reverse ("comment-list" ))
43+ data = load_json (response .content )['data' ][0 ]
44+ # name should be super-author instead of model name RenamedAuthor
45+ assert (data .get ('type' ) == 'resource_name_from_JSONAPIMeta' ), (
46+ 'resource_name from model incorrect on list' )
47+
48+ # Precedence tests
49+ def test_resource_name_precendence (self , client ):
50+ # default
51+ response = client .get (reverse ("comment-list" ))
52+ data = load_json (response .content )['data' ][0 ]
53+ assert (data .get ('type' ) == 'comments' ), (
54+ 'resource_name from model incorrect on list' )
55+
56+ # model > default
57+ models .Comment .__bases__ += (_PatchedModel ,)
58+ response = client .get (reverse ("comment-list" ))
59+ data = load_json (response .content )['data' ][0 ]
60+ assert (data .get ('type' ) == 'resource_name_from_JSONAPIMeta' ), (
61+ 'resource_name from model incorrect on list' )
62+
63+ # serializer > model
4364 serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
65+ response = client .get (reverse ("comment-list" ))
66+ data = load_json (response .content )['data' ][0 ]
67+ assert (data .get ('type' ) == 'resource_name_from_serializer' ), (
68+ 'resource_name from serializer incorrect on list' )
69+
70+ # view > serializer > model
71+ views .CommentViewSet .resource_name = 'resource_name_from_view'
72+ response = client .get (reverse ("comment-list" ))
73+ data = load_json (response .content )['data' ][0 ]
74+ assert (data .get ('type' ) == 'resource_name_from_view' ), (
75+ 'resource_name from view incorrect on list' )
76+
77+ def teardown_method (self , method ):
78+ models .Comment .__bases__ = (models .Comment .__bases__ [0 ],)
79+ try :
80+ delattr (serializers .CommentSerializer .Meta , "resource_name" )
81+ except AttributeError :
82+ pass
83+ try :
84+ delattr (views .CommentViewSet , "resource_name" )
85+ except AttributeError :
86+ pass
87+
88+
89+ @pytest .mark .usefixtures ("single_entry" )
90+ class TestResourceNameConsistency :
91+
92+ # Included rename tests
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" ))
95+
96+ def test_type_match_on_included_and_inline_with_JSONAPIMeta (self , client ):
97+ models .Comment .__bases__ += (_PatchedModel ,)
4498
45- self . _check_resource_and_relationship_comment_type_match ( )
99+ _check_relationship_and_included_comment_type_are_the_same ( client , reverse ( "entry-list" ) )
46100
47- def test_resource_and_relationship_type_match_with_JSONAPIMeta (self ):
48- models . Comment . __bases__ += ( self . _PatchedModel ,)
101+ def test_type_match_on_included_and_inline_with_serializer_resource_name (self , client ):
102+ serializers . CommentSerializer . Meta . resource_name = "resource_name_from_serializer"
49103
50- self . _check_resource_and_relationship_comment_type_match ( )
104+ _check_relationship_and_included_comment_type_are_the_same ( client , reverse ( "entry-list" ) )
51105
52- def test_resource_and_relationship_type_match_with_serializer_resource_name_and_JSONAPIMeta (self ):
53- models .Comment .__bases__ += (self . _PatchedModel ,)
106+ def test_type_match_on_included_and_inline_with_serializer_resource_name_and_JSONAPIMeta (self , client ):
107+ models .Comment .__bases__ += (_PatchedModel ,)
54108 serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
55109
56- self ._check_resource_and_relationship_comment_type_match ()
110+ _check_relationship_and_included_comment_type_are_the_same (client , reverse ("entry-list" ))
111+
112+ # Relation rename tests
113+ def test_resource_and_relationship_type_match (self , client ):
114+ _check_resource_and_relationship_comment_type_match (client )
57115
58- def _check_resource_and_relationship_comment_type_match (self ):
59- entry_response = self .client .get (reverse ("entry-list" ))
60- comment_response = self .client .get (reverse ("comment-list" ))
116+ def test_resource_and_relationship_type_match_with_serializer_resource_name (self , client ):
117+ serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
61118
62- comment_resource_type = load_json (comment_response .content ).get ('data' )[0 ].get ('type' )
63- comment_relationship_type = load_json (entry_response .content ).get (
64- 'data' )[0 ].get ('relationships' ).get ('comments' ).get ('data' )[0 ].get ('type' )
119+ _check_resource_and_relationship_comment_type_match (client )
65120
66- assert comment_resource_type == comment_relationship_type , "The resource type seen in the relationships and head resource do not match"
121+ def test_resource_and_relationship_type_match_with_JSONAPIMeta (self , client ):
122+ models .Comment .__bases__ += (_PatchedModel ,)
67123
68- def _check_relationship_and_included_comment_type_are_the_same (self , url ):
69- response = self .client .get (url + "?include=comments" )
70- data = load_json (response .content ).get ('data' )[0 ]
71- comment = load_json (response .content ).get ('included' )[0 ]
124+ _check_resource_and_relationship_comment_type_match (client )
72125
73- comment_relationship_type = data .get ('relationships' ).get ('comments' ).get ('data' )[0 ].get ('type' )
74- comment_included_type = comment .get ('type' )
126+ def test_resource_and_relationship_type_match_with_serializer_resource_name_and_JSONAPIMeta (self , client ):
127+ models .Comment .__bases__ += (_PatchedModel ,)
128+ serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
75129
76- 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 )
77131
78- def tearDown (self ):
132+ def teardown_method (self , method ):
79133 models .Comment .__bases__ = (models .Comment .__bases__ [0 ],)
80134 try :
81135 delattr (serializers .CommentSerializer .Meta , "resource_name" )
82136 except AttributeError :
83137 pass
84-
85- class _PatchedModel :
86-
87- class JSONAPIMeta :
88- resource_name = "resource_name_from_JSONAPIMeta"
89-
0 commit comments