Added POST

master
Jason Zhu 2020-10-21 16:05:45 +11:00
parent 219b5ae604
commit 6e2ddad059
2 changed files with 50 additions and 4 deletions

View File

@ -58,3 +58,42 @@ class GetSinglePuppyTest(TestCase):
response = client.get(
reverse('get_delete_update_puppy', kwargs={'pk': 30}))
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
class CreateNewPuppyTest(TestCase):
"""
Test module for inserting a new puppy
1. inserting a valid puppy
2. inserting a invalid puppy
"""
def setUp(self):
self.valid_payload = {
'name': 'Muffin',
'age': 4,
'breed': 'Pamerion',
'color': 'White'
}
# Why this syntax works?
self.invalid_payload = {
'name': '',
'age': 4,
'breed': 'Pamerion',
'color': 'White'
}
def test_create_valid_puppy(self):
response = client.post(
reverse('get_post_puppies'),
data=json.dumps(self.valid_payload),
content_type='application/json'
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_create_invalid_puppy(self):
response = client.post(
reverse('get_post_puppies'),
data=json.dumps(self.invalid_payload),
content_type='application/json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

View File

@ -35,7 +35,14 @@ def get_post_puppies(request):
return Response(serializer.data)
# insert a new record for a puppy
elif request.method == 'POST':
return Response({})
# 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')),
'breed': request.data.get('breed'),
'color': request.data.get('color')
}
serializer = PuppySerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)