2021-07-14 16:19:03 +02:00
|
|
|
# tests/Library/test_views.py
|
2021-07-12 07:01:26 +02:00
|
|
|
|
|
|
|
import pytest
|
2021-07-14 16:19:03 +02:00
|
|
|
from app.library.models import Book
|
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
|
2021-07-14 16:19:03 +02:00
|
|
|
book = Book.objects.all()
|
|
|
|
assert len(book) == 0
|
2021-07-12 07:01:26 +02:00
|
|
|
|
|
|
|
# When
|
|
|
|
resp = client.post(
|
2021-07-14 16:19:03 +02:00
|
|
|
reverse("book_list"),
|
2021-07-12 07:01:26 +02:00
|
|
|
{
|
2021-07-14 16:19:03 +02:00
|
|
|
"title": "The End of Eternity",
|
2021-07-14 16:46:34 +02:00
|
|
|
"country": "eeuu",
|
2021-07-12 07:01:26 +02:00
|
|
|
"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
|
2021-07-14 16:19:03 +02:00
|
|
|
assert resp.data["title"] == "The End of Eternity"
|
2021-07-12 07:01:26 +02:00
|
|
|
|
2021-07-14 16:19:03 +02:00
|
|
|
book = Book.objects.all()
|
|
|
|
assert len(book) == 1
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_get_single_book(client):
|
|
|
|
|
|
|
|
# Given
|
2021-07-14 16:19:03 +02:00
|
|
|
book = Book.objects.create(
|
|
|
|
title="The End of Eternity",
|
2021-07-14 16:46:34 +02:00
|
|
|
country="eeuu",
|
2021-07-12 23:47:31 +02:00
|
|
|
author="Isaac Asimov",
|
|
|
|
year="1955",
|
2021-07-13 18:30:31 +02:00
|
|
|
)
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
# When
|
2021-07-14 16:19:03 +02:00
|
|
|
resp = client.get(reverse("book_details", kwargs={"pk": book.id}))
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
# Then
|
|
|
|
assert resp.status_code == 200
|
2021-07-14 16:19:03 +02:00
|
|
|
assert resp.data["title"] == "The End of Eternity"
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_get_single_book_incorrect_id(client):
|
|
|
|
|
|
|
|
# When
|
2021-07-14 16:19:03 +02:00
|
|
|
resp = client.get(reverse("book_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():
|
2021-07-14 16:19:03 +02:00
|
|
|
return Book.objects.create(
|
2021-07-12 23:47:31 +02:00
|
|
|
title=faker.name(),
|
2021-07-14 16:46:34 +02:00
|
|
|
country=faker.country(),
|
2021-07-12 23:47:31 +02:00
|
|
|
author=faker.name_nonbinary(),
|
|
|
|
year=faker.year(),
|
|
|
|
)
|
|
|
|
|
2021-07-14 16:19:03 +02:00
|
|
|
book_1 = create_random_book()
|
|
|
|
book_2 = create_random_book()
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
# When
|
2021-07-14 16:19:03 +02:00
|
|
|
resp = client.get(reverse("book_list"))
|
2021-07-12 23:47:31 +02:00
|
|
|
|
|
|
|
# Then
|
|
|
|
assert resp.status_code == 200
|
2021-07-14 16:19:03 +02:00
|
|
|
assert resp.data[0]["title"] == book_1.title
|
|
|
|
assert resp.data[1]["title"] == book_2.title
|
2021-07-12 23:47:31 +02:00
|
|
|
|
2021-07-13 18:18:18 +02:00
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_remove_book(client):
|
|
|
|
|
|
|
|
# Given
|
2021-07-14 16:19:03 +02:00
|
|
|
book = Book.objects.create(
|
|
|
|
title="The End of Eternity",
|
2021-07-14 16:46:34 +02:00
|
|
|
country="eeuu",
|
2021-07-13 18:18:18 +02:00
|
|
|
author="Isaac Asimov",
|
|
|
|
year="1955",
|
|
|
|
)
|
|
|
|
## Check exist
|
2021-07-14 16:19:03 +02:00
|
|
|
resp_detail = client.get(reverse("book_details", kwargs={"pk": book.id}))
|
2021-07-13 18:18:18 +02:00
|
|
|
assert resp_detail.status_code == 200
|
2021-07-14 16:19:03 +02:00
|
|
|
assert resp_detail.data["title"] == "The End of Eternity"
|
2021-07-13 18:18:18 +02:00
|
|
|
|
|
|
|
# When
|
2021-07-14 16:19:03 +02:00
|
|
|
resp_delete = client.delete(reverse("book_details", kwargs={"pk": book.id}))
|
|
|
|
resp_list = client.get(reverse("book_list"))
|
|
|
|
rest_new_detail = client.get(reverse("book_details", kwargs={"pk": book.id}))
|
2021-07-13 18:18:18 +02:00
|
|
|
|
|
|
|
# Then
|
|
|
|
## Check status delete
|
|
|
|
assert resp_delete.status_code == 200
|
|
|
|
## Check return delete
|
2021-07-14 16:19:03 +02:00
|
|
|
assert resp_delete.data["title"] == "The End of Eternity"
|
2021-07-13 18:18:18 +02:00
|
|
|
## 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
|
2021-07-14 16:19:03 +02:00
|
|
|
book = Book.objects.create(
|
|
|
|
title="The End of Eternity",
|
2021-07-14 16:46:34 +02:00
|
|
|
country="eeuu",
|
2021-07-13 18:18:18 +02:00
|
|
|
author="Isaac Asimov",
|
|
|
|
year="1955",
|
|
|
|
)
|
|
|
|
|
|
|
|
# When
|
2021-07-14 16:19:03 +02:00
|
|
|
resp = client.delete(reverse("book_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
|
2021-07-14 16:19:03 +02:00
|
|
|
book = Book.objects.create(
|
|
|
|
title="The End of Eternity",
|
2021-07-14 16:46:34 +02:00
|
|
|
country="eeuu",
|
2021-07-13 18:18:18 +02:00
|
|
|
author="Isaac Asimov",
|
|
|
|
year="1955",
|
|
|
|
)
|
|
|
|
|
|
|
|
# When
|
|
|
|
resp = client.put(
|
2021-07-14 16:19:03 +02:00
|
|
|
reverse("book_details", kwargs={"pk": book.id}),
|
2021-07-13 18:18:18 +02:00
|
|
|
{
|
|
|
|
"title": "Dune",
|
2021-07-14 16:46:34 +02:00
|
|
|
"country": "eeuu",
|
2021-07-13 18:18:18 +02:00
|
|
|
"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"
|
2021-07-14 16:46:34 +02:00
|
|
|
assert resp.data["country"] == "eeuu"
|
2021-07-13 18:18:18 +02:00
|
|
|
assert resp.data["author"] == "Frank Herbert"
|
|
|
|
assert resp.data["year"] == "1965"
|
|
|
|
|
2021-07-14 16:19:03 +02:00
|
|
|
resp_detail = client.get(reverse("book_details", kwargs={"pk": book.id}))
|
2021-07-13 18:18:18 +02:00
|
|
|
assert resp_detail.status_code == 200
|
|
|
|
assert resp_detail.data["title"] == "Dune"
|
2021-07-14 16:46:34 +02:00
|
|
|
assert resp_detail.data["country"] == "eeuu"
|
2021-07-13 18:18:18 +02:00
|
|
|
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-14 16:19:03 +02:00
|
|
|
resp = client.put(reverse("book_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
|
2021-07-14 16:19:03 +02:00
|
|
|
book = Book.objects.create(
|
|
|
|
title="The End of Eternity",
|
2021-07-14 16:46:34 +02:00
|
|
|
country="eeuu",
|
2021-07-13 18:18:18 +02:00
|
|
|
author="Isaac Asimov",
|
|
|
|
year="1955",
|
|
|
|
)
|
|
|
|
|
|
|
|
# When
|
|
|
|
resp = client.put(
|
2021-07-14 16:19:03 +02:00
|
|
|
reverse("book_details", kwargs={"pk": book.id}),
|
2021-07-13 18:18:18 +02:00
|
|
|
{
|
|
|
|
"foo": "Dune",
|
2021-07-14 16:19:03 +02:00
|
|
|
"boo": "Sciencie Fiction",
|
2021-07-13 18:18:18 +02:00
|
|
|
},
|
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
|