diff --git a/src/starlite_saqlalchemy/service.py b/src/starlite_saqlalchemy/service.py index 4e9e3296..660c4e29 100644 --- a/src/starlite_saqlalchemy/service.py +++ b/src/starlite_saqlalchemy/service.py @@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypeVar from starlite_saqlalchemy.db import async_session_factory +from starlite_saqlalchemy.exceptions import NotFoundError from starlite_saqlalchemy.repository.sqlalchemy import ModelT from starlite_saqlalchemy.worker import queue @@ -50,6 +51,8 @@ def __init_subclass__(cls, *_: Any, **__: Any) -> None: cls.__id__ = f"{cls.__module__}.{cls.__name__}" service_object_identity_map[cls.__id__] = cls + # pylint:disable=unused-argument + async def create(self, data: T) -> T: """Create an instance of `T`. @@ -59,7 +62,7 @@ async def create(self, data: T) -> T: Returns: Representation of created instance. """ - raise NotImplementedError + return data async def list(self, **kwargs: Any) -> list[T]: """Return view of the collection of `T`. @@ -70,7 +73,7 @@ async def list(self, **kwargs: Any) -> list[T]: Returns: The list of instances retrieved from the repository. """ - raise NotImplementedError + return [] async def update(self, id_: Any, data: T) -> T: """Update existing instance of `T` with `data`. @@ -82,7 +85,7 @@ async def update(self, id_: Any, data: T) -> T: Returns: Updated representation. """ - raise NotImplementedError + return data async def upsert(self, id_: Any, data: T) -> T: """Create or update an instance of `T` with `data`. @@ -94,7 +97,7 @@ async def upsert(self, id_: Any, data: T) -> T: Returns: Updated or created representation. """ - raise NotImplementedError + return data async def get(self, id_: Any) -> T: """Retrieve a representation of `T` with that is identified by `id_` @@ -105,7 +108,7 @@ async def get(self, id_: Any) -> T: Returns: Representation of instance with identifier `id_`. """ - raise NotImplementedError + raise NotFoundError async def delete(self, id_: Any) -> T: """Delete `T` that is identified by `id_`. @@ -116,7 +119,7 @@ async def delete(self, id_: Any) -> T: Returns: Representation of the deleted instance. """ - raise NotImplementedError + raise NotFoundError async def enqueue_background_task(self, method_name: str, **kwargs: Any) -> None: """Enqueue an async callback for the operation and data. diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 10719e5d..f3354b1c 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -9,6 +9,7 @@ import pytest from starlite_saqlalchemy import db, service, worker +from starlite_saqlalchemy.exceptions import NotFoundError from tests.utils import domain if TYPE_CHECKING: @@ -130,3 +131,17 @@ async def test_service_new_context_manager() -> None: """Simple test of `Service.new()` context manager behavior.""" async with service.Service[domain.authors.Author].new() as service_obj: assert isinstance(service_obj, service.Service) + + +async def test_service_method_default_behavior() -> None: + """Test default behavior of base service methods.""" + service_obj = service.Service[object]() + data = object() + assert await service_obj.create(data) is data + assert await service_obj.list() == [] + assert await service_obj.update("abc", data) is data + assert await service_obj.upsert("abc", data) is data + with pytest.raises(NotFoundError): + await service_obj.get("abc") + with pytest.raises(NotFoundError): + await service_obj.delete("abc")