test_internal_gateway.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import pytest
  2. from pydantic import Field
  3. from clean_python import Conflict
  4. from clean_python import DoesNotExist
  5. from clean_python import Filter
  6. from clean_python import Id
  7. from clean_python import InMemoryGateway
  8. from clean_python import InternalGateway
  9. from clean_python import Json
  10. from clean_python import Manage
  11. from clean_python import Repository
  12. from clean_python import RootEntity
  13. # domain - other module
  14. class User(RootEntity):
  15. name: str = Field(min_length=1)
  16. class UserRepository(Repository[User]):
  17. pass
  18. # application - other module
  19. class ManageUser(Manage[User]):
  20. def __init__(self):
  21. self.repo = UserRepository(gateway=InMemoryGateway([]))
  22. async def update(self, id: Id, values: Json) -> User:
  23. if values.get("name") == "conflict":
  24. raise Conflict()
  25. return await self.repo.update(id, values)
  26. # infrastructure - this module
  27. class UserGateway(InternalGateway[User]):
  28. def __init__(self, manage: ManageUser):
  29. self._manage = manage
  30. @property
  31. def manage(self) -> ManageUser:
  32. return self._manage
  33. def to_internal(self, obj: User) -> Json:
  34. return dict(id=obj.id, name=obj.name)
  35. @pytest.fixture
  36. def internal_gateway():
  37. return UserGateway(manage=ManageUser())
  38. async def test_get_not_existing(internal_gateway: UserGateway):
  39. assert await internal_gateway.get(1) is None
  40. async def test_add(internal_gateway: UserGateway):
  41. actual = await internal_gateway.add(dict(id=12, name="foo"))
  42. assert actual == dict(id=12, name="foo")
  43. @pytest.fixture
  44. async def internal_gateway_with_record(internal_gateway):
  45. await internal_gateway.add(dict(id=12, name="foo"))
  46. return internal_gateway
  47. async def test_get(internal_gateway_with_record):
  48. assert await internal_gateway_with_record.get(12) == dict(id=12, name="foo")
  49. async def test_filter(internal_gateway_with_record: UserGateway):
  50. assert await internal_gateway_with_record.filter([]) == [dict(id=12, name="foo")]
  51. async def test_filter_2(internal_gateway_with_record: UserGateway):
  52. assert (
  53. await internal_gateway_with_record.filter([Filter(field="id", values=[1])])
  54. == []
  55. )
  56. async def test_remove(internal_gateway_with_record: UserGateway):
  57. assert await internal_gateway_with_record.remove(12)
  58. assert internal_gateway_with_record.manage.repo.gateway.data == {}
  59. async def test_remove_does_not_exist(internal_gateway: UserGateway):
  60. assert not await internal_gateway.remove(12)
  61. async def test_add_bad_request(internal_gateway: UserGateway):
  62. # a 'bad request' should be reraised as a ValueError; errors in gateways
  63. # are an internal affair.
  64. with pytest.raises(ValueError):
  65. await internal_gateway.add(dict(id=12, name=""))
  66. async def test_count(internal_gateway_with_record: UserGateway):
  67. assert await internal_gateway_with_record.count([]) == 1
  68. async def test_count_2(internal_gateway_with_record: UserGateway):
  69. assert (
  70. await internal_gateway_with_record.count([Filter(field="id", values=[1])]) == 0
  71. )
  72. async def test_exists(internal_gateway_with_record: UserGateway):
  73. assert await internal_gateway_with_record.exists([]) is True
  74. async def test_exists_2(internal_gateway_with_record: UserGateway):
  75. assert (
  76. await internal_gateway_with_record.exists([Filter(field="id", values=[1])])
  77. is False
  78. )
  79. async def test_update(internal_gateway_with_record):
  80. updated = await internal_gateway_with_record.update({"id": 12, "name": "bar"})
  81. assert updated == dict(id=12, name="bar")
  82. @pytest.mark.parametrize(
  83. "values", [{"id": 12, "name": "bar"}, {"id": None, "name": "bar"}, {"name": "bar"}]
  84. )
  85. async def test_update_does_not_exist(internal_gateway, values):
  86. with pytest.raises(DoesNotExist):
  87. assert await internal_gateway.update(values)
  88. async def test_update_bad_request(internal_gateway_with_record):
  89. # a 'bad request' should be reraised as a ValueError; errors in gateways
  90. # are an internal affair.
  91. with pytest.raises(ValueError):
  92. assert await internal_gateway_with_record.update({"id": 12, "name": ""})
  93. async def test_update_conflict(internal_gateway_with_record):
  94. # a 'conflict' should bubble through the internal gateway
  95. with pytest.raises(Conflict):
  96. assert await internal_gateway_with_record.update({"id": 12, "name": "conflict"})