Add delete and Add update methods
This commit is contained in:
parent
e2fb03e48c
commit
7fd71eca7f
@ -10,6 +10,6 @@ class LibroSerializer(serializers.ModelSerializer):
|
||||
fields = "__all__"
|
||||
read_only_fields = (
|
||||
"id",
|
||||
"created_date",
|
||||
"updated_date",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
)
|
||||
|
@ -15,7 +15,7 @@ def ping(request):
|
||||
|
||||
class LibrosList(APIView):
|
||||
|
||||
def get(self, request, format=None):
|
||||
def get(self, request):
|
||||
libros = Libros.objects.all().order_by('created_at')
|
||||
serializer = LibroSerializer(libros, many=True)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
@ -30,10 +30,27 @@ class LibrosList(APIView):
|
||||
|
||||
class LibrosDetails(APIView):
|
||||
|
||||
def get(self, request, pk, format=None):
|
||||
def get(self, request, pk):
|
||||
libro = Libros.objects.filter(pk=pk).first()
|
||||
serializer = LibroSerializer(libro)
|
||||
if libro:
|
||||
serializer = LibroSerializer(libro)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
def put(self, request, pk):
|
||||
libro = Libros.objects.filter(pk=pk).first()
|
||||
serializer = LibroSerializer(libro, data=request.data)
|
||||
if libro and serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
def delete(self, request, pk):
|
||||
libro = Libros.objects.filter(pk=pk).first()
|
||||
if libro:
|
||||
serializer = LibroSerializer(libro)
|
||||
libro.delete()
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
@ -83,3 +83,120 @@ def test_get_all_books(client, faker):
|
||||
assert resp.data[0]["title"] == libro_1.title
|
||||
assert resp.data[1]["title"] == libro_2.title
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_remove_book(client):
|
||||
|
||||
# Given
|
||||
libro = Libros.objects.create(
|
||||
title="El fin de la eternidad",
|
||||
genre="Ciencia Ficción",
|
||||
author="Isaac Asimov",
|
||||
year="1955",
|
||||
)
|
||||
## Check exist
|
||||
resp_detail = client.get(f"/api/libros/{libro.id}/")
|
||||
assert resp_detail.status_code == 200
|
||||
assert resp_detail.data["title"] == "El fin de la eternidad"
|
||||
|
||||
# When
|
||||
resp_delete = client.delete(f"/api/libros/{libro.id}/")
|
||||
resp_list = client.get("/api/libros/")
|
||||
rest_new_detail = client.get(f"/api/libros/{libro.id}/")
|
||||
|
||||
# Then
|
||||
## Check status delete
|
||||
assert resp_delete.status_code == 200
|
||||
## Check return delete
|
||||
assert resp_delete.data["title"] == "El fin de la eternidad"
|
||||
## Check status list
|
||||
assert resp_list.status_code == 200
|
||||
## Check not item list
|
||||
assert len(resp_list.data) == 0
|
||||
## Check not exist detail
|
||||
assert rest_new_detail.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_remove_book_incorrect_id(client):
|
||||
# Given
|
||||
libro = Libros.objects.create(
|
||||
title="El fin de la eternidad",
|
||||
genre="Ciencia Ficción",
|
||||
author="Isaac Asimov",
|
||||
year="1955",
|
||||
)
|
||||
|
||||
# When
|
||||
resp = client.delete(f"/api/movies/-1/")
|
||||
|
||||
# Then
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_update_book(client):
|
||||
|
||||
# Given
|
||||
libro = Libros.objects.create(
|
||||
title="El fin de la eternidad",
|
||||
genre="Ciencia Ficción",
|
||||
author="Isaac Asimov",
|
||||
year="1955",
|
||||
)
|
||||
|
||||
# When
|
||||
resp = client.put(
|
||||
f"/api/libros/{libro.id}/",
|
||||
{
|
||||
"title": "Dune",
|
||||
"genre": "Ciencia Ficción",
|
||||
"author": "Frank Herbert",
|
||||
"year": "1965",
|
||||
},
|
||||
content_type="application/json"
|
||||
)
|
||||
|
||||
# Then
|
||||
assert resp.status_code == 200
|
||||
assert resp.data["title"] == "Dune"
|
||||
assert resp.data["genre"] == "Ciencia Ficción"
|
||||
assert resp.data["author"] == "Frank Herbert"
|
||||
assert resp.data["year"] == "1965"
|
||||
|
||||
resp_detail = client.get(f"/api/libros/{libro.id}/")
|
||||
assert resp_detail.status_code == 200
|
||||
assert resp_detail.data["title"] == "Dune"
|
||||
assert resp_detail.data["genre"] == "Ciencia Ficción"
|
||||
assert resp_detail.data["author"] == "Frank Herbert"
|
||||
assert resp_detail.data["year"] == "1965"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_update_book_incorrect_id(client):
|
||||
resp = client.put(f"/api/libros/-1/")
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_update_book_invalid_json(client):
|
||||
# Given
|
||||
libro = Libros.objects.create(
|
||||
title="El fin de la eternidad",
|
||||
genre="Ciencia Ficción",
|
||||
author="Isaac Asimov",
|
||||
year="1955",
|
||||
)
|
||||
|
||||
# When
|
||||
resp = client.put(
|
||||
f"/api/libros/{libro.id}/",
|
||||
{
|
||||
"foo": "Dune",
|
||||
"boo": "Ciencia Ficción",
|
||||
},
|
||||
content_type="application/json"
|
||||
)
|
||||
|
||||
# Then
|
||||
assert resp.status_code == 400
|
Loading…
Reference in New Issue
Block a user