2021-07-12 07:01:26 +02:00
|
|
|
# tests/libros/test_views.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from app.libros.models import Libros
|
2021-07-13 19:37:12 +02:00
|
|
|
from django.urls import reverse
|
2021-07-12 07:01:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2021-07-12 23:47:31 +02:00
|
|
|
def test_add_book(client):
|
|
|
|
|
2021-07-12 07:01:26 +02:00
|
|
|
# Given
|
|
|
|
libros = Libros.objects.all()
|
|
|
|
assert len(libros) == 0
|
|
|
|
|
|
|
|
# When
|
|
|
|
resp = client.post(
|
2021-07-13 19:37:12 +02:00
|
|
|
reverse("libros_list"),
|
2021-07-12 07:01:26 +02:00
|
|
|
{
|
|
|
|
"title": "El fin de la eternidad",
|
|
|
|
"genre": "Ciencia Ficción",
|
|
|
|
"author": "Isaac Asimov",
|
|
|
|
"year": "1955",
|
|
|
|
},
|
2021-07-13 18:30:31 +02:00
|
|
|
content_type="application/json",
|
2021-07-12 07:01:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Then
|
|
|
|
assert resp.status_code == 201
|
|
|
|
assert resp.data["title"] == "El fin de la eternidad"
|
|
|
|
|
|
|
|
libros = Libros.objects.all()
|
2021-07-12 23:47:31 +02:00
|
|
|
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",
|
2021-07-13 18:30:31 +02:00
|
|
|
)
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
# When
|
2021-07-13 19:37:12 +02:00
|
|
|
resp = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
# 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
|
2021-07-13 19:37:12 +02:00
|
|
|
resp = client.get(reverse("libros_details", kwargs={"pk": 99}))
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
# 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
|
2021-07-13 19:37:12 +02:00
|
|
|
resp = client.get(reverse("libros_list"))
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
# Then
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.data[0]["title"] == libro_1.title
|
|
|
|
assert resp.data[1]["title"] == libro_2.title
|
|
|
|
|
2021-07-13 18:18:18 +02:00
|
|
|
|
|
|
|
@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
|
2021-07-13 19:37:12 +02:00
|
|
|
resp_detail = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
|
2021-07-13 18:18:18 +02:00
|
|
|
assert resp_detail.status_code == 200
|
|
|
|
assert resp_detail.data["title"] == "El fin de la eternidad"
|
|
|
|
|
|
|
|
# When
|
2021-07-13 19:37:12 +02:00
|
|
|
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}))
|
2021-07-13 18:18:18 +02:00
|
|
|
|
|
|
|
# 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
|
2021-07-13 19:37:12 +02:00
|
|
|
resp = client.delete(reverse("libros_details", kwargs={"pk": 99}))
|
2021-07-13 18:18:18 +02:00
|
|
|
|
|
|
|
# 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(
|
2021-07-13 19:37:12 +02:00
|
|
|
reverse("libros_details", kwargs={"pk": libro.id}),
|
2021-07-13 18:18:18 +02:00
|
|
|
{
|
|
|
|
"title": "Dune",
|
|
|
|
"genre": "Ciencia Ficción",
|
|
|
|
"author": "Frank Herbert",
|
|
|
|
"year": "1965",
|
|
|
|
},
|
2021-07-13 18:30:31 +02:00
|
|
|
content_type="application/json",
|
2021-07-13 18:18:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
|
2021-07-13 19:37:12 +02:00
|
|
|
resp_detail = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
|
2021-07-13 18:18:18 +02:00
|
|
|
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):
|
2021-07-13 19:37:12 +02:00
|
|
|
resp = client.put(reverse("libros_details", kwargs={"pk": 99}))
|
2021-07-13 18:18:18 +02:00
|
|
|
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(
|
2021-07-13 19:37:12 +02:00
|
|
|
reverse("libros_details", kwargs={"pk": libro.id}),
|
2021-07-13 18:18:18 +02:00
|
|
|
{
|
|
|
|
"foo": "Dune",
|
|
|
|
"boo": "Ciencia Ficción",
|
|
|
|
},
|
2021-07-13 18:30:31 +02:00
|
|
|
content_type="application/json",
|
2021-07-13 18:18:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Then
|
2021-07-13 18:30:31 +02:00
|
|
|
assert resp.status_code == 400
|