test_gateway.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from datetime import datetime
  2. from datetime import timezone
  3. from unittest import mock
  4. import pytest
  5. from clean_python import AlreadyExists
  6. from clean_python import Conflict
  7. from clean_python import DoesNotExist
  8. from clean_python import Filter
  9. from clean_python import InMemoryGateway
  10. from clean_python import PageOptions
  11. @pytest.fixture
  12. def in_memory_gateway():
  13. return InMemoryGateway(
  14. data=[
  15. {"id": 1, "name": "a"},
  16. {"id": 2, "name": "b"},
  17. {"id": 3, "name": "c"},
  18. ]
  19. )
  20. async def test_get(in_memory_gateway):
  21. actual = await in_memory_gateway.get(1)
  22. assert actual == in_memory_gateway.data[1]
  23. async def test_get_none(in_memory_gateway):
  24. actual = await in_memory_gateway.get(4)
  25. assert actual is None
  26. async def test_add(in_memory_gateway):
  27. record = {"id": 5, "name": "d"}
  28. await in_memory_gateway.add(record)
  29. assert in_memory_gateway.data[5] == record
  30. async def test_add_id_autoincrement(in_memory_gateway):
  31. record = {"name": "d"}
  32. await in_memory_gateway.add(record)
  33. assert in_memory_gateway.data[4] == {"id": 4, "name": "d"}
  34. async def test_add_id_exists(in_memory_gateway):
  35. with pytest.raises(AlreadyExists):
  36. await in_memory_gateway.add({"id": 3})
  37. async def test_update(in_memory_gateway):
  38. record = {"id": 3, "name": "d"}
  39. await in_memory_gateway.update(record)
  40. assert in_memory_gateway.data[3] == record
  41. async def test_update_no_id(in_memory_gateway):
  42. with pytest.raises(DoesNotExist):
  43. await in_memory_gateway.update({"no": "id"})
  44. async def test_update_does_not_exist(in_memory_gateway):
  45. with pytest.raises(DoesNotExist):
  46. await in_memory_gateway.update({"id": 4})
  47. async def test_upsert(in_memory_gateway):
  48. record = {"id": 3, "name": "d"}
  49. await in_memory_gateway.upsert(record)
  50. assert in_memory_gateway.data[3] == record
  51. async def test_upsert_no_id(in_memory_gateway):
  52. await in_memory_gateway.upsert({"name": "x"})
  53. assert in_memory_gateway.data[4] == {"id": 4, "name": "x"}
  54. async def test_upsert_does_add(in_memory_gateway):
  55. await in_memory_gateway.upsert({"id": 4, "name": "x"})
  56. assert in_memory_gateway.data[4] == {"id": 4, "name": "x"}
  57. async def test_remove(in_memory_gateway):
  58. assert await in_memory_gateway.remove(1)
  59. assert 1 not in in_memory_gateway.data
  60. assert len(in_memory_gateway.data) == 2
  61. async def test_remove_not_existing(in_memory_gateway):
  62. assert not await in_memory_gateway.remove(4)
  63. assert len(in_memory_gateway.data) == 3
  64. async def test_updated_if_unmodified_since(in_memory_gateway):
  65. existing = {"id": 4, "name": "e", "updated_at": datetime.now(timezone.utc)}
  66. new = {"id": 4, "name": "f", "updated_at": datetime.now(timezone.utc)}
  67. await in_memory_gateway.add(existing)
  68. await in_memory_gateway.update(new, if_unmodified_since=existing["updated_at"])
  69. assert in_memory_gateway.data[4]["name"] == "f"
  70. @pytest.mark.parametrize(
  71. "if_unmodified_since", [datetime.now(timezone.utc), datetime(2010, 1, 1)]
  72. )
  73. async def test_update_if_unmodified_since_not_ok(
  74. in_memory_gateway, if_unmodified_since
  75. ):
  76. existing = {"id": 4, "name": "e", "updated_at": datetime.now(timezone.utc)}
  77. new = {"id": 4, "name": "f", "updated_at": datetime.now(timezone.utc)}
  78. await in_memory_gateway.add(existing)
  79. with pytest.raises(Conflict):
  80. await in_memory_gateway.update(new, if_unmodified_since=if_unmodified_since)
  81. async def test_filter_all(in_memory_gateway):
  82. actual = await in_memory_gateway.filter([])
  83. assert actual == sorted(in_memory_gateway.data.values(), key=lambda x: x["id"])
  84. async def test_filter_all_with_params(in_memory_gateway):
  85. actual = await in_memory_gateway.filter(
  86. [], params=PageOptions(limit=2, offset=1, order_by="id", ascending=False)
  87. )
  88. assert [x["id"] for x in actual] == [2, 1]
  89. async def test_filter(in_memory_gateway):
  90. actual = await in_memory_gateway.filter([Filter(field="name", values=["b"])])
  91. assert actual == [in_memory_gateway.data[2]]
  92. async def test_count_all(in_memory_gateway):
  93. actual = await in_memory_gateway.count([])
  94. assert actual == 3
  95. async def test_count_with_filter(in_memory_gateway):
  96. actual = await in_memory_gateway.count([Filter(field="name", values=["b"])])
  97. assert actual == 1
  98. @mock.patch.object(InMemoryGateway, "update")
  99. async def test_update_transactional(update):
  100. record = {"id": 3, "name": "d", "updated_at": datetime(2010, 1, 1)}
  101. gateway = InMemoryGateway([record])
  102. await gateway.update_transactional(3, lambda x: {"name": x["name"] + "x"})
  103. update.assert_awaited_once_with(
  104. {"name": "dx"}, if_unmodified_since=datetime(2010, 1, 1)
  105. )
  106. async def test_update_transactional_does_not_exist(in_memory_gateway):
  107. with pytest.raises(DoesNotExist):
  108. await in_memory_gateway.update_transactional(5, lambda x: x)
  109. async def test_exists_all(in_memory_gateway):
  110. assert await in_memory_gateway.exists([])
  111. async def test_exists_with_filter(in_memory_gateway):
  112. assert await in_memory_gateway.exists([Filter(field="name", values=["b"])])
  113. async def test_exists_with_filter_not(in_memory_gateway):
  114. assert not await in_memory_gateway.exists([Filter(field="name", values=["bb"])])