forked from django-json-api/django-rest-framework-json-api
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generic_viewset.py
126 lines (100 loc) · 3.57 KB
/
test_generic_viewset.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
importjson
fromdjango.core.urlresolversimportreverse
fromdjango.confimportsettings
fromexample.testsimportTestBase
fromexample.tests.utilsimportload_json
classGenericViewSet(TestBase):
"""
Test expected responses coming from a Generic ViewSet
"""
defsetUp(self):
super(GenericViewSet, self).setUp()
setattr(settings, 'JSON_API_FORMAT_KEYS', 'dasherize')
deftearDown(self):
setattr(settings, 'JSON_API_FORMAT_KEYS', 'camelize')
deftest_default_rest_framework_behavior(self):
"""
This is more of an example really, showing default behavior
"""
url=reverse('user-default', kwargs={'pk': self.miles.pk})
response=self.client.get(url)
self.assertEqual(200, response.status_code)
expected= {
'id': 2,
'first_name': 'Miles',
'last_name': 'Davis',
'email': 'miles@example.com'
}
parsed_content=load_json(response.content)
assertexpected==parsed_content
deftest_ember_expected_renderer(self):
"""
The :class:`UserEmber` ViewSet has the ``resource_name`` of 'data'
so that should be the key in the JSON response.
"""
url=reverse('user-manual-resource-name', kwargs={'pk': self.miles.pk})
response=self.client.get(url)
self.assertEqual(200, response.status_code)
expected= {
'data': {
'type': 'data',
'id': '2',
'attributes': {
'first-name': 'Miles',
'last-name': 'Davis',
'email': 'miles@example.com'
}
}
}
parsed_content=load_json(response.content)
assertexpected==parsed_content
deftest_default_validation_exceptions(self):
"""
Default validation exceptions should conform to json api spec
"""
expected= {
'errors': [
{
'status': '400',
'source': {
'pointer': '/data/attributes/email',
},
'detail': 'Enter a valid email address.',
},
{
'status': '400',
'source': {
'pointer': '/data/attributes/first-name',
},
'detail': 'There\'s a problem with first name',
}
]
}
response=self.client.post('/identities', {
'email': 'bar', 'first_name': 'alajflajaljalajlfjafljalj'})
parsed_content=load_json(response.content)
assertexpected==parsed_content
deftest_custom_validation_exceptions(self):
"""
Exceptions should be able to be formatted manually
"""
expected= {
'errors': [
{
'id': 'armageddon101',
'detail': 'Hey! You need a last name!',
'meta': 'something',
},
{
'status': '400',
'source': {
'pointer': '/data/attributes/email',
},
'detail': 'Enter a valid email address.',
},
]
}
response=self.client.post('/identities', {
'email': 'bar', 'last_name': 'alajflajaljalajlfjafljalj'})
parsed_content=load_json(response.content)
assertexpected==parsed_content