From 5aca9b9725dcc06546847f80f9d305cb5dce09c1 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Tue, 13 Jul 2021 19:37:12 +0200 Subject: [PATCH] Add reverse urls --- app/libros/urls.py | 4 +-- app/libros/views.py | 4 ++- .../{test_create_model.py => test_model.py} | 2 +- tests/libros/test_views.py | 27 ++++++++++--------- 4 files changed, 20 insertions(+), 17 deletions(-) rename tests/libros/{test_create_model.py => test_model.py} (94%) diff --git a/app/libros/urls.py b/app/libros/urls.py index d879fda..d1bba4d 100644 --- a/app/libros/urls.py +++ b/app/libros/urls.py @@ -6,6 +6,6 @@ from app.libros.views import * urlpatterns = [ path("ping/", ping, name="ping"), - path("api/libros/", LibrosList.as_view()), - path("api/libros//", LibrosDetails.as_view()), + path("api/libros/", LibrosList.as_view(), name="libros_list"), + path("api/libros//", LibrosDetails.as_view(), name="libros_details"), ] diff --git a/app/libros/views.py b/app/libros/views.py index b1771b7..2b615ad 100644 --- a/app/libros/views.py +++ b/app/libros/views.py @@ -38,9 +38,11 @@ class LibrosDetails(APIView): def put(self, request, pk): libro = Libros.objects.filter(pk=pk).first() serializer = LibroSerializer(libro, data=request.data) - if libro and serializer.is_valid(): + if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) + elif not libro: + return Response(serializer.data, status=status.HTTP_404_NOT_FOUND) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk): diff --git a/tests/libros/test_create_model.py b/tests/libros/test_model.py similarity index 94% rename from tests/libros/test_create_model.py rename to tests/libros/test_model.py index 9f9afe9..79ce29f 100644 --- a/tests/libros/test_create_model.py +++ b/tests/libros/test_model.py @@ -1,4 +1,4 @@ -# tests/libros/test_create_model.py +# tests/libros/test_model.py import pytest diff --git a/tests/libros/test_views.py b/tests/libros/test_views.py index fd79d20..88d432b 100644 --- a/tests/libros/test_views.py +++ b/tests/libros/test_views.py @@ -2,6 +2,7 @@ import pytest from app.libros.models import Libros +from django.urls import reverse @pytest.mark.django_db @@ -13,7 +14,7 @@ def test_add_book(client): # When resp = client.post( - "/api/libros/", + reverse("libros_list"), { "title": "El fin de la eternidad", "genre": "Ciencia Ficción", @@ -43,7 +44,7 @@ def test_get_single_book(client): ) # When - resp = client.get(f"/api/libros/{libro.id}/") + resp = client.get(reverse("libros_details", kwargs={"pk": libro.id})) # Then assert resp.status_code == 200 @@ -54,7 +55,7 @@ def test_get_single_book(client): def test_get_single_book_incorrect_id(client): # When - resp = client.get(f"/api/libros/-1/") + resp = client.get(reverse("libros_details", kwargs={"pk": 99})) # Then assert resp.status_code == 404 @@ -76,7 +77,7 @@ def test_get_all_books(client, faker): libro_2 = create_random_book() # When - resp = client.get(f"/api/libros/") + resp = client.get(reverse("libros_list")) # Then assert resp.status_code == 200 @@ -95,14 +96,14 @@ def test_remove_book(client): year="1955", ) ## Check exist - resp_detail = client.get(f"/api/libros/{libro.id}/") + resp_detail = client.get(reverse("libros_details", kwargs={"pk": 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}/") + resp_delete = client.delete(reverse("libros_details", kwargs={"pk": libro.id})) + resp_list = client.get(reverse("libros_list")) + rest_new_detail = client.get(reverse("libros_details", kwargs={"pk": libro.id})) # Then ## Check status delete @@ -128,7 +129,7 @@ def test_remove_book_incorrect_id(client): ) # When - resp = client.delete(f"/api/movies/-1/") + resp = client.delete(reverse("libros_details", kwargs={"pk": 99})) # Then assert resp.status_code == 404 @@ -147,7 +148,7 @@ def test_update_book(client): # When resp = client.put( - f"/api/libros/{libro.id}/", + reverse("libros_details", kwargs={"pk": libro.id}), { "title": "Dune", "genre": "Ciencia Ficción", @@ -164,7 +165,7 @@ def test_update_book(client): assert resp.data["author"] == "Frank Herbert" assert resp.data["year"] == "1965" - resp_detail = client.get(f"/api/libros/{libro.id}/") + resp_detail = client.get(reverse("libros_details", kwargs={"pk": libro.id})) assert resp_detail.status_code == 200 assert resp_detail.data["title"] == "Dune" assert resp_detail.data["genre"] == "Ciencia Ficción" @@ -174,7 +175,7 @@ def test_update_book(client): @pytest.mark.django_db def test_update_book_incorrect_id(client): - resp = client.put(f"/api/libros/-1/") + resp = client.put(reverse("libros_details", kwargs={"pk": 99})) assert resp.status_code == 404 @@ -190,7 +191,7 @@ def test_update_book_invalid_json(client): # When resp = client.put( - f"/api/libros/{libro.id}/", + reverse("libros_details", kwargs={"pk": libro.id}), { "foo": "Dune", "boo": "Ciencia Ficción",