test_internal_gateway.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from typing import cast
  2. import pytest
  3. from pydantic import Field
  4. from clean_python import (
  5. Filter,
  6. InMemoryGateway,
  7. InternalGateway,
  8. Manage,
  9. Repository,
  10. RootEntity,
  11. ValueObject,
  12. )
  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. # domain - this module
  23. class UserObj(ValueObject):
  24. id: int
  25. name: str
  26. # infrastructure - this module
  27. class UserGateway(InternalGateway[User, UserObj]):
  28. def __init__(self, manage: ManageUser):
  29. self._manage = manage
  30. @property
  31. def manage(self) -> ManageUser:
  32. return self._manage
  33. def _map(self, obj: User) -> UserObj:
  34. return UserObj(id=cast(int, 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(UserObj(id=12, name="foo"))
  42. assert actual == UserObj(id=12, name="foo")
  43. @pytest.fixture
  44. async def internal_gateway_with_record(internal_gateway):
  45. await internal_gateway.add(UserObj(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) == UserObj(id=12, name="foo")
  49. async def test_filter(internal_gateway_with_record: UserGateway):
  50. assert await internal_gateway_with_record.filter([]) == [UserObj(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(UserObj(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. )