test_sync_repository.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # This module is a copy paste of test_repository.py
  2. from typing import List
  3. from unittest import mock
  4. import pytest
  5. from clean_python import BadRequest
  6. from clean_python import DoesNotExist
  7. from clean_python import Filter
  8. from clean_python import InMemorySyncGateway
  9. from clean_python import Page
  10. from clean_python import PageOptions
  11. from clean_python import RootEntity
  12. from clean_python import SyncRepository
  13. class User(RootEntity):
  14. name: str
  15. @pytest.fixture
  16. def users():
  17. return [
  18. User.create(id=1, name="a"),
  19. User.create(id=2, name="b"),
  20. User.create(id=3, name="c"),
  21. ]
  22. class UserSyncRepository(SyncRepository[User]):
  23. pass
  24. @pytest.fixture
  25. def user_repository(users: List[User]):
  26. return UserSyncRepository(
  27. gateway=InMemorySyncGateway(data=[x.model_dump() for x in users])
  28. )
  29. @pytest.fixture
  30. def page_options():
  31. return PageOptions(limit=10, offset=0, order_by="id")
  32. def test_entity_attr(user_repository):
  33. assert user_repository.entity is User
  34. def test_get(user_repository):
  35. actual = user_repository.get(1)
  36. assert actual.name == "a"
  37. def test_get_does_not_exist(user_repository):
  38. with pytest.raises(DoesNotExist):
  39. user_repository.get(4)
  40. @mock.patch.object(SyncRepository, "filter")
  41. def test_all(filter_m, user_repository, page_options):
  42. filter_m.return_value = Page(total=0, items=[])
  43. assert user_repository.all(page_options) is filter_m.return_value
  44. filter_m.assert_called_once_with([], params=page_options)
  45. def test_add(user_repository: UserSyncRepository):
  46. actual = user_repository.add(User.create(name="d"))
  47. assert actual.name == "d"
  48. assert user_repository.gateway.data[4] == actual.model_dump()
  49. def test_add_json(user_repository: UserSyncRepository):
  50. actual = user_repository.add({"name": "d"})
  51. assert actual.name == "d"
  52. assert user_repository.gateway.data[4] == actual.model_dump()
  53. def test_add_json_validates(user_repository: UserSyncRepository):
  54. with pytest.raises(BadRequest):
  55. user_repository.add({"id": "d"})
  56. def test_update(user_repository: UserSyncRepository):
  57. actual = user_repository.update(id=2, values={"name": "d"})
  58. assert actual.name == "d"
  59. assert user_repository.gateway.data[2] == actual.model_dump()
  60. def test_update_does_not_exist(user_repository: UserSyncRepository):
  61. with pytest.raises(DoesNotExist):
  62. user_repository.update(id=4, values={"name": "d"})
  63. def test_update_validates(user_repository: UserSyncRepository):
  64. with pytest.raises(BadRequest):
  65. user_repository.update(id=2, values={"id": 6})
  66. def test_remove(user_repository: UserSyncRepository):
  67. assert user_repository.remove(2)
  68. assert 2 not in user_repository.gateway.data
  69. def test_remove_does_not_exist(user_repository: UserSyncRepository):
  70. assert not user_repository.remove(4)
  71. def test_upsert_updates(user_repository: UserSyncRepository):
  72. actual = user_repository.upsert(User.create(id=2, name="d"))
  73. assert actual.name == "d"
  74. assert user_repository.gateway.data[2] == actual.model_dump()
  75. def test_upsert_adds(user_repository: UserSyncRepository):
  76. actual = user_repository.upsert(User.create(id=4, name="d"))
  77. assert actual.name == "d"
  78. assert user_repository.gateway.data[4] == actual.model_dump()
  79. @mock.patch.object(InMemorySyncGateway, "count")
  80. def test_filter(count_m, user_repository: UserSyncRepository, users):
  81. actual = user_repository.filter([Filter(field="name", values=["b"])])
  82. assert actual == Page(total=1, items=[users[1]], limit=None, offest=None)
  83. assert not count_m.called
  84. @mock.patch.object(InMemorySyncGateway, "count")
  85. def test_filter_with_pagination(
  86. count_m, user_repository: UserSyncRepository, users, page_options
  87. ):
  88. actual = user_repository.filter([Filter(field="name", values=["b"])], page_options)
  89. assert actual == Page(
  90. total=1, items=[users[1]], limit=page_options.limit, offset=page_options.offset
  91. )
  92. assert not count_m.called
  93. @pytest.mark.parametrize(
  94. "page_options",
  95. [
  96. PageOptions(limit=3, offset=0, order_by="id"),
  97. PageOptions(limit=10, offset=1, order_by="id"),
  98. ],
  99. )
  100. @mock.patch.object(InMemorySyncGateway, "count")
  101. def test_filter_with_pagination_calls_count(
  102. count_m, user_repository: UserSyncRepository, users, page_options
  103. ):
  104. count_m.return_value = 123
  105. actual = user_repository.filter([], page_options)
  106. assert actual == Page(
  107. total=count_m.return_value,
  108. items=users[page_options.offset :],
  109. limit=page_options.limit,
  110. offset=page_options.offset,
  111. )
  112. assert count_m.called
  113. @mock.patch.object(SyncRepository, "filter")
  114. def test_by(filter_m, user_repository: UserSyncRepository, page_options):
  115. filter_m.return_value = Page(total=0, items=[])
  116. assert user_repository.by("name", "b", page_options) is filter_m.return_value
  117. filter_m.assert_called_once_with(
  118. [Filter(field="name", values=["b"])], params=page_options
  119. )
  120. @mock.patch.object(InMemorySyncGateway, "count")
  121. def test_count(gateway_count, user_repository):
  122. assert user_repository.count("foo") is gateway_count.return_value
  123. gateway_count.assert_called_once_with("foo")
  124. @mock.patch.object(InMemorySyncGateway, "exists")
  125. def test_exists(gateway_exists, user_repository):
  126. assert user_repository.exists("foo") is gateway_exists.return_value
  127. gateway_exists.assert_called_once_with("foo")