Compare commits

...

3 Commits

Author SHA1 Message Date
Jason Zhu 6e2ddad059 Added POST 2020-10-21 16:05:45 +11:00
Jason Zhu 219b5ae604 Finished GET Single 2020-10-21 16:00:59 +11:00
Jason Zhu 88c070696d Route: GET ALL 2020-10-21 15:47:16 +11:00
2 changed files with 108 additions and 5 deletions

View File

@ -7,4 +7,93 @@ from ..models import Puppy
from ..serializers import PuppySerializer from ..serializers import PuppySerializer
# initialize the APIClient app # initialize the APIClient app
client = Client() client = Client()
class GetAllPuppiesTest(TestCase):
"""
Test module for GET all puppies API
"""
def setUp(self):
Puppy.objects.create(
name='Casper', age=3, breed='Bull Dog', color='Black')
Puppy.objects.create(
name='Muffin', age=1, breed='Gradane', color='Brown')
Puppy.objects.create(
name='Rambo', age=2, breed='Labrador', color='Black')
Puppy.objects.create(
name='Ricky', age=6, breed='Labrador', color='Brown')
def test_get_all_puppies(self):
# get API response
response = client.get(reverse('get_post_puppies'))
# get data from db
puppies = Puppy.objects.all()
serializer = PuppySerializer(puppies, many=True)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
class GetSinglePuppyTest(TestCase):
""" Test module for GET single puppy API """
def setUp(self):
self.casper = Puppy.objects.create(
name='Casper', age=3, breed='Bull Dog', color='Black')
self.muffin = Puppy.objects.create(
name='Muffin', age=1, breed='Gradane', color='Brown')
self.rambo = Puppy.objects.create(
name='Rambo', age=2, breed='Labrador', color='Black')
self.ricky = Puppy.objects.create(
name='Ricky', age=6, breed='Labrador', color='Brown')
def test_get_valid_single_puppy(self):
response = client.get(
reverse('get_delete_update_puppy', kwargs={'pk': self.rambo.pk}))
puppy = Puppy.objects.get(pk=self.rambo.pk)
serializer = PuppySerializer(puppy)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_invalid_single_puppy(self):
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

@ -7,7 +7,7 @@ from rest_framework import status
from .models import Puppy from .models import Puppy
from .serializers import PuppySerializer from .serializers import PuppySerializer
@api_view(['GET', 'DELETE', 'PUT']) @api_view(['GET', 'DELETE', 'UPDATE'])
def get_delete_update_puppy(request, pk): def get_delete_update_puppy(request, pk):
try: try:
puppy = Puppy.objects.get(pk=pk) puppy = Puppy.objects.get(pk=pk)
@ -16,7 +16,9 @@ def get_delete_update_puppy(request, pk):
# get details of a single puppy # get details of a single puppy
if request.method == 'GET': 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)
# delete a single puppy # delete a single puppy
elif request.method == 'DELETE': elif request.method == 'DELETE':
return Response({}) return Response({})
@ -28,7 +30,19 @@ def get_delete_update_puppy(request, pk):
def get_post_puppies(request): def get_post_puppies(request):
# get all puppies # get all puppies
if request.method == 'GET': 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 # insert a new record for a puppy
elif request.method == 'POST': 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)