Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
fix: remove NotImplementedError from Service base class. (#222)
Browse files Browse the repository at this point in the history
This requires the methods to have a default functionality, so:

- list method returns an empty list
- create, update/upsert methods return the object, untouched.
- get and delete methods raise NotFoundError

closes #179
  • Loading branch information
peterschutt committed Jan 11, 2023
1 parent 818cc22 commit b812f1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/starlite_saqlalchemy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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`.
Expand All @@ -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`.
Expand All @@ -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`.
Expand All @@ -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`.
Expand All @@ -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_`
Expand All @@ -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_`.
Expand All @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")

0 comments on commit b812f1c

Please sign in to comment.