Implement tests for all endpoints and update documentation. Added

endpoint for getting the list of all relationships
This commit is contained in:
2025-06-24 21:28:26 +03:00
parent e6af796d7d
commit 5db6416d79
5 changed files with 80 additions and 6 deletions

View File

@@ -78,6 +78,7 @@ For run tests:
**Relationships** **Relationships**
| Method | Endpoint | Description | | Method | Endpoint | Description |
|--------|-----------------------|------------------------------------------------| |--------|-----------------------|------------------------------------------------|
| GET | `/relationships` | Retrieve a list of all relationships |
| POST | `/relationships` | Add author-book relationship | | POST | `/relationships` | Add author-book relationship |
| DELETE | `/relationships` | Remove author-book relationship | | DELETE | `/relationships` | Remove author-book relationship |

View File

@@ -23,7 +23,7 @@ services:
tests: tests:
container_name: tests container_name: tests
build: . build: .
command: bash -c "pytest tests/test_authors.py" command: bash -c "pytest tests"
volumes: volumes:
- .:/code - .:/code
depends_on: depends_on:

View File

@@ -6,10 +6,10 @@ from library_service.settings import get_session
from library_service.models.db import Book, Author, AuthorBookLink from library_service.models.db import Book, Author, AuthorBookLink
from library_service.models.dto import AuthorRead, BookRead from library_service.models.dto import AuthorRead, BookRead
router = APIRouter(prefix="/relationships", tags=["relations"]) router = APIRouter(tags=["relations"])
# Add author to book # Add author to book
@router.post("/", response_model=AuthorBookLink) @router.post("/relationships", response_model=AuthorBookLink)
def add_author_to_book(author_id: int, book_id: int, session: Session = Depends(get_session)): def add_author_to_book(author_id: int, book_id: int, session: Session = Depends(get_session)):
author = session.get(Author, author_id) author = session.get(Author, author_id)
if not author: if not author:
@@ -35,7 +35,7 @@ def add_author_to_book(author_id: int, book_id: int, session: Session = Depends(
return link return link
# Remove author from book # Remove author from book
@router.delete("/", response_model=Dict[str, str]) @router.delete("/relationships", response_model=Dict[str, str])
def remove_author_from_book(author_id: int, book_id: int, session: Session = Depends(get_session)): def remove_author_from_book(author_id: int, book_id: int, session: Session = Depends(get_session)):
link = session.exec( link = session.exec(
select(AuthorBookLink) select(AuthorBookLink)
@@ -50,6 +50,12 @@ def remove_author_from_book(author_id: int, book_id: int, session: Session = Dep
session.commit() session.commit()
return {"message": "Relationship removed successfully"} return {"message": "Relationship removed successfully"}
# Get relationships
@router.get("/relationships", response_model=List[AuthorBookLink])
def get_relationships(session: Session = Depends(get_session)):
relationships = session.exec(select(AuthorBookLink)).all()
return relationships
# Get author's books # Get author's books
@router.get("/authors/{author_id}/books/", response_model=List[BookRead]) @router.get("/authors/{author_id}/books/", response_model=List[BookRead])
def get_books_for_author(author_id: int, session: Session = Depends(get_session)): def get_books_for_author(author_id: int, session: Session = Depends(get_session)):

View File

@@ -33,7 +33,7 @@ def test_get_existing_author(setup_database):
response = client.get("/authors/1") response = client.get("/authors/1")
print(response.json()) print(response.json())
assert response.status_code == 200, "Invalid response status" assert response.status_code == 200, "Invalid response status"
assert response.json() == {"id": 1, "name": "Test Author"}, "Invalid response data" assert response.json() == {"id": 1, "name": "Test Author", "books": []}, "Invalid response data"
def test_get_not_existing_author(setup_database): def test_get_not_existing_author(setup_database):
response = client.get("/authors/2") response = client.get("/authors/2")

View File

@@ -9,4 +9,71 @@ from tests.test_misc import setup_database
client = TestClient(app) client = TestClient(app)
#TODO: add tests for relationships endpoints def make_relationship(author_id, book_id):
response = client.post("/relationships", params={"author_id": author_id, "book_id": book_id})
assert response.status_code == 200, "Invalid response status"
def test_prepare_data(setup_database):
response = client.post("/books", json={"title": "Test Book 1", "description": "Test Description 1"})
response = client.post("/books", json={"title": "Test Book 2", "description": "Test Description 2"})
response = client.post("/books", json={"title": "Test Book 3", "description": "Test Description 3"})
response = client.post("/authors", json={"name": "Test Author 1"})
response = client.post("/authors", json={"name": "Test Author 2"})
response = client.post("/authors", json={"name": "Test Author 3"})
make_relationship(1, 1)
make_relationship(2, 1)
make_relationship(1, 2)
make_relationship(2, 3)
make_relationship(3, 3)
response = client.get("/relationships")
assert response.status_code == 200, "Invalid response status"
assert len(response.json()) == 5, "Invalid number of relationships"
def test_get_book_authors():
response1 = client.get("/books/1/authors")
assert response1.status_code == 200, "Invalid response status"
assert len(response1.json()) == 2, "Invalid number of authors"
assert response1.json()[0]["name"] == "Test Author 1"
assert response1.json()[1]["name"] == "Test Author 2"
assert response1.json()[0]["id"] == 1
assert response1.json()[1]["id"] == 2
response2 = client.get("/books/2/authors")
assert response2.status_code == 200, "Invalid response status"
assert len(response2.json()) == 1, "Invalid number of authors"
assert response2.json()[0]["name"] == "Test Author 1"
assert response2.json()[0]["id"] == 1
response3 = client.get("/books/3/authors")
assert response3.status_code == 200, "Invalid response status"
assert len(response3.json()) == 2, "Invalid number of authors"
assert response3.json()[0]["name"] == "Test Author 2"
assert response3.json()[1]["name"] == "Test Author 3"
assert response3.json()[0]["id"] == 2
assert response3.json()[1]["id"] == 3
def test_get_author_books():
response1 = client.get("/authors/1/books")
assert response1.status_code == 200, "Invalid response status"
assert len(response1.json()) == 2, "Invalid number of books"
assert response1.json()[0]["title"] == "Test Book 1"
assert response1.json()[1]["title"] == "Test Book 2"
assert response1.json()[0]["id"] == 1
assert response1.json()[1]["id"] == 2
response2 = client.get("/authors/2/books")
assert response2.status_code == 200, "Invalid response status"
assert len(response2.json()) == 2, "Invalid number of books"
assert response2.json()[0]["title"] == "Test Book 1"
assert response2.json()[1]["title"] == "Test Book 3"
assert response2.json()[0]["id"] == 1
assert response2.json()[1]["id"] == 3
response3 = client.get("/authors/3/books")
assert response3.status_code == 200, "Invalid response status"
assert len(response3.json()) == 1, "Invalid number of books"
assert response3.json()[0]["title"] == "Test Book 3"
assert response3.json()[0]["id"] == 3