Skip to content

Commit d167a82

Browse files
committed
make sync test cases reusable for cluster
1 parent a5875e4 commit d167a82

File tree

8 files changed

+169
-1231
lines changed

8 files changed

+169
-1231
lines changed

tests/conftest.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import copy
12
from collections.abc import Iterable
3+
from typing import cast
24

35
import pytest
46
import pytest_asyncio
7+
from pytest_django.fixtures import SettingsWrapper
58

69
from asgiref.compatibility import iscoroutinefunction
7-
from django.core.cache import cache as default_cache
10+
from django.core.cache import cache as default_cache, caches
811

912
from django_valkey.base import BaseValkeyCache
10-
13+
from django_valkey.cache import ValkeyCache
1114

1215
# for some reason `isawaitable` doesn't work here
1316
if iscoroutinefunction(default_cache.clear):
@@ -23,3 +26,20 @@ async def cache():
2326
def cache() -> Iterable[BaseValkeyCache]:
2427
yield default_cache
2528
default_cache.clear()
29+
30+
31+
@pytest.fixture
32+
def key_prefix_cache(
33+
cache: ValkeyCache, settings: SettingsWrapper
34+
) -> Iterable[ValkeyCache]:
35+
caches_setting = copy.deepcopy(settings.CACHES)
36+
caches_setting["default"]["KEY_PREFIX"] = "*"
37+
settings.CACHES = caches_setting
38+
yield cache
39+
40+
41+
@pytest.fixture
42+
def with_prefix_cache() -> Iterable[ValkeyCache]:
43+
with_prefix = cast(ValkeyCache, caches["with_prefix"])
44+
yield with_prefix
45+
with_prefix.clear()

tests/test_backend.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from django_valkey.cache import ValkeyCache
1717
from django_valkey.client import ShardClient, herd
18+
from django_valkey.cluster_cache.client import DefaultClusterClient
1819
from django_valkey.serializers.json import JSONSerializer
1920
from django_valkey.serializers.msgpack import MSGPackSerializer
2021
from django_valkey.serializers.pickle import PickleSerializer
@@ -99,7 +100,7 @@ def test_unicode_keys(self, cache: ValkeyCache):
99100
res = cache.get("ключ")
100101
assert res == "value"
101102

102-
def test_save_and_integer(self, cache: ValkeyCache):
103+
def test_save_an_integer(self, cache: ValkeyCache):
103104
cache.set("test_key", 2)
104105
res = cache.get("test_key", "Foo")
105106

@@ -223,7 +224,9 @@ def test_get_many(self, cache: ValkeyCache):
223224
assert res == {"a": 1, "b": 2, "c": 3}
224225

225226
def test_mget(self, cache: ValkeyCache):
226-
if isinstance(cache.client, ShardClient):
227+
if isinstance(cache.client, ShardClient) or isinstance(
228+
cache.client, DefaultClusterClient
229+
):
227230
pytest.skip()
228231
cache.set("a", 1)
229232
cache.set("b", 2)
@@ -240,13 +243,28 @@ def test_get_many_unicode(self, cache: ValkeyCache):
240243
res = cache.get_many(["a", "ب", "c"])
241244
assert res == {"a": "1", "ب": "2", "c": "الف"}
242245

246+
def test_mget_unicode(self, cache: ValkeyCache):
247+
if isinstance(cache.client, ShardClient) or isinstance(
248+
cache.client, DefaultClusterClient
249+
):
250+
pytest.skip()
251+
252+
cache.set("fooa", "1")
253+
cache.set("fooب", "2")
254+
cache.set("fooc", "الف")
255+
256+
res = cache.mget(["fooa", "fooب", "fooc"])
257+
assert res == {"fooa": "1", "fooب": "2", "fooc": "الف"}
258+
243259
def test_set_many(self, cache: ValkeyCache):
244260
cache.set_many({"a": 1, "b": 2, "c": 3})
245261
res = cache.get_many(["a", "b", "c"])
246262
assert res == {"a": 1, "b": 2, "c": 3}
247263

248264
def test_mset(self, cache: ValkeyCache):
249-
if isinstance(cache.client, ShardClient):
265+
if isinstance(cache.client, ShardClient) or isinstance(
266+
cache.client, DefaultClusterClient
267+
):
250268
pytest.skip()
251269
cache.mset({"a": 1, "b": 2, "c": 3})
252270
res = cache.mget(["a", "b", "c"])
@@ -518,6 +536,9 @@ def test_ttl_incr_version_no_timeout(self, cache: ValkeyCache):
518536
assert my_value == "hello world!"
519537

