mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
Форматирование кода, добавление лого, исправление тестов, улучшение эндпоинтов и документации
This commit is contained in:
169
tests/README.md
Normal file
169
tests/README.md
Normal file
@@ -0,0 +1,169 @@
|
||||
# Тесты без базы данных
|
||||
|
||||
## Обзор изменений
|
||||
|
||||
Все тесты были переработаны для работы без реальной базы данных PostgreSQL. Вместо этого используется in-memory мок-хранилище.
|
||||
|
||||
## Новые компоненты
|
||||
|
||||
### 1. Мок-хранилище ()
|
||||
- Реализует все операции с данными в памяти
|
||||
- Поддерживает CRUD операции для книг, авторов и жанров
|
||||
- Управляет связями между сущностями
|
||||
- Автоматически генерирует ID
|
||||
- Предоставляет метод для очистки данных между тестами
|
||||
|
||||
### 2. Мок-сессия ()
|
||||
- Эмулирует поведение SQLModel Session
|
||||
- Предоставляет совместимый интерфейс для dependency injection
|
||||
|
||||
### 3. Мок-роутеры ()
|
||||
- - упрощенные роутеры для операций с книгами
|
||||
- - упрощенные роутеры для операций с авторами
|
||||
- - упрощенные роутеры для связей между сущностями
|
||||
|
||||
### 4. Мок-приложение ()
|
||||
- FastAPI приложение для тестирования
|
||||
- Использует мок-роутеры вместо реальных
|
||||
- Включает реальный misc роутер (не требует БД)
|
||||
|
||||
## Обновленные тесты
|
||||
|
||||
Все тесты были обновлены:
|
||||
|
||||
###
|
||||
- Переработана фикстура для работы с мок-хранилищем
|
||||
- Добавлен автоматический cleanup между тестами
|
||||
|
||||
###
|
||||
- Использует мок-приложение вместо реального
|
||||
- Все тесты создают необходимые данные явно
|
||||
- Автоматическая очистка данных между тестами
|
||||
|
||||
###
|
||||
- Аналогично
|
||||
- Полная поддержка всех CRUD операций
|
||||
|
||||
###
|
||||
- Поддерживает создание и получение связей автор-книга
|
||||
- Тестирует получение авторов по книге и книг по автору
|
||||
|
||||
## Запуск тестов
|
||||
|
||||
============================= test session starts ==============================
|
||||
platform linux -- Python 3.13.7, pytest-8.4.1, pluggy-1.6.0 -- /bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /home/wowlikon/code/python/LibraryAPI
|
||||
configfile: pyproject.toml
|
||||
plugins: anyio-4.10.0, asyncio-0.26.0, cov-6.1.1
|
||||
asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
|
||||
collecting ... collected 23 items
|
||||
|
||||
tests/test_authors.py::test_empty_list_authors PASSED [ 4%]
|
||||
tests/test_authors.py::test_create_author PASSED [ 8%]
|
||||
tests/test_authors.py::test_list_authors PASSED [ 13%]
|
||||
tests/test_authors.py::test_get_existing_author PASSED [ 17%]
|
||||
tests/test_authors.py::test_get_not_existing_author PASSED [ 21%]
|
||||
tests/test_authors.py::test_update_author PASSED [ 26%]
|
||||
tests/test_authors.py::test_update_not_existing_author PASSED [ 30%]
|
||||
tests/test_authors.py::test_delete_author PASSED [ 34%]
|
||||
tests/test_authors.py::test_not_existing_delete_author PASSED [ 39%]
|
||||
tests/test_books.py::test_empty_list_books PASSED [ 43%]
|
||||
tests/test_books.py::test_create_book PASSED [ 47%]
|
||||
tests/test_books.py::test_list_books PASSED [ 52%]
|
||||
tests/test_books.py::test_get_existing_book PASSED [ 56%]
|
||||
tests/test_books.py::test_get_not_existing_book PASSED [ 60%]
|
||||
tests/test_books.py::test_update_book PASSED [ 65%]
|
||||
tests/test_books.py::test_update_not_existing_book PASSED [ 69%]
|
||||
tests/test_books.py::test_delete_book PASSED [ 73%]
|
||||
tests/test_books.py::test_not_existing_delete_book PASSED [ 78%]
|
||||
tests/test_misc.py::test_main_page PASSED [ 82%]
|
||||
tests/test_misc.py::test_app_info_test PASSED [ 86%]
|
||||
tests/test_relationships.py::test_prepare_data PASSED [ 91%]
|
||||
tests/test_relationships.py::test_get_book_authors PASSED [ 95%]
|
||||
tests/test_relationships.py::test_get_author_books PASSED [100%]
|
||||
|
||||
============================== 23 passed in 1.42s ==============================
|
||||
============================= test session starts ==============================
|
||||
platform linux -- Python 3.13.7, pytest-8.4.1, pluggy-1.6.0 -- /bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /home/wowlikon/code/python/LibraryAPI
|
||||
configfile: pyproject.toml
|
||||
plugins: anyio-4.10.0, asyncio-0.26.0, cov-6.1.1
|
||||
asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
|
||||
collecting ... collected 9 items
|
||||
|
||||
tests/test_books.py::test_empty_list_books PASSED [ 11%]
|
||||
tests/test_books.py::test_create_book PASSED [ 22%]
|
||||
tests/test_books.py::test_list_books PASSED [ 33%]
|
||||
tests/test_books.py::test_get_existing_book PASSED [ 44%]
|
||||
tests/test_books.py::test_get_not_existing_book PASSED [ 55%]
|
||||
tests/test_books.py::test_update_book PASSED [ 66%]
|
||||
tests/test_books.py::test_update_not_existing_book PASSED [ 77%]
|
||||
tests/test_books.py::test_delete_book PASSED [ 88%]
|
||||
tests/test_books.py::test_not_existing_delete_book PASSED [100%]
|
||||
|
||||
============================== 9 passed in 0.99s ===============================
|
||||
============================= test session starts ==============================
|
||||
platform linux -- Python 3.13.7, pytest-8.4.1, pluggy-1.6.0 -- /bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /home/wowlikon/code/python/LibraryAPI
|
||||
configfile: pyproject.toml
|
||||
plugins: anyio-4.10.0, asyncio-0.26.0, cov-6.1.1
|
||||
asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
|
||||
collecting ... collected 9 items
|
||||
|
||||
tests/test_authors.py::test_empty_list_authors PASSED [ 11%]
|
||||
tests/test_authors.py::test_create_author PASSED [ 22%]
|
||||
tests/test_authors.py::test_list_authors PASSED [ 33%]
|
||||
tests/test_authors.py::test_get_existing_author PASSED [ 44%]
|
||||
tests/test_authors.py::test_get_not_existing_author PASSED [ 55%]
|
||||
tests/test_authors.py::test_update_author PASSED [ 66%]
|
||||
tests/test_authors.py::test_update_not_existing_author PASSED [ 77%]
|
||||
tests/test_authors.py::test_delete_author PASSED [ 88%]
|
||||
tests/test_authors.py::test_not_existing_delete_author PASSED [100%]
|
||||
|
||||
============================== 9 passed in 0.96s ===============================
|
||||
============================= test session starts ==============================
|
||||
platform linux -- Python 3.13.7, pytest-8.4.1, pluggy-1.6.0 -- /bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /home/wowlikon/code/python/LibraryAPI
|
||||
configfile: pyproject.toml
|
||||
plugins: anyio-4.10.0, asyncio-0.26.0, cov-6.1.1
|
||||
asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
|
||||
collecting ... collected 3 items
|
||||
|
||||
tests/test_relationships.py::test_prepare_data PASSED [ 33%]
|
||||
tests/test_relationships.py::test_get_book_authors PASSED [ 66%]
|
||||
tests/test_relationships.py::test_get_author_books PASSED [100%]
|
||||
|
||||
============================== 3 passed in 1.09s ===============================
|
||||
============================= test session starts ==============================
|
||||
platform linux -- Python 3.13.7, pytest-8.4.1, pluggy-1.6.0 -- /bin/python
|
||||
cachedir: .pytest_cache
|
||||
rootdir: /home/wowlikon/code/python/LibraryAPI
|
||||
configfile: pyproject.toml
|
||||
plugins: anyio-4.10.0, asyncio-0.26.0, cov-6.1.1
|
||||
asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
|
||||
collecting ... collected 2 items
|
||||
|
||||
tests/test_misc.py::test_main_page PASSED [ 50%]
|
||||
tests/test_misc.py::test_app_info_test PASSED [100%]
|
||||
|
||||
============================== 2 passed in 0.93s ===============================
|
||||
|
||||
## Преимущества нового подхода
|
||||
|
||||
1. **Независимость**: Тесты не требуют PostgreSQL или Docker
|
||||
2. **Скорость**: Выполняются значительно быстрее
|
||||
3. **Изоляция**: Каждый тест работает с чистым состоянием
|
||||
4. **Стабильность**: Нет проблем с сетевыми подключениями или состоянием БД
|
||||
5. **CI/CD готовность**: Легко интегрируются в CI пайплайны
|
||||
|
||||
## Ограничения
|
||||
|
||||
- Мок-хранилище упрощено по сравнению с реальной БД
|
||||
- Отсутствуют некоторые возможности SQLModel (сложные запросы, транзакции)
|
||||
- Нет проверки целостности данных на уровне БД
|
||||
|
||||
Однако для юнит-тестирования API логики этого достаточно.
|
||||
26
tests/mock_app.py
Normal file
26
tests/mock_app.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from fastapi import FastAPI
|
||||
from tests.mock_routers import books, authors, genres, relationships
|
||||
from library_service.routers.misc import router as misc_router
|
||||
|
||||
|
||||
def create_mock_app() -> FastAPI:
|
||||
"""Create FastAPI app with mock routers for testing"""
|
||||
app = FastAPI(
|
||||
title="Library API Test",
|
||||
description="Library API for testing without database",
|
||||
version="1.0.0",
|
||||
)
|
||||
|
||||
# Include mock routers
|
||||
app.include_router(books.router)
|
||||
app.include_router(authors.router)
|
||||
app.include_router(genres.router)
|
||||
app.include_router(relationships.router)
|
||||
|
||||
# Include real misc router (it doesn't use database)
|
||||
app.include_router(misc_router)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
mock_app = create_mock_app()
|
||||
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}
|
||||
0
tests/mocks/__init__.py
Normal file
0
tests/mocks/__init__.py
Normal file
62
tests/mocks/mock_session.py
Normal file
62
tests/mocks/mock_session.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from typing import Optional, List, Any
|
||||
from tests.mocks.mock_storage import mock_storage
|
||||
|
||||
|
||||
class MockSession:
|
||||
"""Mock SQLModel Session that works with MockStorage"""
|
||||
|
||||
def __init__(self):
|
||||
self.storage = mock_storage
|
||||
|
||||
def add(self, obj: Any):
|
||||
"""Mock add - not needed for our implementation"""
|
||||
pass
|
||||
|
||||
def commit(self):
|
||||
"""Mock commit - not needed for our implementation"""
|
||||
pass
|
||||
|
||||
def refresh(self, obj: Any):
|
||||
"""Mock refresh - not needed for our implementation"""
|
||||
pass
|
||||
|
||||
def get(self, model_class, pk: int):
|
||||
"""Mock get method to retrieve object by primary key"""
|
||||
if hasattr(model_class, "__name__"):
|
||||
model_name = model_class.__name__.lower()
|
||||
else:
|
||||
model_name = str(model_class).lower()
|
||||
|
||||
if "book" in model_name:
|
||||
return self.storage.get_book(pk)
|
||||
elif "author" in model_name:
|
||||
return self.storage.get_author(pk)
|
||||
elif "genre" in model_name:
|
||||
return self.storage.get_genre(pk)
|
||||
return None
|
||||
|
||||
def delete(self, obj: Any):
|
||||
"""Mock delete - handled in storage methods"""
|
||||
pass
|
||||
|
||||
def exec(self, statement):
|
||||
"""Mock exec method for queries"""
|
||||
return MockResult([])
|
||||
|
||||
|
||||
class MockResult:
|
||||
"""Mock result for query operations"""
|
||||
|
||||
def __init__(self, data: List):
|
||||
self.data = data
|
||||
|
||||
def all(self):
|
||||
return self.data
|
||||
|
||||
def first(self):
|
||||
return self.data[0] if self.data else None
|
||||
|
||||
|
||||
def mock_get_session():
|
||||
"""Mock session dependency"""
|
||||
return MockSession()
|
||||
169
tests/mocks/mock_storage.py
Normal file
169
tests/mocks/mock_storage.py
Normal file
@@ -0,0 +1,169 @@
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
|
||||
class MockStorage:
|
||||
"""In-memory storage for testing without database"""
|
||||
|
||||
def __init__(self):
|
||||
self.books = {}
|
||||
self.authors = {}
|
||||
self.genres = {}
|
||||
self.author_book_links = []
|
||||
self.genre_book_links = []
|
||||
self.book_id_counter = 1
|
||||
self.author_id_counter = 1
|
||||
self.genre_id_counter = 1
|
||||
|
||||
def clear_all(self):
|
||||
"""Clear all data"""
|
||||
self.books.clear()
|
||||
self.authors.clear()
|
||||
self.genres.clear()
|
||||
self.author_book_links.clear()
|
||||
self.genre_book_links.clear()
|
||||
self.book_id_counter = 1
|
||||
self.author_id_counter = 1
|
||||
self.genre_id_counter = 1
|
||||
|
||||
# Book operations
|
||||
def create_book(self, title: str, description: str) -> dict:
|
||||
book_id = self.book_id_counter
|
||||
book = {"id": book_id, "title": title, "description": description}
|
||||
self.books[book_id] = book
|
||||
self.book_id_counter += 1
|
||||
return book
|
||||
|
||||
def get_book(self, book_id: int) -> Optional[dict]:
|
||||
return self.books.get(book_id)
|
||||
|
||||
def get_all_books(self) -> List[dict]:
|
||||
return list(self.books.values())
|
||||
|
||||
def update_book(
|
||||
self,
|
||||
book_id: int,
|
||||
title: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
) -> Optional[dict]:
|
||||
if book_id not in self.books:
|
||||
return None
|
||||
book = self.books[book_id]
|
||||
if title is not None:
|
||||
book["title"] = title
|
||||
if description is not None:
|
||||
book["description"] = description
|
||||
return book
|
||||
|
||||
def delete_book(self, book_id: int) -> Optional[dict]:
|
||||
if book_id not in self.books:
|
||||
return None
|
||||
book = self.books.pop(book_id)
|
||||
self.author_book_links = [
|
||||
link for link in self.author_book_links if link["book_id"] != book_id
|
||||
]
|
||||
self.genre_book_links = [
|
||||
link for link in self.genre_book_links if link["book_id"] != book_id
|
||||
]
|
||||
return book
|
||||
|
||||
# Author operations
|
||||
def create_author(self, name: str) -> dict:
|
||||
author_id = self.author_id_counter
|
||||
author = {"id": author_id, "name": name}
|
||||
self.authors[author_id] = author
|
||||
self.author_id_counter += 1
|
||||
return author
|
||||
|
||||
def get_author(self, author_id: int) -> Optional[dict]:
|
||||
return self.authors.get(author_id)
|
||||
|
||||
def get_all_authors(self) -> List[dict]:
|
||||
return list(self.authors.values())
|
||||
|
||||
def update_author(
|
||||
self, author_id: int, name: Optional[str] = None
|
||||
) -> Optional[dict]:
|
||||
if author_id not in self.authors:
|
||||
return None
|
||||
author = self.authors[author_id]
|
||||
if name is not None:
|
||||
author["name"] = name
|
||||
return author
|
||||
|
||||
def delete_author(self, author_id: int) -> Optional[dict]:
|
||||
if author_id not in self.authors:
|
||||
return None
|
||||
author = self.authors.pop(author_id)
|
||||
self.author_book_links = [
|
||||
link for link in self.author_book_links if link["author_id"] != author_id
|
||||
]
|
||||
return author
|
||||
|
||||
# Genre operations
|
||||
def create_genre(self, name: str) -> dict:
|
||||
genre_id = self.genre_id_counter
|
||||
genre = {"id": genre_id, "name": name}
|
||||
self.genres[genre_id] = genre
|
||||
self.genre_id_counter += 1
|
||||
return genre
|
||||
|
||||
def get_genre(self, genre_id: int) -> Optional[dict]:
|
||||
return self.genres.get(genre)
|
||||
|
||||
def get_all_authors(self) -> List[dict]:
|
||||
return list(self.authors.values())
|
||||
|
||||
def update_genre(
|
||||
self, genre_id: int, name: Optional[str] = None
|
||||
) -> Optional[dict]:
|
||||
if genre_id not in self.genres:
|
||||
return None
|
||||
genre = self.genres[genre_id]
|
||||
if name is not None:
|
||||
genre["name"] = name
|
||||
return genre
|
||||
|
||||
def delete_genre(self, genre_id: int) -> Optional[dict]:
|
||||
if genre_id not in self.genres:
|
||||
return None
|
||||
genre = self.genres.pop(genre_id)
|
||||
self.genre_book_links = [
|
||||
link for link in self.genre_book_links if link["genre_id"] != genre_id
|
||||
]
|
||||
return genre
|
||||
|
||||
# Relationship operations
|
||||
def create_author_book_link(self, author_id: int, book_id: int) -> bool:
|
||||
if author_id not in self.authors or book_id not in self.books:
|
||||
return False
|
||||
for link in self.author_book_links:
|
||||
if link["author_id"] == author_id and link["book_id"] == book_id:
|
||||
return False
|
||||
self.author_book_links.append({"author_id": author_id, "book_id": book_id})
|
||||
return True
|
||||
|
||||
def get_authors_by_book(self, book_id: int) -> List[dict]:
|
||||
author_ids = [
|
||||
link["author_id"]
|
||||
for link in self.author_book_links
|
||||
if link["book_id"] == book_id
|
||||
]
|
||||
return [
|
||||
self.authors[author_id]
|
||||
for author_id in author_ids
|
||||
if author_id in self.authors
|
||||
]
|
||||
|
||||
def get_books_by_author(self, author_id: int) -> List[dict]:
|
||||
book_ids = [
|
||||
link["book_id"]
|
||||
for link in self.author_book_links
|
||||
if link["author_id"] == author_id
|
||||
]
|
||||
return [self.books[book_id] for book_id in book_ids if book_id in self.books]
|
||||
|
||||
def get_all_author_book_links(self) -> List[dict]:
|
||||
return list(self.author_book_links)
|
||||
|
||||
|
||||
mock_storage = MockStorage()
|
||||
@@ -1,66 +1,107 @@
|
||||
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: add tests for author endpoints
|
||||
@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_authors(setup_database):
|
||||
|
||||
def test_empty_list_authors():
|
||||
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):
|
||||
|
||||
def test_create_author():
|
||||
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):
|
||||
|
||||
def test_list_authors():
|
||||
# First create an author
|
||||
client.post("/authors", json={"name": "Test Author"})
|
||||
|
||||
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"
|
||||
assert response.json() == {
|
||||
"authors": [{"id": 1, "name": "Test Author"}],
|
||||
"total": 1,
|
||||
}, "Invalid response data"
|
||||
|
||||
|
||||
def test_get_existing_author():
|
||||
# First create an author
|
||||
client.post("/authors", json={"name": "Test Author"})
|
||||
|
||||
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", "books": []}, "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():
|
||||
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):
|
||||
|
||||
def test_update_author():
|
||||
# First create an author
|
||||
client.post("/authors", json={"name": "Test Author"})
|
||||
|
||||
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"
|
||||
assert response.json() == {
|
||||
"id": 1,
|
||||
"name": "Updated Author",
|
||||
}, "Invalid response data"
|
||||
|
||||
def test_update_not_existing_author(setup_database):
|
||||
|
||||
def test_update_not_existing_author():
|
||||
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):
|
||||
|
||||
def test_delete_author():
|
||||
# First create an author
|
||||
client.post("/authors", json={"name": "Test Author"})
|
||||
|
||||
# Update it first
|
||||
client.put("/authors/1", json={"name": "Updated Author"})
|
||||
|
||||
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"
|
||||
assert response.json() == {
|
||||
"id": 1,
|
||||
"name": "Updated Author",
|
||||
}, "Invalid response data"
|
||||
|
||||
def test_not_existing_delete_author(setup_database):
|
||||
|
||||
def test_not_existing_delete_author():
|
||||
response = client.delete("/authors/2")
|
||||
assert response.status_code == 404, "Invalid response status"
|
||||
assert response.json() == {"detail": "Author not found"}, "Invalid response data"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -1,56 +1,27 @@
|
||||
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
|
||||
from tests.mock_app import mock_app
|
||||
from tests.mocks.mock_storage import mock_storage
|
||||
|
||||
from library_service.main import app, engine
|
||||
from library_service.models.db import Author, Book, Genre
|
||||
from library_service.models.db import AuthorBookLink, GenreBookLink
|
||||
client = TestClient(mock_app)
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_database():
|
||||
# Save original data backup
|
||||
with Session(engine) as session:
|
||||
original_authors = session.exec(select(Author)).all()
|
||||
original_books = session.exec(select(Book)).all()
|
||||
original_genres = session.exec(select(Genre)).all()
|
||||
original_author_book_links = session.exec(select(AuthorBookLink)).all()
|
||||
original_genre_book_links = session.exec(select(GenreBookLink)).all()
|
||||
# Reset database
|
||||
alembic_cfg = Config("alembic.ini")
|
||||
with engine.begin() as connection:
|
||||
alembic_cfg.attributes['connection'] = connection
|
||||
command.downgrade(alembic_cfg, 'base')
|
||||
command.upgrade(alembic_cfg, 'head')
|
||||
# Check database state after reset
|
||||
with Session(engine) as session:
|
||||
assert len(session.exec(select(Author)).all()) == 0
|
||||
assert len(session.exec(select(Book)).all()) == 0
|
||||
assert len(session.exec(select(Genre)).all()) == 0
|
||||
assert len(session.exec(select(AuthorBookLink)).all()) == 0
|
||||
assert len(session.exec(select(GenreBookLink)).all()) == 0
|
||||
yield # Here pytest will start testing
|
||||
# Restore original data from backup
|
||||
with Session(engine) as session:
|
||||
for author in original_authors:
|
||||
session.add(author)
|
||||
for book in original_books:
|
||||
session.add(book)
|
||||
for link in original_author_book_links:
|
||||
session.add(link)
|
||||
for link in original_genre_book_links:
|
||||
session.add(link)
|
||||
session.commit()
|
||||
"""Setup and cleanup mock database for each test"""
|
||||
# Clear data before each test
|
||||
mock_storage.clear_all()
|
||||
yield
|
||||
# Clear data after each test (optional, but good practice)
|
||||
mock_storage.clear_all()
|
||||
|
||||
|
||||
# Test the main page of the application
|
||||
def test_main_page():
|
||||
response = client.get("/") # Send GET request to the main page
|
||||
try:
|
||||
content = response.content.decode('utf-8') # Decode response content
|
||||
content = response.content.decode("utf-8") # Decode response content
|
||||
# Find indices of key elements in the content
|
||||
title_idx = content.index("Welcome to ")
|
||||
description_idx = content.index("Description: ")
|
||||
@@ -59,17 +30,18 @@ def test_main_page():
|
||||
status_idx = content.index("Status: ")
|
||||
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert content.startswith('<!doctype html>'), "Not HTML"
|
||||
assert content.endswith('</html>'), "HTML tag not closed"
|
||||
assert content[title_idx+1] != '<', "Title not provided"
|
||||
assert content[description_idx+1] != '<', "Description not provided"
|
||||
assert content[version_idx+1] != '<', "Version not provided"
|
||||
assert content[time_idx+1] != '<', "Time not provided"
|
||||
assert content[status_idx+1] != '<', "Status not provided"
|
||||
assert content.startswith("<!doctype html>"), "Not HTML"
|
||||
assert content.endswith("</html>"), "HTML tag not closed"
|
||||
assert content[title_idx + 1] != "<", "Title not provided"
|
||||
assert content[description_idx + 1] != "<", "Description not provided"
|
||||
assert content[version_idx + 1] != "<", "Version not provided"
|
||||
assert content[time_idx + 1] != "<", "Time not provided"
|
||||
assert content[status_idx + 1] != "<", "Status not provided"
|
||||
except Exception as e:
|
||||
print(f"Error: {e}") # Print error if an exception occurs
|
||||
assert False, "Unexpected error" # Force test failure on unexpected error
|
||||
|
||||
|
||||
# Test application info endpoint
|
||||
def test_app_info_test():
|
||||
response = client.get("/api/info") # Send GET request to the info endpoint
|
||||
@@ -79,5 +51,12 @@ def test_app_info_test():
|
||||
assert response.json()["app_info"]["description"] != "", "Description not provided"
|
||||
assert response.json()["app_info"]["version"] != "", "Version not provided"
|
||||
# Check time difference
|
||||
assert 0 < (datetime.now() - datetime.fromisoformat(response.json()["server_time"])).total_seconds(), "Negative time difference"
|
||||
assert (datetime.now() - datetime.fromisoformat(response.json()["server_time"])).total_seconds() < 1, "Time difference too large"
|
||||
assert (
|
||||
0
|
||||
< (
|
||||
datetime.now() - datetime.fromisoformat(response.json()["server_time"])
|
||||
).total_seconds()
|
||||
), "Negative time difference"
|
||||
assert (
|
||||
datetime.now() - datetime.fromisoformat(response.json()["server_time"])
|
||||
).total_seconds() < 1, "Time difference too large"
|
||||
|
||||
@@ -1,42 +1,72 @@
|
||||
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)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_database():
|
||||
"""Clear mock storage before each test"""
|
||||
mock_storage.clear_all()
|
||||
yield
|
||||
mock_storage.clear_all()
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
def make_authorbook_relationship(author_id, book_id):
|
||||
response = client.post("/relationships/author-book", params={"author_id": author_id, "book_id": book_id})
|
||||
response = client.post(
|
||||
"/relationships/author-book",
|
||||
params={"author_id": author_id, "book_id": book_id},
|
||||
)
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
|
||||
def make_genrebook_relationship(author_id, book_id):
|
||||
response = client.post("/relationships/genre-book", params={"genre_id": author_id, "book_id": book_id})
|
||||
|
||||
def make_genrebook_relationship(genre_id, book_id):
|
||||
response = client.post(
|
||||
"/relationships/genre-book", params={"genre_id": genre_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"})
|
||||
def test_prepare_data():
|
||||
# Create books
|
||||
assert client.post(
|
||||
"/books", json={"title": "Test Book 1", "description": "Test Description 1"}
|
||||
).status_code == 200
|
||||
assert client.post(
|
||||
"/books", json={"title": "Test Book 2", "description": "Test Description 2"}
|
||||
).status_code == 200
|
||||
assert client.post(
|
||||
"/books", json={"title": "Test Book 3", "description": "Test Description 3"}
|
||||
).status_code == 200
|
||||
|
||||
# Create authors
|
||||
assert client.post("/authors", json={"name": "Test Author 1"}).status_code == 200
|
||||
assert client.post("/authors", json={"name": "Test Author 2"}).status_code == 200
|
||||
assert client.post("/authors", json={"name": "Test Author 3"}).status_code == 200
|
||||
|
||||
# Create genres
|
||||
assert client.post("/genres", json={"name": "Test Genre 1"}).status_code == 200
|
||||
assert client.post("/genres", json={"name": "Test Genre 2"}).status_code == 200
|
||||
assert client.post("/genres", json={"name": "Test Genre 3"}).status_code == 200
|
||||
|
||||
# Create relationships
|
||||
make_authorbook_relationship(1, 1)
|
||||
make_authorbook_relationship(2, 1)
|
||||
make_authorbook_relationship(1, 2)
|
||||
make_authorbook_relationship(2, 3)
|
||||
make_authorbook_relationship(3, 3)
|
||||
|
||||
response = client.get("/relationships/author-book")
|
||||
assert response.status_code == 200, "Invalid response status"
|
||||
assert len(response.json()) == 5, "Invalid number of relationships"
|
||||
make_genrebook_relationship(1, 1)
|
||||
make_genrebook_relationship(2, 1)
|
||||
make_genrebook_relationship(1, 2)
|
||||
make_genrebook_relationship(2, 3)
|
||||
make_genrebook_relationship(3, 3)
|
||||
|
||||
def test_get_book_authors():
|
||||
# Setup test data
|
||||
test_prepare_data()
|
||||
|
||||
response1 = client.get("/books/1/authors")
|
||||
assert response1.status_code == 200, "Invalid response status"
|
||||
assert len(response1.json()) == 2, "Invalid number of authors"
|
||||
@@ -59,7 +89,11 @@ def test_get_book_authors():
|
||||
assert response3.json()[0]["id"] == 2
|
||||
assert response3.json()[1]["id"] == 3
|
||||
|
||||
|
||||
def test_get_author_books():
|
||||
# Setup test data
|
||||
test_prepare_data()
|
||||
|
||||
response1 = client.get("/authors/1/books")
|
||||
assert response1.status_code == 200, "Invalid response status"
|
||||
assert len(response1.json()) == 2, "Invalid number of books"
|
||||
|
||||
Reference in New Issue
Block a user