|
|
@@ -3,9 +3,13 @@ from typing import cast
|
|
|
import pytest
|
|
|
from pydantic import Field
|
|
|
|
|
|
+from clean_python import Conflict
|
|
|
+from clean_python import DoesNotExist
|
|
|
from clean_python import Filter
|
|
|
+from clean_python import Id
|
|
|
from clean_python import InMemoryGateway
|
|
|
from clean_python import InternalGateway
|
|
|
+from clean_python import Json
|
|
|
from clean_python import Manage
|
|
|
from clean_python import Repository
|
|
|
from clean_python import RootEntity
|
|
|
@@ -26,6 +30,11 @@ class 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)
|
|
|
+
|
|
|
|
|
|
# domain - this module
|
|
|
class UserObj(ValueObject):
|
|
|
@@ -120,3 +129,30 @@ async def test_exists_2(internal_gateway_with_record: UserGateway):
|
|
|
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 == UserObj(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"})
|