Skip to content

Commit

Permalink
modify(inMemoryRepository): variabilize exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Saucisse à roulettes committed Jun 11, 2023
1 parent ad9be69 commit 73c6d64
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 55 deletions.
2 changes: 1 addition & 1 deletion ppa/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .in_memory import in_memory_repository
from .in_memory import in_memory_repository
90 changes: 48 additions & 42 deletions ppa/in_memory.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
from typing import Any, Type


def in_memory_repository(cls, entity_already_exists_exception: Type[Exception], entity_not_found_exception: Type[Exception]) -> Type:

e_already_exists = entity_already_exists_exception
e_not_found = entity_not_found_exception
class InMemoryRepository:
find_by_fields: list[str] = ["id"]
entity_already_exists_exception: Type[Exception] = e_already_exists
entity_not_found_exception: Type[Exception] = e_not_found

def __init__(self) -> None:
self._store: dict[str, Any] = {}
for key in self.find_by_fields:
setattr(self, f"find_by_{key}", self._make_find_by_method(key))

def add(self, entity: Any) -> Any:
if entity.id in self._store:
raise self.entity_already_exists_exception()
self._store[entity.id] = entity

def retrieve(self, id_: Any) -> Any:
try:
return self._store[id_]
except KeyError as err:
raise self.entity_not_found_exception() from err

def update(self, entity: Any) -> None:
if entity.id not in self._store:
raise self.entity_not_found_exception()
self._store[entity.id] = entity

def delete(self, id_: Any) -> None:
if id_ not in self._store:
raise self.entity_not_found_exception(id_)
del self._store[id_]

def _make_find_by_method(self, field_name: str) -> Any:
def find_by_method(value: Any) -> list[Any]:
return [entity for id_, entity in self._store.items() if getattr(entity, field_name) == value]

return find_by_method

return InMemoryRepository
def in_memory_repository(
entity_already_exists_exception: Type[Exception] = ValueError,
entity_not_found_exception: Type[Exception] = ValueError,
):
def decorator(cls) -> Type:
e_already_exists = entity_already_exists_exception
e_not_found = entity_not_found_exception

class InMemoryRepository(cls):
find_by_fields: list[str] = ["id"]
entity_already_exists_exception: Type[Exception] = e_already_exists
entity_not_found_exception: Type[Exception] = e_not_found

def __init__(self) -> None:
self._store: dict[str, Any] = {}
for key in self.find_by_fields:
setattr(self, f"find_by_{key}", self._make_find_by_method(key))

def add(self, entity: Any) -> Any:
if entity.id in self._store:
raise self.entity_already_exists_exception()
self._store[entity.id] = entity

def retrieve(self, id_: Any) -> Any:
try:
return self._store[id_]
except KeyError as err:
raise self.entity_not_found_exception() from err

def update(self, entity: Any) -> None:
if entity.id not in self._store:
raise self.entity_not_found_exception()
self._store[entity.id] = entity

def delete(self, id_: Any) -> None:
if id_ not in self._store:
raise self.entity_not_found_exception(id_)
del self._store[id_]

def _make_find_by_method(self, field_name: str) -> Any:
def find_by_method(value: Any) -> list[Any]:
return [entity for id_, entity in self._store.items() if getattr(entity, field_name) == value]

return find_by_method

return InMemoryRepository

return decorator
23 changes: 11 additions & 12 deletions ppa/test/test_in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,25 @@ def __init__(self, id_: int, name: str):


@pytest.fixture
def UserRepository(User):
@in_memory_repository
def user_repository():
@in_memory_repository()
class UserRepository:
pass

return UserRepository()


@pytest.mark.parametrize("entity_id, entity_name", [(1, "John"), (2, "Alice"), (3, "Bob")])
def test_in_memory_behavior(UserRepository, User, entity_id, entity_name):
user = User(id_=entity_id, name=entity_name)
def test_in_memory_repository(user_repository, User):
user = User(id_=1, name="John")

UserRepository.add(user)
retrieved_user = UserRepository.retrieve(entity_id)
user_repository.add(user)
retrieved_user = user_repository.retrieve(1)
assert retrieved_user == user

updated_user = User(id_=entity_id, name="Updated Name")
UserRepository.update(updated_user)
assert UserRepository.retrieve(entity_id) == updated_user
updated_user = User(id_=1, name="Johny")
user_repository.update(updated_user)
assert user_repository.retrieve(1) == updated_user

UserRepository.delete(entity_id)
user_repository.delete(1)
with pytest.raises(ValueError):
UserRepository.retrieve(entity_id)
user_repository.retrieve(1)

0 comments on commit 73c6d64

Please sign in to comment.