520538
def test_delete_pattern(self, cache: ValkeyCache):
539+
if isinstance(cache.client, DefaultClusterClient):
540+
pytest.skip("cluster client has a specific test")
541+
521542
for key in ["foo-aa", "foo-ab", "foo-bb", "foo-bc"]:
522543
cache.set(key, "foo")
523544

@@ -532,6 +553,9 @@ def test_delete_pattern(self, cache: ValkeyCache):
532553

533554
@patch("django_valkey.cache.ValkeyCache.client")
534555
def test_delete_pattern_with_custom_count(self, client_mock, cache: ValkeyCache):
556+
if isinstance(cache.client, DefaultClusterClient):
557+
pytest.skip("cluster client has a specific test")
558+
535559
for key in ["foo-aa", "foo-ab", "foo-bb", "foo-bc"]:
536560
cache.set(key, "foo")
537561

@@ -547,6 +571,9 @@ def test_delete_pattern_with_settings_default_scan_count(
547571
cache: ValkeyCache,
548572
settings: SettingsWrapper,
549573
):
574+
if isinstance(cache.client, DefaultClusterClient):
575+
pytest.skip("cluster client has a specific test")
576+
550577
for key in ["foo-aa", "foo-ab", "foo-bb", "foo-bc"]:
551578
cache.set(key, "foo")
552579
expected_count = settings.DJANGO_VALKEY_SCAN_ITERSIZE
@@ -715,6 +742,11 @@ def test_lock(self, cache: ValkeyCache):
715742
lock.release()
716743
assert not cache.has_key("foobar")
717744

745+
def test_lock_context_manager(self, cache: ValkeyCache):
746+
with cache.lock("foobar"):
747+
assert cache.has_key("foobar")
748+
assert not cache.has_key("foobar")
749+
718750
def test_lock_released_by_thread(self, cache: ValkeyCache):
719751
lock = cache.lock("foobar", thread_local=False)
720752
lock.acquire(blocking=True)
@@ -918,6 +950,9 @@ def test_sdiff(self, cache: ValkeyCache):
918950
if isinstance(cache.client, ShardClient):
919951
pytest.skip("ShardClient doesn't support sdiff")
920952

953+
if isinstance(cache.client, DefaultClusterClient):
954+
pytest.skip("cluster client has a specific test")
955+
921956
cache.sadd("foo1", "bar1", "bar2")
922957
cache.sadd("foo2", "bar2", "bar3")
923958
assert cache.sdiff("foo1", "foo2") == {"bar1"}
@@ -926,6 +961,9 @@ def test_sdiffstore(self, cache: ValkeyCache):
926961
if isinstance(cache.client, ShardClient):
927962
pytest.skip("ShardClient doesn't support sdiffstore")
928963

964+
if isinstance(cache.client, DefaultClusterClient):
965+
pytest.skip("cluster client has a specific test")
966+
929967
cache.sadd("foo1", "bar1", "bar2")
930968
cache.sadd("foo2", "bar2", "bar3")
931969
assert cache.sdiffstore("foo3", "foo1", "foo2") == 1
@@ -935,6 +973,9 @@ def test_sdiffstore_with_keys_version(self, cache: ValkeyCache):
935973
if isinstance(cache.client, ShardClient):
936974
pytest.skip("ShardClient doesn't support sdiffstore")
937975

