33
44from example .tests .utils import load_json
55
6+ from rest_framework .test import APITestCase
7+ from example import models , serializers
68pytestmark = pytest .mark .django_db
79
810
@@ -11,3 +13,77 @@ def test_model_resource_name_on_list(single_entry, client):
1113 data = load_json (response .content )['data' ]
1214 # name should be super-author instead of model name RenamedAuthor
1315 assert [x .get ('type' ) for x in data ] == ['super-author' ], 'List included types are incorrect'
16+
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" ))
22+
23+ def test_type_match_on_included_and_inline_with_JSONAPIMeta (self ):
24+ models .Comment .__bases__ += (self ._PatchedModel ,)
25+
26+ self ._check_relationship_and_included_comment_type_are_the_same (reverse ("entry-list" ))
27+
28+ def test_type_match_on_included_and_inline_with_serializer_resource_name (self ):
29+ serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
30+
31+ self ._check_relationship_and_included_comment_type_are_the_same (reverse ("entry-list" ))
32+
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"
36+
37+ self ._check_relationship_and_included_comment_type_are_the_same (reverse ("entry-list" ))
38+
39+ def test_resource_and_relationship_type_match (self ):
40+ self ._check_resource_and_relationship_comment_type_match ()
41+
42+ def test_resource_and_relationship_type_match_with_serializer_resource_name (self ):
43+ serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
44+
45+ self ._check_resource_and_relationship_comment_type_match ()
46+
47+ def test_resource_and_relationship_type_match_with_JSONAPIMeta (self ):
48+ models .Comment .__bases__ += (self ._PatchedModel ,)
49+
50+ self ._check_resource_and_relationship_comment_type_match ()
51+
52+ def test_resource_and_relationship_type_match_with_serializer_resource_name_and_JSONAPIMeta (self ):
53+ models .Comment .__bases__ += (self ._PatchedModel ,)
54+ serializers .CommentSerializer .Meta .resource_name = "resource_name_from_serializer"
55+
56+ self ._check_resource_and_relationship_comment_type_match ()
57+
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" ))
61+
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' )
65+
66+ assert comment_resource_type == comment_relationship_type , "The resource type seen in the relationships and head resource do not match"
67+
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 ]
72+
73+ comment_relationship_type = data .get ('relationships' ).get ('comments' ).get ('data' )[0 ].get ('type' )
74+ comment_included_type = comment .get ('type' )
75+
76+ assert comment_relationship_type == comment_included_type , "The resource type seen in the relationships and included do not match"
77+
78+ def tearDown (self ):
79+ models .Comment .__bases__ = (models .Comment .__bases__ [0 ],)
80+ try :
81+ delattr (serializers .CommentSerializer .Meta , "resource_name" )
82+ except AttributeError :
83+ pass
84+
85+ class _PatchedModel :
86+
87+ class JSONAPIMeta :
88+ resource_name = "resource_name_from_JSONAPIMeta"
89+
0 commit comments