Add list and detail
This commit is contained in:
parent
2e3f2029d3
commit
e2fb03e48c
@ -1,3 +0,0 @@
|
|||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
# Create your tests here.
|
|
@ -1,10 +1,11 @@
|
|||||||
# app/libros/urls.py
|
# app/libros/urls.py
|
||||||
|
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from app.libros.views import ping, LibrosList
|
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()),
|
||||||
]
|
path("api/libros/<int:pk>/", LibrosDetails.as_view()),
|
||||||
|
]
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
# app/libros/views.py
|
# app/libros/views.py
|
||||||
|
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from rest_framework.views import APIView # nuevo
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response # nuevo
|
from rest_framework.response import Response
|
||||||
from rest_framework import status # nuevo
|
from rest_framework import status
|
||||||
from .serializers import LibroSerializer # nuevo
|
from .serializers import LibroSerializer
|
||||||
|
from .models import Libros
|
||||||
|
|
||||||
|
|
||||||
def ping(request):
|
def ping(request):
|
||||||
@ -14,9 +15,25 @@ def ping(request):
|
|||||||
|
|
||||||
class LibrosList(APIView):
|
class LibrosList(APIView):
|
||||||
|
|
||||||
|
def get(self, request, format=None):
|
||||||
|
libros = Libros.objects.all().order_by('created_at')
|
||||||
|
serializer = LibroSerializer(libros, many=True)
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
serializer = LibroSerializer(data=request.data)
|
serializer = LibroSerializer(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
|
class LibrosDetails(APIView):
|
||||||
|
|
||||||
|
def get(self, request, pk, format=None):
|
||||||
|
libro = Libros.objects.filter(pk=pk).first()
|
||||||
|
serializer = LibroSerializer(libro)
|
||||||
|
if libro:
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
django
|
django
|
||||||
djangorestframework
|
djangorestframework
|
||||||
pytest-django
|
pytest-django
|
||||||
pytest
|
pytest
|
||||||
|
Faker
|
||||||
|
@ -28,4 +28,4 @@ def test_invalid_libro_serializer():
|
|||||||
assert serializer.errors == {
|
assert serializer.errors == {
|
||||||
"year": ["This field is required."],
|
"year": ["This field is required."],
|
||||||
"genre": ["This field is required."],
|
"genre": ["This field is required."],
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,8 @@ from app.libros.models import Libros
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_add_movie(client):
|
def test_add_book(client):
|
||||||
|
|
||||||
# Given
|
# Given
|
||||||
libros = Libros.objects.all()
|
libros = Libros.objects.all()
|
||||||
assert len(libros) == 0
|
assert len(libros) == 0
|
||||||
@ -27,4 +28,58 @@ def test_add_movie(client):
|
|||||||
assert resp.data["title"] == "El fin de la eternidad"
|
assert resp.data["title"] == "El fin de la eternidad"
|
||||||
|
|
||||||
libros = Libros.objects.all()
|
libros = Libros.objects.all()
|
||||||
assert len(libros) == 1
|
assert len(libros) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_get_single_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.get(f"/api/libros/{libro.id}/")
|
||||||
|
|
||||||
|
# Then
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert resp.data["title"] == "El fin de la eternidad"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_get_single_book_incorrect_id(client):
|
||||||
|
|
||||||
|
# When
|
||||||
|
resp = client.get(f"/api/libros/-1/")
|
||||||
|
|
||||||
|
# Then
|
||||||
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_get_all_books(client, faker):
|
||||||
|
|
||||||
|
# Given
|
||||||
|
def create_random_book():
|
||||||
|
return Libros.objects.create(
|
||||||
|
title=faker.name(),
|
||||||
|
genre=faker.job(),
|
||||||
|
author=faker.name_nonbinary(),
|
||||||
|
year=faker.year(),
|
||||||
|
)
|
||||||
|
|
||||||
|
libro_1 = create_random_book()
|
||||||
|
libro_2 = create_random_book()
|
||||||
|
|
||||||
|
# When
|
||||||
|
resp = client.get(f"/api/libros/")
|
||||||
|
|
||||||
|
# Then
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert resp.data[0]["title"] == libro_1.title
|
||||||
|
assert resp.data[1]["title"] == libro_2.title
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user