Skip to content

Commit

Permalink
enhance(inMemoryRepository): move configuration from class attributes…
Browse files Browse the repository at this point in the history
… to decorator parameters.

* enhance(inMemoryRepository): move configuration from class attributes to decorator parameters.
implements tests to cover most important use cases

* fix(inMemoryRepository): mypy errors

* rename(Package): from ppa to src to match CI configuration
  • Loading branch information
Saucisse à roulettes committed Jun 14, 2023
1 parent 73c6d64 commit 0febb52
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 52 deletions.
38 changes: 0 additions & 38 deletions ppa/test/test_in_memory.py

This file was deleted.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.3.0"
description = ""
authors = ["Saucisse À Roulette <gael.monachon.dev@gmail.com>"]
readme = "README.md"
packages = [{include = "ppa"}]
packages = [{include = "src"}]

[tool.poetry.dependencies]
python = "^3.11"
Expand All @@ -27,4 +27,4 @@ include = '\.pyi?$'

[tool.mypy]
python_version = 3.11
files = "ppa/"
files = "src/"
File renamed without changes.
19 changes: 7 additions & 12 deletions ppa/in_memory.py → src/in_memory.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
from collections.abc import Iterable
from typing import Any, Type


def in_memory_repository(
find_by_fields: Iterable[str] | None = None,
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:
for key in find_by_fields or []:
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()
raise 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
raise 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()
raise 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_)
raise entity_not_found_exception()
del self._store[id_]

def _make_find_by_method(self, field_name: str) -> Any:
Expand Down
File renamed without changes.
File renamed without changes.
91 changes: 91 additions & 0 deletions src/test/test_in_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from typing import Any

import pytest

from src.in_memory import in_memory_repository


class User:
def __init__(self, id_: int, name: str):
self.id = id_
self.name = name


@pytest.fixture
def user():
return User(1, "john")


@in_memory_repository(find_by_fields=[])
class UserRepository:
pass


@pytest.fixture
def user_repository():
return UserRepository()


def test_in_memory_repository_find_by_fields(user: User):
@in_memory_repository(find_by_fields=("id", "name"))
class FieldsRepository:
pass

rep: Any = FieldsRepository()

rep.add(user)

assert rep.find_by_id(user.id) == [user]
assert rep.find_by_name(user.name) == [user]


def test_in_memory_repository_exceptions(user: User):
class CustomException1(Exception):
pass

class CustomException2(Exception):
pass

@in_memory_repository(
entity_already_exists_exception=CustomException1,
entity_not_found_exception=CustomException2,
)
class ExceptionsRepository:
pass

rep: Any = ExceptionsRepository()

with pytest.raises(CustomException2):
rep.delete(user.id)

rep.add(user)
with pytest.raises(CustomException1):
rep.add(user)


def test_in_memory_repository_methods(user_repository: Any, user: User):
user_repository.add(user)
assert user_repository.retrieve(user.id) == user
updated_user = User(id_=user.id, name="john_updated")
user_repository.update(updated_user)
modified_user = user_repository.retrieve(user.id)
assert modified_user.id == updated_user.id and modified_user.name == updated_user.name
user_repository.delete(user.id)
with pytest.raises(ValueError):
assert user_repository.retrieve(user.id)


def test_in_memory_repository_add_already_exists(user_repository: Any, user: User):
user_repository.add(user)
with pytest.raises(ValueError):
user_repository.add(user)


def test_in_memory_repository_update_not_found(user_repository: Any, user: User):
with pytest.raises(ValueError):
user_repository.update(user)


def test_in_memory_repository_delete_not_found(user_repository: Any, user: User):
with pytest.raises(ValueError):
user_repository.delete(user)

0 comments on commit 0febb52

Please sign in to comment.