Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/gateway/repositories/base_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,25 @@

from typing import Any, Generic, TypeVar

from pydantic import BaseModel
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import class_mapper
from sqlmodel import SQLModel

from gateway.core.unit_of_work import UnitOfWork, session_for

ModelType = TypeVar("ModelType", bound=SQLModel)
CreateSchemaType = TypeVar("CreateSchemaType", bound=SQLModel)
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=SQLModel)
ModelType = TypeVar("ModelType")
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)


class BaseRepository(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
"""Common CRUD operations over one SQLModel table.
"""Provide the common CRUD operations over one mapped table.

Repositories are pure data access: no business logic, no authorization, and
no commits (see the module docstring).
Repositories are pure data access: no business logic, no authorization, and no commits (see the module docstring).

Type Parameters:
ModelType: The SQLModel table class.
ModelType: The mapped table class.
CreateSchemaType: The schema describing a creation payload.
UpdateSchemaType: The schema describing an update payload.
"""
Expand All @@ -45,7 +44,7 @@ def __init__(self, db: AsyncSession | UnitOfWork, model_class: type[ModelType]):

Args:
db: The active async session, or the unit of work whose blocks it runs in.
model_class: The SQLModel class for this repository.
model_class: The mapped class for this repository.
"""
self._db = db
self.model_class = model_class
Expand Down
30 changes: 30 additions & 0 deletions tests/integration/test_base_repository_declarative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""The generic repository serves a declarative table as well as a SQLModel one.

mypy does most of the checking here, because the runtime does not enforce the bound of a type argument.
"""

import pytest
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession

from gateway.core.unit_of_work import UnitOfWork
from gateway.models.budgets import BudgetResetLog
from gateway.repositories.base_repository import BaseRepository

pytestmark = pytest.mark.asyncio


class _Empty(BaseModel):
pass


class _BudgetResetLogReader(BaseRepository[BudgetResetLog, _Empty, _Empty]):
def __init__(self, uow: UnitOfWork) -> None:
super().__init__(uow, BudgetResetLog)


async def test_a_repository_counts_a_declarative_table(async_db: AsyncSession) -> None:
uow = UnitOfWork(async_db)

async with uow:
assert await _BudgetResetLogReader(uow).count() == 0
Loading