From 7fd71eca7f970061026d31c6fff80a1a7ae3d353 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Tue, 13 Jul 2021 18:18:18 +0200 Subject: [PATCH] Add delete and Add update methods --- app/libros/serializers.py | 4 +- app/libros/views.py | 25 ++++++-- tests/libros/test_views.py | 117 +++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 6 deletions(-) diff --git a/app/libros/serializers.py b/app/libros/serializers.py index 8933e14..d64b6d3 100644 --- a/app/libros/serializers.py +++ b/app/libros/serializers.py @@ -10,6 +10,6 @@ class LibroSerializer(serializers.ModelSerializer): fields = "__all__" read_only_fields = ( "id", - "created_date", - "updated_date", + "created_at", + "updated_at", ) diff --git a/app/libros/views.py b/app/libros/views.py index 125e994..ad927c9 100644 --- a/app/libros/views.py +++ b/app/libros/views.py @@ -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) diff --git a/tests/libros/test_views.py b/tests/libros/test_views.py index 945196a..f26a529 100644 --- a/tests/libros/test_views.py +++ b/tests/libros/test_views.py @@ -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 \ No newline at end of file