Finished implementation/test for PUT option
parent
6e2ddad059
commit
c0f3354f1f
|
@ -97,3 +97,41 @@ class CreateNewPuppyTest(TestCase):
|
|||
content_type='application/json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class UpdateSinglePuppyTest(TestCase):
|
||||
""" Test module for updating an existing puppy record
|
||||
Test PUT option
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.casper = Puppy.objects.create(
|
||||
name='Casper', age=3, breed='Bull Dog', color='Black')
|
||||
self.muffin = Puppy.objects.create(
|
||||
name='Muffy', age=1, breed='Gradane', color='Brown')
|
||||
self.valid_payload = {
|
||||
'name': 'Muffy',
|
||||
'age': 2,
|
||||
'breed': 'Labrador',
|
||||
'color': 'Black'
|
||||
}
|
||||
self.invalid_payload = {
|
||||
'name': '',
|
||||
'age': 4,
|
||||
'breed': 'Pamerion',
|
||||
'color': 'White'
|
||||
}
|
||||
|
||||
def test_valid_update_puppy(self):
|
||||
response = client.put(
|
||||
reverse('get_delete_update_puppy', kwargs={'pk': self.muffin.pk}),
|
||||
data=json.dumps(self.valid_payload),
|
||||
content_type='application/json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
def test_invalid_update_puppy(self):
|
||||
response = client.put(
|
||||
reverse('get_delete_update_puppy', kwargs={'pk': self.muffin.pk}),
|
||||
data=json.dumps(self.invalid_payload),
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
|
|
@ -7,7 +7,7 @@ from rest_framework import status
|
|||
from .models import Puppy
|
||||
from .serializers import PuppySerializer
|
||||
|
||||
@api_view(['GET', 'DELETE', 'UPDATE'])
|
||||
@api_view(['GET', 'DELETE', 'PUT'])
|
||||
def get_delete_update_puppy(request, pk):
|
||||
try:
|
||||
puppy = Puppy.objects.get(pk=pk)
|
||||
|
@ -16,6 +16,9 @@ def get_delete_update_puppy(request, pk):
|
|||
|
||||
# get details of a single puppy
|
||||
if request.method == 'GET':
|
||||
|
||||
# return Response({})
|
||||
|
||||
# we get the puppy using an ID. Run the tests to ensure they all pass.
|
||||
serializer = PuppySerializer(puppy)
|
||||
return Response(serializer.data)
|
||||
|
@ -24,17 +27,30 @@ def get_delete_update_puppy(request, pk):
|
|||
return Response({})
|
||||
# update details of a single puppy
|
||||
elif request.method == 'PUT':
|
||||
return Response({})
|
||||
|
||||
# return Response({})
|
||||
|
||||
serializer = PuppySerializer(puppy, data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@api_view(['GET', 'POST'])
|
||||
def get_post_puppies(request):
|
||||
# get all puppies
|
||||
if request.method == 'GET':
|
||||
|
||||
# return Response({})
|
||||
|
||||
puppies = Puppy.objects.all()
|
||||
serializer = PuppySerializer(puppies, many=True) # generate a nested list of 'edit' items
|
||||
return Response(serializer.data)
|
||||
# insert a new record for a puppy
|
||||
elif request.method == 'POST':
|
||||
|
||||
# return Response({})
|
||||
|
||||
data = {
|
||||
'name': request.data.get('name'),
|
||||
'age': int(request.data.get('age')),
|
||||
|
|
Loading…
Reference in New Issue