976+
if isinstance(cache.client, DefaultClusterClient):
977+
pytest.skip("cluster client has a specific test")
978+
938979
cache.sadd("foo1", "bar1", "bar2", version=2)
939980
cache.sadd("foo2", "bar2", "bar3", version=2)
940981
assert cache.sdiffstore("foo3", "foo1", "foo2", version_keys=2) == 1
@@ -946,6 +987,9 @@ def test_sdiffstore_with_different_keys_versions_without_initial_set_in_version(
946987
if isinstance(cache.client, ShardClient):
947988
pytest.skip("ShardClient doesn't support sdiffstore")
948989

990+
if isinstance(cache.client, DefaultClusterClient):
991+
pytest.skip("cluster client has a specific test")
992+
949993
cache.sadd("foo1", "bar1", "bar2", version=1)
950994
cache.sadd("foo2", "bar2", "bar3", version=2)
951995
assert cache.sdiffstore("foo3", "foo1", "foo2", version_keys=2) == 0
@@ -956,6 +1000,9 @@ def test_sdiffstore_with_different_keys_versions_with_initial_set_in_version(
9561000
if isinstance(cache.client, ShardClient):
9571001
pytest.skip("ShardClient doesn't support sdiffstore")
9581002

1003+
if isinstance(cache.client, DefaultClusterClient):
1004+
pytest.skip("cluster client has a specific test")
1005+
9591006
cache.sadd("foo1", "bar1", "bar2", version=2)
9601007
cache.sadd("foo2", "bar2", "bar3", version=1)
9611008
assert cache.sdiffstore("foo3", "foo1", "foo2", version_keys=2) == 2
@@ -964,14 +1011,20 @@ def test_sinter(self, cache: ValkeyCache):
9641011
if isinstance(cache.client, ShardClient):
9651012
pytest.skip("ShardClient doesn't support sinter")
9661013

1014+
if isinstance(cache.client, DefaultClusterClient):
1015+
pytest.skip("cluster client has a specific test")
1016+
9671017
cache.sadd("foo1", "bar1", "bar2")
9681018
cache.sadd("foo2", "bar2", "bar3")
9691019
assert cache.sinter("foo1", "foo2") == {"bar2"}
9701020

971-
def test_interstore(self, cache: ValkeyCache):
1021+
def test_sinterstore(self, cache: ValkeyCache):
9721022
if isinstance(cache.client, ShardClient):
9731023
pytest.skip("ShardClient doesn't support sinterstore")
9741024

1025+
if isinstance(cache.client, DefaultClusterClient):
1026+
pytest.skip("cluster client has a specific test")
1027+
9751028
cache.sadd("foo1", "bar1", "bar2")
9761029
cache.sadd("foo2", "bar2", "bar3")
9771030
assert cache.sinterstore("foo3", "foo1", "foo2") == 1
@@ -1066,6 +1119,9 @@ def test_smove(self, cache: ValkeyCache):
10661119
# if isinstance(cache.client, ShardClient):
10671120
# pytest.skip("ShardClient doesn't support get_client")
10681121

1122+
if isinstance(cache.client, DefaultClusterClient):
1123+
pytest.skip("cluster client has a specific test")
1124+
10691125
cache.sadd("foo1", "bar1", "bar2")
10701126
cache.sadd("foo2", "bar2", "bar3")
10711127
assert cache.smove("foo1", "foo2", "bar1") is True
@@ -1130,6 +1186,9 @@ def test_sunion(self, cache: ValkeyCache):
11301186
if isinstance(cache.client, ShardClient):
11311187
pytest.skip("ShardClient doesn't support sunion")
11321188

1189+
if isinstance(cache.client, DefaultClusterClient):
1190+
pytest.skip("cluster client has a specific test")
1191+
11331192
cache.sadd("foo1", "bar1", "bar2")
11341193
cache.sadd("foo2", "bar2", "bar3")
11351194
assert cache.sunion("foo1", "foo2") == {"bar1", "bar2", "bar3"}
@@ -1138,6 +1197,9 @@ def test_sunionstore(self, cache: ValkeyCache):
11381197
if isinstance(cache.client, ShardClient):
11391198
pytest.skip("ShardClient doesn't support sunionstore")
11401199

1200+
if isinstance(cache.client, DefaultClusterClient):
1201+
pytest.skip("cluster client has a specific test")
1202+
11411203
cache.sadd("foo1", "bar1", "bar2")
11421204
cache.sadd("foo2", "bar2", "bar3")
11431205
assert cache.sunionstore("foo3", "foo1", "foo2") == 3

tests/test_cache_options.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import copy
2-
from collections.abc import Iterable
32
from typing import cast
43

54
import pytest
6-
from django.core.cache import caches
75
from pytest import LogCaptureFixture
86
from pytest_django.fixtures import SettingsWrapper
7+
8+
from django.core.cache import caches, cache as default_cache
9+
910
from valkey.exceptions import ConnectionError
1011

1112
from django_valkey.cache import ValkeyCache
1213
from django_valkey.client import ShardClient
14+
from django_valkey.cluster_cache.client import DefaultClusterClient
1315

1416

1517
def make_key(key: str, prefix: str, version: str) -> str:
@@ -31,13 +33,21 @@ def ignore_exceptions_cache(settings: SettingsWrapper) -> ValkeyCache:
3133
return cast(ValkeyCache, caches["doesnotexist"])
3234

3335

36+
@pytest.mark.skipif(
37+
isinstance(default_cache.client, DefaultClusterClient),
38+
reason="cluster client doesn't support ignore exception",
39+
)
3440
def test_get_django_omit_exceptions_many_returns_default_arg(
3541
ignore_exceptions_cache: ValkeyCache,
3642
):
3743
assert ignore_exceptions_cache._ignore_exceptions is True
3844
assert ignore_exceptions_cache.get_many(["key1", "key2", "key3"]) == {}
3945

4046

47+
@pytest.mark.skipif(
48+
isinstance(default_cache.client, DefaultClusterClient),
49+
reason="cluster client doesn't support ignore exception",
50+
)
4151
def test_get_django_omit_exceptions(
4252
caplog: LogCaptureFixture, ignore_exceptions_cache: ValkeyCache
4353
):
@@ -55,6 +65,10 @@ def test_get_django_omit_exceptions(
5565
)
5666

5767

68+
@pytest.mark.skipif(
69+
isinstance(default_cache.client, DefaultClusterClient),
70+
reason="cluster client doesn't support ignore exception",
71+
)
5872
def test_get_django_omit_exceptions_priority_1(settings: SettingsWrapper):
5973
caches_setting = copy.deepcopy(settings.CACHES)
6074
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
@@ -65,6 +79,10 @@ def test_get_django_omit_exceptions_priority_1(settings: SettingsWrapper):
6579
assert cache.get("key") is None
6680

6781

82+
@pytest.mark.skipif(
83+
isinstance(default_cache.client, DefaultClusterClient),
84+
reason="cluster client doesn't support ignore exception",
85+
)
6886
def test_get_django_omit_exceptions_priority_2(settings: SettingsWrapper):
6987
caches_setting = copy.deepcopy(settings.CACHES)
7088
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = False
@@ -76,23 +94,6 @@ def test_get_django_omit_exceptions_priority_2(settings: SettingsWrapper):
7694
cache.get("key")
7795

7896

79-
@pytest.fixture
80-
def key_prefix_cache(
81-
cache: ValkeyCache, settings: SettingsWrapper
82-
) -> Iterable[ValkeyCache]:
83-
caches_setting = copy.deepcopy(settings.CACHES)
84-
caches_setting["default"]["KEY_PREFIX"] = "*"
85-
settings.CACHES = caches_setting
86-
yield cache
87-
88-
89-
@pytest.fixture
90-
def with_prefix_cache() -> Iterable[ValkeyCache]:
91-
with_prefix = cast(ValkeyCache, caches["with_prefix"])
92-
yield with_prefix
93-
with_prefix.clear()
94-
95-
9697
class TestDjangoValkeyCacheEscapePrefix:
9798
def test_delete_pattern(
9899
self, key_prefix_cache: ValkeyCache, with_prefix_cache: ValkeyCache
@@ -113,6 +114,10 @@ def test_iter_keys(
113114
with_prefix_cache.set("b", "2")
114115
assert list(key_prefix_cache.iter_keys("*")) == ["a"]
115116

117+
@pytest.mark.skipif(
118+
isinstance(default_cache.client, DefaultClusterClient),
119+
reason="cluster client doesn't support ignore exception",
120+
)
116121
def test_keys(self, key_prefix_cache: ValkeyCache, with_prefix_cache: ValkeyCache):
117122
key_prefix_cache.set("a", "1")
118123
with_prefix_cache.set("b", "2")
@@ -121,6 +126,10 @@ def test_keys(self, key_prefix_cache: ValkeyCache, with_prefix_cache: ValkeyCach
121126
assert "b" not in keys
122127

123128

129+
@pytest.mark.skipif(
130+
isinstance(default_cache.client, DefaultClusterClient),
131+
reason="cluster client doesn't support ignore exception",
132+
)
124133
def test_custom_key_function(cache: ValkeyCache, settings: SettingsWrapper):
125134
caches_setting = copy.deepcopy(settings.CACHES)
126135
caches_setting["default"]["KEY_FUNCTION"] = "tests.test_cache_options.make_key"

0 commit comments

Comments
 (0)