| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 | import pytestfrom pydantic import Fieldfrom clean_python import Conflictfrom clean_python import DoesNotExistfrom clean_python import Filterfrom clean_python import Idfrom clean_python import InMemoryGatewayfrom clean_python import InternalGatewayfrom clean_python import Jsonfrom clean_python import Managefrom clean_python import Repositoryfrom clean_python import RootEntity# domain - other moduleclass User(RootEntity):    name: str = Field(min_length=1)class UserRepository(Repository[User]):    pass# application - other moduleclass ManageUser(Manage[User]):    def __init__(self):        self.repo = UserRepository(gateway=InMemoryGateway([]))    async def update(self, id: Id, values: Json) -> User:        if values.get("name") == "conflict":            raise Conflict()        return await self.repo.update(id, values)# infrastructure - this moduleclass UserGateway(InternalGateway[User]):    def __init__(self, manage: ManageUser):        self._manage = manage    @property    def manage(self) -> ManageUser:        return self._manage    def to_internal(self, obj: User) -> Json:        return dict(id=obj.id, name=obj.name)@pytest.fixturedef internal_gateway():    return UserGateway(manage=ManageUser())async def test_get_not_existing(internal_gateway: UserGateway):    assert await internal_gateway.get(1) is Noneasync def test_add(internal_gateway: UserGateway):    actual = await internal_gateway.add(dict(id=12, name="foo"))    assert actual == dict(id=12, name="foo")@pytest.fixtureasync def internal_gateway_with_record(internal_gateway):    await internal_gateway.add(dict(id=12, name="foo"))    return internal_gatewayasync def test_get(internal_gateway_with_record):    assert await internal_gateway_with_record.get(12) == dict(id=12, name="foo")async def test_filter(internal_gateway_with_record: UserGateway):    assert await internal_gateway_with_record.filter([]) == [dict(id=12, name="foo")]async def test_filter_2(internal_gateway_with_record: UserGateway):    assert (        await internal_gateway_with_record.filter([Filter(field="id", values=[1])])        == []    )async def test_remove(internal_gateway_with_record: UserGateway):    assert await internal_gateway_with_record.remove(12)    assert internal_gateway_with_record.manage.repo.gateway.data == {}async def test_remove_does_not_exist(internal_gateway: UserGateway):    assert not await internal_gateway.remove(12)async def test_add_bad_request(internal_gateway: UserGateway):    # a 'bad request' should be reraised as a ValueError; errors in gateways    # are an internal affair.    with pytest.raises(ValueError):        await internal_gateway.add(dict(id=12, name=""))async def test_count(internal_gateway_with_record: UserGateway):    assert await internal_gateway_with_record.count([]) == 1async def test_count_2(internal_gateway_with_record: UserGateway):    assert (        await internal_gateway_with_record.count([Filter(field="id", values=[1])]) == 0    )async def test_exists(internal_gateway_with_record: UserGateway):    assert await internal_gateway_with_record.exists([]) is Trueasync def test_exists_2(internal_gateway_with_record: UserGateway):    assert (        await internal_gateway_with_record.exists([Filter(field="id", values=[1])])        is False    )async def test_update(internal_gateway_with_record):    updated = await internal_gateway_with_record.update({"id": 12, "name": "bar"})    assert updated == dict(id=12, name="bar")@pytest.mark.parametrize(    "values", [{"id": 12, "name": "bar"}, {"id": None, "name": "bar"}, {"name": "bar"}])async def test_update_does_not_exist(internal_gateway, values):    with pytest.raises(DoesNotExist):        assert await internal_gateway.update(values)async def test_update_bad_request(internal_gateway_with_record):    # a 'bad request' should be reraised as a ValueError; errors in gateways    # are an internal affair.    with pytest.raises(ValueError):        assert await internal_gateway_with_record.update({"id": 12, "name": ""})async def test_update_conflict(internal_gateway_with_record):    # a 'conflict' should bubble through the internal gateway    with pytest.raises(Conflict):        assert await internal_gateway_with_record.update({"id": 12, "name": "conflict"})
 |