test_sync_gateway.py 4.9 KB

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