forked from django-json-api/django-rest-framework-json-api
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
137 lines (102 loc) · 4.8 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
importpytest
fromdjango.confimportsettings
fromdjango.contrib.authimportget_user_model
fromdjango.utilsimportsix
fromrest_frameworkimportserializers
fromrest_framework.genericsimportGenericAPIView
fromrest_framework.responseimportResponse
fromrest_framework.viewsimportAPIView
fromexample.serializersimport (EntrySerializer, BlogSerializer,
AuthorSerializer, CommentSerializer)
fromrest_framework_json_apiimportutils
fromrest_framework_json_api.utilsimportget_included_serializers
pytestmark=pytest.mark.django_db
classNonModelResourceSerializer(serializers.Serializer):
classMeta:
resource_name='users'
classResourceSerializer(serializers.ModelSerializer):
classMeta:
fields= ('username',)
model=get_user_model()
deftest_get_resource_name():
view=APIView()
context= {'view': view}
setattr(settings, 'JSON_API_FORMAT_TYPES', None)
assert'APIViews'==utils.get_resource_name(context), 'not formatted'
context= {'view': view}
setattr(settings, 'JSON_API_FORMAT_TYPES', 'dasherize')
assert'api-views'==utils.get_resource_name(context), 'derived from view'
view.model=get_user_model()
assert'users'==utils.get_resource_name(context), 'derived from view model'
view.resource_name='custom'
assert'custom'==utils.get_resource_name(context), 'manually set on view'
view.response=Response(status=403)
assert'errors'==utils.get_resource_name(context), 'handles 4xx error'
view.response=Response(status=500)
assert'errors'==utils.get_resource_name(context), 'handles 500 error'
view=GenericAPIView()
view.serializer_class=ResourceSerializer
context= {'view': view}
assert'users'==utils.get_resource_name(context), 'derived from serializer'
view.serializer_class.Meta.resource_name='rcustom'
assert'rcustom'==utils.get_resource_name(context), 'set on serializer'
view=GenericAPIView()
view.serializer_class=NonModelResourceSerializer
context= {'view': view}
assert'users'==utils.get_resource_name(context), 'derived from non-model serializer'
deftest_format_keys():
underscored= {
'first_name': 'a',
'last_name': 'b',
}
output= {'firstName': 'a', 'lastName': 'b'}
assertutils.format_keys(underscored, 'camelize') ==output
output= {'FirstName': 'a', 'LastName': 'b'}
assertutils.format_keys(underscored, 'capitalize') ==output
output= {'first-name': 'a', 'last-name': 'b'}
assertutils.format_keys(underscored, 'dasherize') ==output
new_input= {'firstName': 'a', 'lastName': 'b'}
assertutils.format_keys(new_input, 'underscore') ==underscored
output= [{'first-name': 'a', 'last-name': 'b'}]
assertutils.format_keys([underscored], 'dasherize') ==output
deftest_format_value():
assertutils.format_value('first_name', 'camelize') =='firstName'
assertutils.format_value('first_name', 'capitalize') =='FirstName'
assertutils.format_value('first_name', 'dasherize') =='first-name'
assertutils.format_value('first-name', 'underscore') =='first_name'
deftest_format_resource_type():
assertutils.format_resource_type('first_name', 'capitalize') =='FirstNames'
assertutils.format_resource_type('first_name', 'camelize') =='firstNames'
classSerializerWithIncludedSerializers(EntrySerializer):
included_serializers= {
'blog': BlogSerializer,
'authors': 'example.serializers.AuthorSerializer',
'comments': 'example.serializers.CommentSerializer',
'self': 'self'# this wouldn't make sense in practice (and would be prohibited by
# IncludedResourcesValidationMixin) but it's useful for the test
}
deftest_get_included_serializers_against_class():
klass=SerializerWithIncludedSerializers
included_serializers=get_included_serializers(klass)
expected_included_serializers= {
'blog': BlogSerializer,
'authors': AuthorSerializer,
'comments': CommentSerializer,
'self': klass
}
assert (six.viewkeys(included_serializers) ==six.viewkeys(klass.included_serializers),
'the keys must be preserved')
assertincluded_serializers==expected_included_serializers
deftest_get_included_serializers_against_instance():
klass=SerializerWithIncludedSerializers
instance=klass()
included_serializers=get_included_serializers(instance)
expected_included_serializers= {
'blog': BlogSerializer,
'authors': AuthorSerializer,
'comments': CommentSerializer,
'self': klass
}
assert (six.viewkeys(included_serializers) ==six.viewkeys(klass.included_serializers),
'the keys must be preserved')
assertincluded_serializers==expected_included_serializers