Add a CRUD Repo test helper to handle basic crud repo testing
This commit is contained in:
parent
da40febc7f
commit
0d884e5836
2 changed files with 85 additions and 21 deletions
56
src/infra/db_tests.py
Normal file
56
src/infra/db_tests.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
from sqlalchemy import Engine
|
||||
from typing import Callable
|
||||
from src.infra import CRUDRepo, Base
|
||||
import pytest
|
||||
|
||||
|
||||
class CRUDRepoHelper[A: Base, B, C]:
|
||||
@pytest.fixture
|
||||
def repo(self, db: Engine) -> CRUDRepo[A, B, C]:
|
||||
import pdb
|
||||
|
||||
pdb.set_trace()
|
||||
raise NotImplementedError()
|
||||
|
||||
@pytest.fixture
|
||||
def dto(self) -> C:
|
||||
raise NotImplementedError()
|
||||
|
||||
@pytest.fixture
|
||||
def item(self, repo: CRUDRepo[A, B, C], dto: C) -> B:
|
||||
return repo.create(dto, None)
|
||||
|
||||
@pytest.fixture
|
||||
def transform(self) -> Callable[[B], B]:
|
||||
raise NotImplementedError
|
||||
|
||||
@pytest.fixture
|
||||
def expected_transformed_value(self, item: B) -> B:
|
||||
raise NotImplementedError()
|
||||
|
||||
def test_create(self, item: B):
|
||||
assert item is not None
|
||||
|
||||
def test_create_returns_none_if_invalid(self, repo: CRUDRepo[A, B, C], dto: C):
|
||||
def always_invalid(*_args, **_kw):
|
||||
raise ValueError('test')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
repo.create(dto, always_invalid)
|
||||
|
||||
def test_create_returns_a_value_if_valid(self, repo: CRUDRepo[A, B, C], dto: C):
|
||||
assert repo.create(dto, lambda _: None) is not None
|
||||
|
||||
def test_get_returns_none_when_missing(self, repo: CRUDRepo[A, B, C]):
|
||||
assert repo.get(42) is None
|
||||
|
||||
def test_get_returns_a_value_if_it_exists(self, repo: CRUDRepo[A, B, C], item: B):
|
||||
assert repo.get(1) == item
|
||||
|
||||
def test_update(self, repo: CRUDRepo[A, B, C], transform: Callable[[B], B], item: B, expected_transformed_value: B):
|
||||
assert repo.update(1, transform) == expected_transformed_value
|
||||
|
||||
def test_delete(self, repo: CRUDRepo[A, B, C], item: B):
|
||||
assert repo.get(1) is not None
|
||||
repo.delete(1, None)
|
||||
assert repo.get(1) is None
|
||||
Loading…
Add table
Add a link
Reference in a new issue