Skip to content

Commit

Permalink
test: Coverage for repository and SQLAlchemy (#2586)
Browse files Browse the repository at this point in the history
* coverage: repository

---------

Signed-off-by: Janek Nouvertné <[email protected]>
  • Loading branch information
provinzkraut authored Nov 1, 2023
1 parent 5056928 commit 6973452
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 47 deletions.
10 changes: 5 additions & 5 deletions litestar/repository/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from __future__ import annotations
from __future__ import annotations # pragma: no cover

__all__ = ("ConflictError", "NotFoundError", "RepositoryError")
__all__ = ("ConflictError", "NotFoundError", "RepositoryError") # pragma: no cover


class RepositoryError(Exception):
class RepositoryError(Exception): # pragma: no cover
"""Base repository exception type."""


class ConflictError(RepositoryError):
class ConflictError(RepositoryError): # pragma: no cover
"""Data integrity error."""


class NotFoundError(RepositoryError):
class NotFoundError(RepositoryError): # pragma: no cover
"""An identity does not exist."""
2 changes: 1 addition & 1 deletion litestar/repository/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
try:
from advanced_alchemy.exceptions import ConflictError, NotFoundError, RepositoryError
except ImportError:
except ImportError: # pragma: no cover
from ._exceptions import ConflictError, NotFoundError, RepositoryError # type: ignore[assignment]

__all__ = ("ConflictError", "NotFoundError", "RepositoryError")
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ ignore_errors = true
module = ["tests.unit.test_kwargs.test_reserved_kwargs_injection"]
disallow_untyped_decorators = false

[[tool.mypy.overrides]]
module = ["tests.unit.test_contrib.test_repository"]
strict_equality = false

[[tool.mypy.overrides]]
ignore_missing_imports = true
module = [
Expand All @@ -267,7 +271,8 @@ exclude = [
"tests/examples",
"tests/docker_service_fixtures.py",
"litestar/contrib/pydantic/pydantic_dto_factory.py",
"litestar/contrib/pydantic/pydantic_init_plugin.py"
"litestar/contrib/pydantic/pydantic_init_plugin.py",
"tests/unit/test_contrib/test_sqlalchemy.py"
]
include = ["litestar", "tests"]
pythonVersion = "3.8"
Expand Down
80 changes: 80 additions & 0 deletions tests/unit/test_contrib/test_repository.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from litestar.app import Litestar
from litestar.repository import handlers
from litestar.repository.filters import (
Expand Down Expand Up @@ -27,3 +29,81 @@ def test_app_repository_signature_namespace() -> None:
"NotInSearchFilter": NotInSearchFilter,
"FilterTypes": FilterTypes,
}


def test_deprecated_abc_imports() -> None:
from litestar.contrib.repository import abc as abc_contrib
from litestar.repository import abc

assert abc_contrib.AbstractAsyncRepository is abc.AbstractAsyncRepository
assert abc_contrib.AbstractSyncRepository is abc.AbstractSyncRepository

with pytest.raises(AttributeError):
abc_contrib.foo


def test_deprecated_exception_imports() -> None:
from litestar.contrib.repository import exceptions as contrib_exceptions
from litestar.repository import exceptions

assert exceptions.RepositoryError is contrib_exceptions.RepositoryError
assert exceptions.ConflictError is contrib_exceptions.ConflictError
assert exceptions.NotFoundError is contrib_exceptions.NotFoundError

with pytest.raises(AttributeError):
contrib_exceptions.foo


def test_deprecated_filters_imports() -> None:
from litestar.contrib.repository import filters as contrib_filter
from litestar.repository import filters

assert filters.FilterTypes is contrib_filter.FilterTypes
assert filters.CollectionFilter is contrib_filter.CollectionFilter
assert filters.NotInCollectionFilter is contrib_filter.NotInCollectionFilter
assert filters.SearchFilter is contrib_filter.SearchFilter
assert filters.NotInSearchFilter is contrib_filter.NotInSearchFilter
assert filters.OnBeforeAfter is contrib_filter.OnBeforeAfter
assert filters.BeforeAfter is contrib_filter.BeforeAfter
assert filters.LimitOffset is contrib_filter.LimitOffset
assert filters.OrderBy is contrib_filter.OrderBy

with pytest.raises(AttributeError):
contrib_filter.foo


def test_advanced_alchemy_imports() -> None:
from advanced_alchemy import filters

from litestar.repository import _filters

assert filters.FilterTypes is not _filters.FilterTypes
assert filters.CollectionFilter is not _filters.CollectionFilter
assert filters.NotInCollectionFilter is not _filters.NotInCollectionFilter
assert filters.SearchFilter is not _filters.SearchFilter
assert filters.NotInSearchFilter is not _filters.NotInSearchFilter
assert filters.OnBeforeAfter is not _filters.OnBeforeAfter
assert filters.BeforeAfter is not _filters.BeforeAfter
assert filters.LimitOffset is not _filters.LimitOffset
assert filters.OrderBy is not _filters.OrderBy


def test_deprecated_handlers_imports() -> None:
from litestar.contrib.repository import handlers as contrib_handlers
from litestar.repository import handlers

assert handlers.on_app_init is contrib_handlers.on_app_init

with pytest.raises(AttributeError):
contrib_handlers.foo


def test_deprecated_testing_imports() -> None:
from litestar.contrib.repository import testing as contrib_testing
from litestar.repository.testing import generic_mock_repository

assert generic_mock_repository.GenericAsyncMockRepository is contrib_testing.GenericAsyncMockRepository
assert generic_mock_repository.GenericSyncMockRepository is contrib_testing.GenericSyncMockRepository

with pytest.raises(AttributeError):
contrib_testing.foo
64 changes: 64 additions & 0 deletions tests/unit/test_contrib/test_sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import annotations

import pytest
from advanced_alchemy import repository as advanced_alchemy_repo
from advanced_alchemy import types as advanced_alchemy_types
from advanced_alchemy.repository import typing as advanced_alchemy_typing
from sqlalchemy import Engine, create_engine
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine

from litestar.contrib.sqlalchemy.plugins.init.config.asyncio import SQLAlchemyAsyncConfig
from litestar.contrib.sqlalchemy.plugins.init.config.sync import SQLAlchemySyncConfig


def test_create_engine_with_engine_instance() -> None:
engine = create_engine("sqlite:///:memory:")
config = SQLAlchemySyncConfig(engine_instance=engine)
with pytest.deprecated_call():
assert engine is config.create_engine()


def test_create_engine_with_connection_string() -> None:
config = SQLAlchemySyncConfig(connection_string="sqlite:///:memory:")
with pytest.deprecated_call():
engine = config.create_engine()
assert isinstance(engine, Engine)


def test_async_create_engine_with_engine_instance() -> None:
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
config = SQLAlchemyAsyncConfig(engine_instance=engine)
with pytest.deprecated_call():
assert engine is config.create_engine()


def test_async_create_engine_with_connection_string() -> None:
config = SQLAlchemyAsyncConfig(connection_string="sqlite+aiosqlite:///:memory:")
with pytest.deprecated_call():
engine = config.create_engine()
assert isinstance(engine, AsyncEngine)


def test_repository_re_exports() -> None:
from litestar.contrib.sqlalchemy import types
from litestar.contrib.sqlalchemy.repository import (
SQLAlchemyAsyncRepository,
SQLAlchemySyncRepository,
wrap_sqlalchemy_exception,
)
from litestar.contrib.sqlalchemy.repository import types as repository_types

assert SQLAlchemySyncRepository is advanced_alchemy_repo.SQLAlchemySyncRepository
assert SQLAlchemyAsyncRepository is advanced_alchemy_repo.SQLAlchemyAsyncRepository
assert wrap_sqlalchemy_exception is advanced_alchemy_repo._util.wrap_sqlalchemy_exception

assert repository_types.ModelT is advanced_alchemy_typing.ModelT
assert repository_types.RowT is advanced_alchemy_typing.RowT
assert repository_types.SQLAlchemyAsyncRepositoryT is advanced_alchemy_typing.SQLAlchemyAsyncRepositoryT
assert repository_types.SQLAlchemySyncRepositoryT is advanced_alchemy_typing.SQLAlchemySyncRepositoryT

assert types.GUID is advanced_alchemy_types.GUID
assert types.ORA_JSONB is advanced_alchemy_types.ORA_JSONB
assert types.BigIntIdentity is advanced_alchemy_types.BigIntIdentity
assert types.DateTimeUTC is advanced_alchemy_types.DateTimeUTC
assert types.JsonB is advanced_alchemy_types.JsonB
Empty file.
Empty file.
Empty file.

This file was deleted.

This file was deleted.

0 comments on commit 6973452

Please sign in to comment.