Moved the logic for getting linked data into the relationship router and

added tests for authors.
This commit is contained in:
2025-06-24 19:46:27 +03:00
parent fd9e878134
commit e6af796d7d
8 changed files with 134 additions and 58 deletions

View File

@@ -10,3 +10,57 @@ from tests.test_misc import setup_database
client = TestClient(app)
#TODO: add tests for author endpoints
def test_empty_list_authors(setup_database):
response = client.get("/authors")
print(response.json())
assert response.status_code == 200, "Invalid response status"
assert response.json() == {"authors": [], "total": 0}, "Invalid response data"
def test_create_author(setup_database):
response = client.post("/authors", json={"name": "Test Author"})
print(response.json())
assert response.status_code == 200, "Invalid response status"
assert response.json() == {"id": 1, "name": "Test Author"}, "Invalid response data"
def test_list_authors(setup_database):
response = client.get("/authors")
print(response.json())
assert response.status_code == 200, "Invalid response status"
assert response.json() == {"authors": [{"id": 1, "name": "Test Author"}], "total": 1}, "Invalid response data"
def test_get_existing_author(setup_database):
response = client.get("/authors/1")
print(response.json())
assert response.status_code == 200, "Invalid response status"
assert response.json() == {"id": 1, "name": "Test Author"}, "Invalid response data"
def test_get_not_existing_author(setup_database):
response = client.get("/authors/2")
print(response.json())
assert response.status_code == 404, "Invalid response status"
assert response.json() == {"detail": "Author not found"}, "Invalid response data"
def test_update_author(setup_database):
response = client.get("/authors/1")
assert response.status_code == 200, "Invalid response status"
response = client.put("/authors/1", json={"name": "Updated Author"})
assert response.status_code == 200, "Invalid response status"
assert response.json() == {"id": 1, "name": "Updated Author"}, "Invalid response data"
def test_update_not_existing_author(setup_database):
response = client.put("/authors/2", json={"name": "Updated Author"})
assert response.status_code == 404, "Invalid response status"
assert response.json() == {"detail": "Author not found"}, "Invalid response data"
def test_delete_author(setup_database):
response = client.get("/authors/1")
assert response.status_code == 200, "Invalid response status"
response = client.delete("/authors/1")
assert response.status_code == 200, "Invalid response status"
assert response.json() == {"id": 1, "name": "Updated Author"}, "Invalid response data"
def test_not_existing_delete_author(setup_database):
response = client.delete("/authors/2")
assert response.status_code == 404, "Invalid response status"
assert response.json() == {"detail": "Author not found"}, "Invalid response data"

View File

@@ -13,46 +13,56 @@ client = TestClient(app)
#TODO: add comments
#TODO: update tests
def test_empty_list_books(setup_database):
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"})
print(response.json())
assert response.status_code == 200
assert response.json() == {"id": 1, "title": "Test Book", "description": "Test Description"}
assert response.status_code == 200, "Invalid response status"
assert response.json() == {"id": 1, "title": "Test Book", "description": "Test Description"}, "Invalid response data"
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"
def test_get_existing_book(setup_database):
response = client.get("/books/1")
print(response.json())
assert response.status_code == 200
assert response.json() == {"id": 1, "title": "Test Book", "description": "Test Description", 'authors': []}
assert response.status_code == 200, "Invalid response status"
assert response.json() == {"id": 1, "title": "Test Book", "description": "Test Description", 'authors': []}, "Invalid response data"
def test_get_not_existing_book(setup_database):
response = client.get("/books/2")
print(response.json())
assert response.status_code == 404
assert response.json() == {"detail": "Book not found"}
assert response.status_code == 404, "Invalid response status"
assert response.json() == {"detail": "Book not found"}, "Invalid response data"
def test_update_book(setup_database):
response = client.get("/books/1")
assert response.status_code == 200
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
assert response.json() == {"id": 1, "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"})
assert response.status_code == 404
assert response.json() == {"detail": "Book not found"}
assert response.status_code == 404, "Invalid response status"
assert response.json() == {"detail": "Book not found"}, "Invalid response data"
def test_delete_book(setup_database):
response = client.get("/books/1")
assert response.status_code == 200
assert response.status_code == 200, "Invalid response status"
response = client.delete("/books/1")
assert response.status_code == 200
assert response.json() == {"id": 1, "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_not_existing_delete_book(setup_database):
response = client.delete("/books/2")
assert response.status_code == 404
assert response.json() == {"detail": "Book not found"}
#TODO: add tests for other books endpoints
assert response.status_code == 404, "Invalid response status"
assert response.json() == {"detail": "Book not found"}, "Invalid response data"

View File

@@ -1,6 +1,7 @@
import pytest
from alembic import command
from alembic.config import Config
from datetime import datetime
from fastapi.testclient import TestClient
from sqlmodel import select, delete, Session