Add reverse urls

This commit is contained in:
Andros Fenollosa 2021-07-13 19:37:12 +02:00
parent 204556f495
commit 5aca9b9725
4 changed files with 20 additions and 17 deletions

View File

@ -6,6 +6,6 @@ from app.libros.views import *
urlpatterns = [ urlpatterns = [
path("ping/", ping, name="ping"), path("ping/", ping, name="ping"),
path("api/libros/", LibrosList.as_view()), path("api/libros/", LibrosList.as_view(), name="libros_list"),
path("api/libros/<int:pk>/", LibrosDetails.as_view()), path("api/libros/<int:pk>/", LibrosDetails.as_view(), name="libros_details"),
] ]

View File

@ -38,9 +38,11 @@ class LibrosDetails(APIView):
def put(self, request, pk): def put(self, request, pk):
libro = Libros.objects.filter(pk=pk).first() libro = Libros.objects.filter(pk=pk).first()
serializer = LibroSerializer(libro, data=request.data) serializer = LibroSerializer(libro, data=request.data)
if libro and serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK) 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) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk): def delete(self, request, pk):

View File

@ -1,4 +1,4 @@
# tests/libros/test_create_model.py # tests/libros/test_model.py
import pytest import pytest

View File

@ -2,6 +2,7 @@
import pytest import pytest
from app.libros.models import Libros from app.libros.models import Libros
from django.urls import reverse
@pytest.mark.django_db @pytest.mark.django_db
@ -13,7 +14,7 @@ def test_add_book(client):
# When # When
resp = client.post( resp = client.post(
"/api/libros/", reverse("libros_list"),
{ {
"title": "El fin de la eternidad", "title": "El fin de la eternidad",
"genre": "Ciencia Ficción", "genre": "Ciencia Ficción",
@ -43,7 +44,7 @@ def test_get_single_book(client):
) )
# When # When
resp = client.get(f"/api/libros/{libro.id}/") resp = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
# Then # Then
assert resp.status_code == 200 assert resp.status_code == 200
@ -54,7 +55,7 @@ def test_get_single_book(client):
def test_get_single_book_incorrect_id(client): def test_get_single_book_incorrect_id(client):
# When # When
resp = client.get(f"/api/libros/-1/") resp = client.get(reverse("libros_details", kwargs={"pk": 99}))
# Then # Then
assert resp.status_code == 404 assert resp.status_code == 404
@ -76,7 +77,7 @@ def test_get_all_books(client, faker):
libro_2 = create_random_book() libro_2 = create_random_book()
# When # When
resp = client.get(f"/api/libros/") resp = client.get(reverse("libros_list"))
# Then # Then
assert resp.status_code == 200 assert resp.status_code == 200
@ -95,14 +96,14 @@ def test_remove_book(client):
year="1955", year="1955",
) )
## Check exist ## 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.status_code == 200
assert resp_detail.data["title"] == "El fin de la eternidad" assert resp_detail.data["title"] == "El fin de la eternidad"
# When # When
resp_delete = client.delete(f"/api/libros/{libro.id}/") resp_delete = client.delete(reverse("libros_details", kwargs={"pk": libro.id}))
resp_list = client.get("/api/libros/") resp_list = client.get(reverse("libros_list"))
rest_new_detail = client.get(f"/api/libros/{libro.id}/") rest_new_detail = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
# Then # Then
## Check status delete ## Check status delete
@ -128,7 +129,7 @@ def test_remove_book_incorrect_id(client):
) )
# When # When
resp = client.delete(f"/api/movies/-1/") resp = client.delete(reverse("libros_details", kwargs={"pk": 99}))
# Then # Then
assert resp.status_code == 404 assert resp.status_code == 404
@ -147,7 +148,7 @@ def test_update_book(client):
# When # When
resp = client.put( resp = client.put(
f"/api/libros/{libro.id}/", reverse("libros_details", kwargs={"pk": libro.id}),
{ {
"title": "Dune", "title": "Dune",
"genre": "Ciencia Ficción", "genre": "Ciencia Ficción",
@ -164,7 +165,7 @@ def test_update_book(client):
assert resp.data["author"] == "Frank Herbert" assert resp.data["author"] == "Frank Herbert"
assert resp.data["year"] == "1965" 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.status_code == 200
assert resp_detail.data["title"] == "Dune" assert resp_detail.data["title"] == "Dune"
assert resp_detail.data["genre"] == "Ciencia Ficción" assert resp_detail.data["genre"] == "Ciencia Ficción"
@ -174,7 +175,7 @@ def test_update_book(client):
@pytest.mark.django_db @pytest.mark.django_db
def test_update_book_incorrect_id(client): 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 assert resp.status_code == 404
@ -190,7 +191,7 @@ def test_update_book_invalid_json(client):
# When # When
resp = client.put( resp = client.put(
f"/api/libros/{libro.id}/", reverse("libros_details", kwargs={"pk": libro.id}),
{ {
"foo": "Dune", "foo": "Dune",
"boo": "Ciencia Ficción", "boo": "Ciencia Ficción",