mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Merge branch 'num-query-checking' of https://github.com/mjtamlyn/django-rest-framework
This commit is contained in:
commit
126cadf27e
|
@ -60,6 +60,7 @@ class TestRootView(TestCase):
|
|||
GET requests to ListCreateAPIView should return list of objects.
|
||||
"""
|
||||
request = factory.get('/')
|
||||
with self.assertNumQueries(1):
|
||||
response = self.view(request).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data, self.data)
|
||||
|
@ -71,6 +72,7 @@ class TestRootView(TestCase):
|
|||
content = {'text': 'foobar'}
|
||||
request = factory.post('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(1):
|
||||
response = self.view(request).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(response.data, {'id': 4, 'text': 'foobar'})
|
||||
|
@ -84,6 +86,7 @@ class TestRootView(TestCase):
|
|||
content = {'text': 'foobar'}
|
||||
request = factory.put('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(0):
|
||||
response = self.view(request).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||
self.assertEqual(response.data, {"detail": "Method 'PUT' not allowed."})
|
||||
|
@ -93,6 +96,7 @@ class TestRootView(TestCase):
|
|||
DELETE requests to ListCreateAPIView should not be allowed
|
||||
"""
|
||||
request = factory.delete('/')
|
||||
with self.assertNumQueries(0):
|
||||
response = self.view(request).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||
self.assertEqual(response.data, {"detail": "Method 'DELETE' not allowed."})
|
||||
|
@ -102,6 +106,7 @@ class TestRootView(TestCase):
|
|||
OPTIONS requests to ListCreateAPIView should return metadata
|
||||
"""
|
||||
request = factory.options('/')
|
||||
with self.assertNumQueries(0):
|
||||
response = self.view(request).render()
|
||||
expected = {
|
||||
'parses': [
|
||||
|
@ -126,6 +131,7 @@ class TestRootView(TestCase):
|
|||
content = {'id': 999, 'text': 'foobar'}
|
||||
request = factory.post('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(1):
|
||||
response = self.view(request).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(response.data, {'id': 4, 'text': 'foobar'})
|
||||
|
@ -154,6 +160,7 @@ class TestInstanceView(TestCase):
|
|||
GET requests to RetrieveUpdateDestroyAPIView should return a single object.
|
||||
"""
|
||||
request = factory.get('/1')
|
||||
with self.assertNumQueries(1):
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data, self.data[0])
|
||||
|
@ -165,6 +172,7 @@ class TestInstanceView(TestCase):
|
|||
content = {'text': 'foobar'}
|
||||
request = factory.post('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(0):
|
||||
response = self.view(request).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||
self.assertEqual(response.data, {"detail": "Method 'POST' not allowed."})
|
||||
|
@ -176,6 +184,7 @@ class TestInstanceView(TestCase):
|
|||
content = {'text': 'foobar'}
|
||||
request = factory.put('/1', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(3):
|
||||
response = self.view(request, pk='1').render()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data, {'id': 1, 'text': 'foobar'})
|
||||
|
@ -190,6 +199,7 @@ class TestInstanceView(TestCase):
|
|||
request = factory.patch('/1', json.dumps(content),
|
||||
content_type='application/json')
|
||||
|
||||
with self.assertNumQueries(3):
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data, {'id': 1, 'text': 'foobar'})
|
||||
|
@ -201,6 +211,7 @@ class TestInstanceView(TestCase):
|
|||
DELETE requests to RetrieveUpdateDestroyAPIView should delete an object.
|
||||
"""
|
||||
request = factory.delete('/1')
|
||||
with self.assertNumQueries(2):
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
||||
self.assertEqual(response.content, six.b(''))
|
||||
|
@ -212,6 +223,7 @@ class TestInstanceView(TestCase):
|
|||
OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata
|
||||
"""
|
||||
request = factory.options('/')
|
||||
with self.assertNumQueries(0):
|
||||
response = self.view(request).render()
|
||||
expected = {
|
||||
'parses': [
|
||||
|
@ -236,6 +248,7 @@ class TestInstanceView(TestCase):
|
|||
content = {'id': 999, 'text': 'foobar'}
|
||||
request = factory.put('/1', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(3):
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data, {'id': 1, 'text': 'foobar'})
|
||||
|
@ -251,6 +264,7 @@ class TestInstanceView(TestCase):
|
|||
content = {'text': 'foobar'}
|
||||
request = factory.put('/1', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(4):
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(response.data, {'id': 1, 'text': 'foobar'})
|
||||
|
@ -263,9 +277,10 @@ class TestInstanceView(TestCase):
|
|||
at the requested url if it doesn't exist.
|
||||
"""
|
||||
content = {'text': 'foobar'}
|
||||
# pk fields can not be created on demand, only the database can set th pk for a new object
|
||||
# pk fields can not be created on demand, only the database can set the pk for a new object
|
||||
request = factory.put('/5', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(4):
|
||||
response = self.view(request, pk=5).render()
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
new_obj = self.objects.get(pk=5)
|
||||
|
@ -279,6 +294,7 @@ class TestInstanceView(TestCase):
|
|||
content = {'text': 'foobar'}
|
||||
request = factory.put('/test_slug', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(2):
|
||||
response = self.slug_based_view(request, slug='test_slug').render()
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(response.data, {'slug': 'test_slug', 'text': 'foobar'})
|
||||
|
|
Loading…
Reference in New Issue
Block a user