mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
Форматирование кода, добавление лого, исправление тестов, улучшение эндпоинтов и документации
This commit is contained in:
0
tests/mock_routers/__init__.py
Normal file
0
tests/mock_routers/__init__.py
Normal file
43
tests/mock_routers/authors.py
Normal file
43
tests/mock_routers/authors.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from tests.mocks.mock_storage import mock_storage
|
||||
|
||||
router = APIRouter(prefix="/authors", tags=["authors"])
|
||||
|
||||
|
||||
@router.post("/")
|
||||
def create_author(author: dict):
|
||||
return mock_storage.create_author(author["name"])
|
||||
|
||||
|
||||
@router.get("/")
|
||||
def read_authors():
|
||||
authors = mock_storage.get_all_authors()
|
||||
return {"authors": authors, "total": len(authors)}
|
||||
|
||||
|
||||
@router.get("/{author_id}")
|
||||
def get_author(author_id: int):
|
||||
author = mock_storage.get_author(author_id)
|
||||
if not author:
|
||||
raise HTTPException(status_code=404, detail="Author not found")
|
||||
|
||||
books = mock_storage.get_books_by_author(author_id)
|
||||
author_with_books = author.copy()
|
||||
author_with_books["books"] = books
|
||||
return author_with_books
|
||||
|
||||
|
||||
@router.put("/{author_id}")
|
||||
def update_author(author_id: int, author: dict):
|
||||
updated_author = mock_storage.update_author(author_id, author.get("name"))
|
||||
if not updated_author:
|
||||
raise HTTPException(status_code=404, detail="Author not found")
|
||||
return updated_author
|
||||
|
||||
|
||||
@router.delete("/{author_id}")
|
||||
def delete_author(author_id: int):
|
||||
author = mock_storage.delete_author(author_id)
|
||||
if not author:
|
||||
raise HTTPException(status_code=404, detail="Author not found")
|
||||
return author
|
||||
45
tests/mock_routers/books.py
Normal file
45
tests/mock_routers/books.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from tests.mocks.mock_storage import mock_storage
|
||||
|
||||
router = APIRouter(prefix="/books", tags=["books"])
|
||||
|
||||
|
||||
@router.post("/")
|
||||
def create_book(book: dict):
|
||||
return mock_storage.create_book(book["title"], book["description"])
|
||||
|
||||
|
||||
@router.get("/")
|
||||
def read_books():
|
||||
books = mock_storage.get_all_books()
|
||||
return {"books": books, "total": len(books)}
|
||||
|
||||
|
||||
@router.get("/{book_id}")
|
||||
def get_book(book_id: int):
|
||||
book = mock_storage.get_book(book_id)
|
||||
if not book:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
|
||||
authors = mock_storage.get_authors_by_book(book_id)
|
||||
book_with_authors = book.copy()
|
||||
book_with_authors["authors"] = authors
|
||||
return book_with_authors
|
||||
|
||||
|
||||
@router.put("/{book_id}")
|
||||
def update_book(book_id: int, book: dict):
|
||||
updated_book = mock_storage.update_book(
|
||||
book_id, book.get("title"), book.get("description")
|
||||
)
|
||||
if not updated_book:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
return updated_book
|
||||
|
||||
|
||||
@router.delete("/{book_id}")
|
||||
def delete_book(book_id: int):
|
||||
book = mock_storage.delete_book(book_id)
|
||||
if not book:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
return book
|
||||
43
tests/mock_routers/genres.py
Normal file
43
tests/mock_routers/genres.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from tests.mocks.mock_storage import mock_storage
|
||||
|
||||
router = APIRouter(prefix="/genres", tags=["genres"])
|
||||
|
||||
|
||||
@router.post("/")
|
||||
def create_genre(genre: dict):
|
||||
return mock_storage.create_genre(genre["name"])
|
||||
|
||||
|
||||
@router.get("/")
|
||||
def read_genres():
|
||||
genres = mock_storage.get_all_genres()
|
||||
return {"genres": genres, "total": len(genres)}
|
||||
|
||||
|
||||
@router.get("/{genre_id}")
|
||||
def get_genre(genre_id: int):
|
||||
genre = mock_storage.get_genre(genre_id)
|
||||
if not genre:
|
||||
raise HTTPException(status_code=404, detail="genre not found")
|
||||
|
||||
books = mock_storage.get_books_by_genre(genre_id)
|
||||
genre_with_books = genre.copy()
|
||||
genre_with_books["books"] = books
|
||||
return genre_with_books
|
||||
|
||||
|
||||
@router.put("/{genre_id}")
|
||||
def update_genre(genre_id: int, genre: dict):
|
||||
updated_genre = mock_storage.update_genre(genre_id, genre.get("name"))
|
||||
if not updated_genre:
|
||||
raise HTTPException(status_code=404, detail="genre not found")
|
||||
return updated_genre
|
||||
|
||||
|
||||
@router.delete("/{genre_id}")
|
||||
def delete_genre(genre_id: int):
|
||||
genre = mock_storage.delete_genre(genre_id)
|
||||
if not genre:
|
||||
raise HTTPException(status_code=404, detail="genre not found")
|
||||
return genre
|
||||
40
tests/mock_routers/relationships.py
Normal file
40
tests/mock_routers/relationships.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from tests.mocks.mock_storage import mock_storage
|
||||
|
||||
router = APIRouter(tags=["relations"])
|
||||
|
||||
|
||||
@router.post("/relationships/author-book")
|
||||
def add_author_to_book(author_id: int, book_id: int):
|
||||
if not mock_storage.create_author_book_link(author_id, book_id):
|
||||
if not mock_storage.get_author(author_id):
|
||||
raise HTTPException(status_code=404, detail="Author not found")
|
||||
if not mock_storage.get_book(book_id):
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
raise HTTPException(status_code=400, detail="Relationship already exists")
|
||||
|
||||
return {"author_id": author_id, "book_id": book_id}
|
||||
|
||||
|
||||
@router.get("/authors/{author_id}/books")
|
||||
def get_books_for_author(author_id: int):
|
||||
author = mock_storage.get_author(author_id)
|
||||
if not author:
|
||||
raise HTTPException(status_code=404, detail="Author not found")
|
||||
|
||||
return mock_storage.get_books_by_author(author_id)
|
||||
|
||||
|
||||
@router.get("/books/{book_id}/authors")
|
||||
def get_authors_for_book(book_id: int):
|
||||
book = mock_storage.get_book(book_id)
|
||||
if not book:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
|
||||
return mock_storage.get_authors_by_book(book_id)
|
||||
|
||||
|
||||
@router.post("/relationships/genre-book")
|
||||
def add_genre_to_book(genre_id: int, book_id: int):
|
||||
# For tests that need genre functionality
|
||||
return {"genre_id": genre_id, "book_id": book_id}
|
||||
Reference in New Issue
Block a user