-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathtest_generics.py
287 lines (240 loc) · 9.15 KB
/
test_generics.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from __future__ import unicode_literals, print_function
import json
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import RequestFactory
from rest_framework import status
from .simple_app.models import SimpleModel, UniqueTogetherModel
from .simple_app.views import FilteredBulkAPIView, SimpleBulkAPIView
class TestBulkAPIView(TestCase):
def setUp(self):
super(TestBulkAPIView, self).setUp()
self.view = SimpleBulkAPIView.as_view()
self.request = RequestFactory()
def test_get(self):
"""
Test that GET request is successful on bulk view.
"""
response = self.view(self.request.get(''))
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_post_single(self):
"""
Test that POST request with single resource only creates a single resource.
"""
response = self.view(self.request.post(
'',
json.dumps({'contents': 'hello world', 'number': 1}),
content_type='application/json',
))
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(SimpleModel.objects.count(), 1)
self.assertEqual(SimpleModel.objects.get().contents, 'hello world')
def test_post_bulk(self):
"""
Test that POST request with multiple resources creates all posted resources.
"""
response = self.view(self.request.post(
'',
json.dumps([
{'contents': 'hello world', 'number': 1},
{'contents': 'hello mars', 'number': 2},
]),
content_type='application/json',
))
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(SimpleModel.objects.count(), 2)
self.assertEqual(list(SimpleModel.objects.all().values_list('contents', flat=True)), [
'hello world',
'hello mars',
])
def test_put(self):
"""
Test that PUT request updates all submitted resources.
"""
obj1 = SimpleModel.objects.create(contents='hello world', number=1)
obj2 = SimpleModel.objects.create(contents='hello mars', number=2)
response = self.view(self.request.put(
'',
json.dumps([
{'contents': 'foo', 'number': 3, 'id': obj1.pk},
{'contents': 'bar', 'number': 4, 'id': obj2.pk},
]),
content_type='application/json',
))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(SimpleModel.objects.count(), 2)
self.assertEqual(
list(SimpleModel.objects.all().values_list('id', 'contents', 'number')),
[
(obj1.pk, 'foo', 3),
(obj2.pk, 'bar', 4),
]
)
def test_patch(self):
"""
Test that PATCH request partially updates all submitted resources.
"""
obj1 = SimpleModel.objects.create(contents='hello world', number=1)
obj2 = SimpleModel.objects.create(contents='hello mars', number=2)
response = self.view(self.request.patch(
'',
json.dumps([
{'contents': 'foo', 'id': obj1.pk},
{'contents': 'bar', 'id': obj2.pk},
]),
content_type='application/json',
))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(SimpleModel.objects.count(), 2)
self.assertEqual(
list(SimpleModel.objects.all().values_list('id', 'contents', 'number')),
[
(obj1.pk, 'foo', 1),
(obj2.pk, 'bar', 2),
]
)
def test_delete_not_filtered(self):
"""
Test that DELETE is not allowed when results are not filtered.
"""
SimpleModel.objects.create(contents='hello world', number=1)
SimpleModel.objects.create(contents='hello mars', number=10)
response = self.view(self.request.delete(''))
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_delete_filtered(self):
"""
Test that DELETE removes all filtered resources.
"""
SimpleModel.objects.create(contents='hello world', number=1)
SimpleModel.objects.create(contents='hello mars', number=10)
response = FilteredBulkAPIView.as_view()(self.request.delete(''))
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(SimpleModel.objects.count(), 1)
self.assertEqual(SimpleModel.objects.get().contents, 'hello world')
def test_options(self):
"""
Test that OPTIONS request is successful on bulk view.
"""
response = self.view(self.request.options(''))
self.assertEqual(response.status_code, status.HTTP_200_OK)
class TestBulkAPIViewSet(TestCase):
"""
Integration class testing that viewset requests are correctly
routed via bulk router and that expected status code is returned.
"""
def setUp(self):
super(TestBulkAPIViewSet, self).setUp()
self.url = reverse('api:simple-list')
def test_get_single(self):
"""
Test that we are still able to query single resource
"""
response = self.client.get(self.url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get(self):
"""
Test that GET returns 200
"""
obj = SimpleModel.objects.create(contents='hello world', number=7)
response = self.client.get(reverse('api:simple-detail', args=[obj.pk]))
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_post_single(self):
"""
Test that POST with single resource returns 201
"""
response = self.client.post(
self.url,
data=json.dumps({
'contents': 'hello world',
'number': 1,
}),
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_post_bulk(self):
"""
Test that POST with multiple resources returns 201
"""
response = self.client.post(
self.url,
data=json.dumps([
{
'contents': 'hello world',
'number': 1,
},
{
'contents': 'hello mars',
'number': 2,
},
]),
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_put(self):
"""
Test that PUT with multiple resources returns 200
"""
obj1 = SimpleModel.objects.create(contents='hello world', number=7)
obj2 = SimpleModel.objects.create(contents='hello mars', number=10)
response = self.client.put(
self.url,
data=json.dumps([
{
'contents': 'foo',
'number': 1,
'id': obj1.pk,
},
{
'contents': 'bar',
'number': 2,
'id': obj2.pk,
},
]),
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_patch(self):
"""
Test that PATCH with multiple partial resources returns 200
"""
obj1 = SimpleModel.objects.create(contents='hello world', number=7)
obj2 = SimpleModel.objects.create(contents='hello mars', number=10)
response = self.client.patch(
self.url,
data=json.dumps([
{
'contents': 'foo',
'id': obj1.pk,
},
{
'contents': 'bar',
'id': obj2.pk,
},
]),
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_patch_unique_together(self):
"""
Test that PATCH with multiple partial resources returns 200
even on model with unique together columns
"""
obj1 = UniqueTogetherModel.objects.create(foo=1, bar=2)
obj2 = UniqueTogetherModel.objects.create(foo=3, bar=4)
response = self.client.patch(
reverse('api:unique-together-list'),
data=json.dumps([
{'foo': 5, 'id': obj1.pk},
{'foo': 6, 'id': obj2.pk},
]),
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_delete(self):
"""
Test that PATCH with multiple partial resources returns 200
"""
SimpleModel.objects.create(contents='hello world', number=7)
SimpleModel.objects.create(contents='hello mars', number=10)
response = self.client.delete(self.url)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)