mirror of
https://github.com/Kludex/awesome-fastapi-projects.git
synced 2025-05-15 05:37:05 +00:00
Test listing the repositories
This commit is contained in:
parent
4cc03600c3
commit
61967ab207
@ -4,10 +4,12 @@ from collections.abc import AsyncGenerator
|
|||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from dirty_equals import IsList
|
||||||
from pytest_mock import MockerFixture
|
from pytest_mock import MockerFixture
|
||||||
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession
|
||||||
|
|
||||||
from app.database import async_session_maker
|
from app.database import Dependency, Repo, async_session_maker
|
||||||
|
from app.factories import RepoCreateDataFactory
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="session")
|
@pytest.fixture(autouse=True, scope="session")
|
||||||
@ -19,7 +21,7 @@ def anyio_backend() -> Literal["asyncio"]:
|
|||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def test_db(mocker: MockerFixture) -> None:
|
def test_db(mocker: MockerFixture) -> None:
|
||||||
"""Use the in-memory database for tests."""
|
"""Use the in-memory database for tests."""
|
||||||
mocker.patch("app.database.SQLALCHEMY_DATABASE_URL", "sqlite+aiosqlite:///")
|
mocker.patch("app.database.DB_PATH", "")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
@ -61,10 +63,31 @@ async def test_db_session(
|
|||||||
) -> AsyncGenerator[AsyncSession, None]:
|
) -> AsyncGenerator[AsyncSession, None]:
|
||||||
"""Use the in-memory database for tests."""
|
"""Use the in-memory database for tests."""
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
try:
|
|
||||||
async with session.begin():
|
async with session.begin():
|
||||||
|
try:
|
||||||
yield session
|
yield session
|
||||||
finally:
|
finally:
|
||||||
await session.flush()
|
await session.flush()
|
||||||
await session.rollback()
|
await session.rollback()
|
||||||
await session.close()
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def some_repos(
|
||||||
|
test_db_session: AsyncSession, repo_create_data_factory: RepoCreateDataFactory
|
||||||
|
) -> list[Repo]:
|
||||||
|
"""Create some repos."""
|
||||||
|
repo_create_data = repo_create_data_factory.batch(10)
|
||||||
|
assert repo_create_data == IsList(length=10)
|
||||||
|
repos = [
|
||||||
|
Repo(
|
||||||
|
url=str(repo.url),
|
||||||
|
dependencies=[
|
||||||
|
Dependency(name=dependency.name) for dependency in repo.dependencies
|
||||||
|
],
|
||||||
|
)
|
||||||
|
for repo in repo_create_data
|
||||||
|
]
|
||||||
|
test_db_session.add_all(repos)
|
||||||
|
await test_db_session.flush()
|
||||||
|
await asyncio.gather(*[test_db_session.refresh(repo) for repo in repos])
|
||||||
|
yield repos
|
||||||
|
@ -18,3 +18,18 @@ class RepoCreateData(BaseModel):
|
|||||||
|
|
||||||
url: AnyUrl
|
url: AnyUrl
|
||||||
dependencies: list[DependencyCreateData] = Field(default_factory=list)
|
dependencies: list[DependencyCreateData] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
class DependencyDetails(BaseModel):
|
||||||
|
"""A single dependency."""
|
||||||
|
|
||||||
|
id: DependencyId
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
|
class RepoDetails(BaseModel):
|
||||||
|
"""A repository that is being tracked."""
|
||||||
|
|
||||||
|
id: RepoId
|
||||||
|
url: AnyUrl
|
||||||
|
dependencies: list[DependencyDetails] = Field(default_factory=list)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
"""Test the operations on the database models."""
|
"""Test the operations on the database models."""
|
||||||
import pytest
|
import pytest
|
||||||
|
import sqlalchemy as sa
|
||||||
|
import sqlalchemy.orm
|
||||||
from dirty_equals import IsList
|
from dirty_equals import IsList
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
@ -49,3 +51,27 @@ async def test_create_repo_with_dependencies(
|
|||||||
repo_dependencies, repo_create_data.dependencies, strict=True
|
repo_dependencies, repo_create_data.dependencies, strict=True
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_list_repositories(
|
||||||
|
test_db_session: AsyncSession,
|
||||||
|
some_repos: list[database.Repo],
|
||||||
|
) -> None:
|
||||||
|
"""Test listing repositories."""
|
||||||
|
repos_from_db_result = await test_db_session.execute(
|
||||||
|
sa.select(database.Repo).options(
|
||||||
|
sqlalchemy.orm.joinedload(database.Repo.dependencies)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
repos_from_db = repos_from_db_result.scalars().unique().all()
|
||||||
|
assert repos_from_db == IsList(length=10)
|
||||||
|
assert all(
|
||||||
|
repo.url == str(repo_create_data.url)
|
||||||
|
and all(
|
||||||
|
repo_dependency.name == dependency.name
|
||||||
|
for repo_dependency, dependency in zip(
|
||||||
|
repo.dependencies, repo_create_data.dependencies, strict=True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
for repo, repo_create_data in zip(repos_from_db, some_repos, strict=True)
|
||||||
|
)
|
||||||
|
@ -95,6 +95,7 @@ extend-select = [
|
|||||||
"**/migrations/*.py" = ["D"]
|
"**/migrations/*.py" = ["D"]
|
||||||
"**/migrations/env.py" = ["ERA001"]
|
"**/migrations/env.py" = ["ERA001"]
|
||||||
"**/tests/*.py" = ["S101"]
|
"**/tests/*.py" = ["S101"]
|
||||||
|
"**/conftest.py" = ["S101"]
|
||||||
|
|
||||||
[tool.ruff.pydocstyle]
|
[tool.ruff.pydocstyle]
|
||||||
convention = "numpy"
|
convention = "numpy"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user