mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
Форматирование кода, добавление лого, исправление тестов, улучшение эндпоинтов и документации
This commit is contained in:
@@ -1,68 +1,130 @@
|
||||
import pytest
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlmodel import select, delete, Session
|
||||
from tests.mock_app import mock_app
|
||||
from tests.mocks.mock_storage import mock_storage
|
||||
|
||||
from library_service.main import app
|
||||
from tests.test_misc import setup_database
|
||||
client = TestClient(mock_app)
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
#TODO: assert descriptions
|
||||
#TODO: add comments
|
||||
#TODO: update tests
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_database():
|
||||
"""Clear mock storage before each test"""
|
||||
mock_storage.clear_all()
|
||||
yield
|
||||
mock_storage.clear_all()
|
||||
|
||||
def test_empty_list_books(setup_database):
|
||||
|
||||
def test_empty_list_books():
|
||||
response = client.get("/books")
|
||||
print(response.json())
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert response.json() == {"books": [], "total": 0}, "Invalid response data"
|
||||
|
||||
def test_create_book(setup_database):
|
||||
response = client.post("/books", json={"title": "Test Book", "description": "Test Description"})
|
||||
|
||||
def test_create_book():
|
||||
response = client.post(
|
||||
"/books", json={"title": "Test Book", "description": "Test Description"}
|
||||
)
|
||||
print(response.json())
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert response.json() == {"id": 1, "title": "Test Book", "description": "Test Description"}, "Invalid response data"
|
||||
assert response.json() == {
|
||||
"id": 1,
|
||||
"title": "Test Book",
|
||||
"description": "Test Description",
|
||||
}, "Invalid response data"
|
||||
|
||||
|
||||
def test_list_books():
|
||||
# First create a book
|
||||
client.post(
|
||||
"/books", json={"title": "Test Book", "description": "Test Description"}
|
||||
)
|
||||
|
||||
def test_list_books(setup_database):
|
||||
response = client.get("/books")
|
||||
print(response.json())
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert response.json() == {"books": [{"id": 1, "title": "Test Book", "description": "Test Description"}], "total": 1}, "Invalid response data"
|
||||
assert response.json() == {
|
||||
"books": [{"id": 1, "title": "Test Book", "description": "Test Description"}],
|
||||
"total": 1,
|
||||
}, "Invalid response data"
|
||||
|
||||
|
||||
def test_get_existing_book():
|
||||
# First create a book
|
||||
client.post(
|
||||
"/books", json={"title": "Test Book", "description": "Test Description"}
|
||||
)
|
||||
|
||||
def test_get_existing_book(setup_database):
|
||||
response = client.get("/books/1")
|
||||
print(response.json())
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert response.json() == {"id": 1, "title": "Test Book", "description": "Test Description", 'authors': []}, "Invalid response data"
|
||||
assert response.json() == {
|
||||
"id": 1,
|
||||
"title": "Test Book",
|
||||
"description": "Test Description",
|
||||
"authors": [],
|
||||
}, "Invalid response data"
|
||||
|
||||
def test_get_not_existing_book(setup_database):
|
||||
|
||||
def test_get_not_existing_book():
|
||||
response = client.get("/books/2")
|
||||
print(response.json())
|
||||
assert response.status_code == 404, "Invalid response status"
|
||||
assert response.json() == {"detail": "Book not found"}, "Invalid response data"
|
||||
|
||||
def test_update_book(setup_database):
|
||||
|
||||
def test_update_book():
|
||||
# First create a book
|
||||
client.post(
|
||||
"/books", json={"title": "Test Book", "description": "Test Description"}
|
||||
)
|
||||
|
||||
response = client.get("/books/1")
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
response = client.put("/books/1", json={"title": "Updated Book", "description": "Updated Description"})
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert response.json() == {"id": 1, "title": "Updated Book", "description": "Updated Description"}, "Invalid response data"
|
||||
|
||||
def test_update_not_existing_book(setup_database):
|
||||
response = client.put("/books/2", json={"title": "Updated Book", "description": "Updated Description"})
|
||||
response = client.put(
|
||||
"/books/1", json={"title": "Updated Book", "description": "Updated Description"}
|
||||
)
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert response.json() == {
|
||||
"id": 1,
|
||||
"title": "Updated Book",
|
||||
"description": "Updated Description",
|
||||
}, "Invalid response data"
|
||||
|
||||
|
||||
def test_update_not_existing_book():
|
||||
response = client.put(
|
||||
"/books/2", json={"title": "Updated Book", "description": "Updated Description"}
|
||||
)
|
||||
assert response.status_code == 404, "Invalid response status"
|
||||
assert response.json() == {"detail": "Book not found"}, "Invalid response data"
|
||||
|
||||
def test_delete_book(setup_database):
|
||||
|
||||
def test_delete_book():
|
||||
# First create a book
|
||||
client.post(
|
||||
"/books", json={"title": "Test Book", "description": "Test Description"}
|
||||
)
|
||||
|
||||
# Update it first
|
||||
client.put(
|
||||
"/books/1", json={"title": "Updated Book", "description": "Updated Description"}
|
||||
)
|
||||
|
||||
response = client.get("/books/1")
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
|
||||
response = client.delete("/books/1")
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert response.json() == {"id": 1, "title": "Updated Book", "description": "Updated Description"}, "Invalid response data"
|
||||
assert response.json() == {
|
||||
"id": 1,
|
||||
"title": "Updated Book",
|
||||
"description": "Updated Description",
|
||||
}, "Invalid response data"
|
||||
|
||||
def test_not_existing_delete_book(setup_database):
|
||||
|
||||
def test_not_existing_delete_book():
|
||||
response = client.delete("/books/2")
|
||||
assert response.status_code == 404, "Invalid response status"
|
||||
assert response.json() == {"detail": "Book not found"}, "Invalid response data"
|
||||
|
||||
Reference in New Issue
Block a user