Skip to content

Commit 742fb87

Browse files
committed
test omit_exception with more operations
1 parent b5578c4 commit 742fb87

File tree

2 files changed

+379
-107
lines changed

2 files changed

+379
-107
lines changed

tests/test_cache_options.py

Lines changed: 195 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from valkey.exceptions import ConnectionError
1111

1212
from django_valkey.cache import ValkeyCache
13-
from django_valkey.client import ShardClient
13+
from django_valkey.client import ShardClient, HerdClient, DefaultClient
1414
from django_valkey.cluster_cache.client import DefaultClusterClient
1515

1616

@@ -22,76 +22,213 @@ def reverse_key(key: str) -> str:
2222
return key.split("#", 2)[2]
2323

2424

25-
@pytest.fixture
26-
def ignore_exceptions_cache(settings: SettingsWrapper) -> ValkeyCache:
27-
caches_setting = copy.deepcopy(settings.CACHES)
28-
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
29-
caches_setting["doesnotexist"]["OPTIONS"]["LOG_IGNORED_EXCEPTIONS"] = True
30-
settings.CACHES = caches_setting
31-
settings.DJANGO_VALKEY_IGNORE_EXCEPTIONS = True
32-
settings.DJANGO_VALKEY_LOG_IGNORED_EXCEPTIONS = True
33-
return cast(ValkeyCache, caches["doesnotexist"])
25+
methods_with_no_parameters = {"clear", "close"}
3426

27+
methods_with_one_required_parameters = {
28+
"decr",
29+
"delete",
30+
"delete_many",
31+
"delete_pattern",
32+
"get",
33+
"get_many",
34+
"has_key",
35+
"hkeys",
36+
"hlen",
37+
"incr",
38+
"incr_version",
39+
"keys",
40+
"lock",
41+
"mget",
42+
"pttl",
43+
"persist",
44+
"scard",
45+
"sdiff",
46+
"sinter",
47+
"smembers",
48+
"spop",
49+
"srandmember",
50+
"sscan",
51+
"sunion",
52+
"touch",
53+
"ttl",
54+
}
55+
methods_with_two_required_parameters = {
56+
"add",
57+
"expire",
58+
"expire_at",
59+
"hdel",
60+
"hexists",
61+
"pexpire",
62+
"pexpire_at",
63+
"sadd",
64+
"sdiffstore",
65+
"set",
66+
"sinterstore",
67+
"sismember",
68+
"smismember",
69+
"srem",
70+
"sunionstore",
71+
}
72+
methods_with_three_required_parameters = {"hset", "smove"}
73+
methods_taking_dictionary = {"mset", "set_many"}
74+
iter_methods = {
75+
"iter_keys",
76+
"sscan_iter",
77+
}
3578

36-
@pytest.mark.skipif(
37-
isinstance(default_cache.client, DefaultClusterClient),
38-
reason="cluster client doesn't support ignore exception",
39-
)
40-
def test_get_django_omit_exceptions_many_returns_default_arg(
41-
ignore_exceptions_cache: ValkeyCache,
42-
):
43-
assert ignore_exceptions_cache._ignore_exceptions is True
44-
assert ignore_exceptions_cache.get_many(["key1", "key2", "key3"]) == {}
79+
no_shard_methods = {
80+
"mget",
81+
"mset",
82+
"iter_keys",
83+
}
84+
no_herd_method = {
85+
"incr",
86+
"decr",
87+
}
4588

4689

4790
@pytest.mark.skipif(
4891
isinstance(default_cache.client, DefaultClusterClient),
4992
reason="cluster client doesn't support ignore exception",
5093
)
51-
def test_get_django_omit_exceptions(
52-
caplog: LogCaptureFixture, ignore_exceptions_cache: ValkeyCache
53-
):
54-
assert ignore_exceptions_cache._ignore_exceptions is True
55-
assert ignore_exceptions_cache._log_ignored_exceptions is True
56-
57-
assert ignore_exceptions_cache.get("key") is None
58-
assert ignore_exceptions_cache.get("key", "default") == "default"
59-
assert ignore_exceptions_cache.get("key", default="default") == "default"
60-
61-
assert len(caplog.records) == 3
62-
assert all(
63-
record.levelname == "ERROR" and record.msg == "Exception ignored"
64-
for record in caplog.records
94+
class TestDjangoValkeyOmitException:
95+
@pytest.fixture
96+
def conf_cache(self, settings: SettingsWrapper):
97+
caches_setting = copy.deepcopy(settings.CACHES)
98+
settings.CACHES = caches_setting
99+
return caches_setting
100+
101+
@pytest.fixture
102+
def conf_cache_to_ignore_exception(self, settings: SettingsWrapper, conf_cache):
103+
conf_cache["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
104+
conf_cache["doesnotexist"]["OPTIONS"]["LOG_IGNORED_EXCEPTIONS"] = True
105+
settings.DJANGO_VALKEY_IGNORE_EXCEPTIONS = True
106+
settings.DJANGO_VALKEY_LOG_IGNORED_EXCEPTIONS = True
107+
108+
@pytest.fixture
109+
def ignore_exceptions_cache(self, conf_cache_to_ignore_exception) -> ValkeyCache:
110+
return cast(ValkeyCache, caches["doesnotexist"])
111+
112+
def test_methods_with_no_argument_omit_exception(
113+
self, ignore_exceptions_cache: ValkeyCache, subtests
114+
):
115+
for m in methods_with_no_parameters:
116+
method = getattr(ignore_exceptions_cache, m)
117+
with subtests.test(method=method):
118+
method()
119+
120+
def test_methods_with_one_argument_omit_exception(
121+
self, ignore_exceptions_cache: ValkeyCache, subtests
122+
):
123+
for m in methods_with_one_required_parameters:
124+
method = getattr(ignore_exceptions_cache, m)
125+
with subtests.test(method=method):
126+
if (
127+
isinstance(default_cache.client, ShardClient)
128+
and m in no_shard_methods
129+
):
130+
pytest.skip(f"shard client doesn't support {m}")
131+
elif (
132+
isinstance(default_cache.client, HerdClient) and m in no_herd_method
133+
):
134+
pytest.skip(f"herd client doesn't support {m}")
135+
method("abc")
136+
137+
def test_methods_with_two_argument_omit_exception(
138+
self, ignore_exceptions_cache: ValkeyCache, subtests
139+
):
140+
for m in methods_with_two_required_parameters:
141+
method = getattr(ignore_exceptions_cache, m)
142+
if isinstance(default_cache.client, ShardClient) and m in no_shard_methods:
143+
pytest.skip(f"shard client doesn't support {m}")
144+
with subtests.test(method=method):
145+
method("abc", 1)
146+
147+
@pytest.mark.skipif(
148+
not isinstance(default_cache.client, DefaultClient),
149+
reason="not supported by none default clients",
65150
)
151+
def test_methods_with_three_argument_omit_exception(
152+
self, ignore_exceptions_cache: ValkeyCache, subtests
153+
):
154+
for m in methods_with_three_required_parameters:
155+
method = getattr(ignore_exceptions_cache, m)
156+
with subtests.test(method=method):
157+
method("abc", "def", "ghi")
66158

159+
def test_methods_taking_dictionary(
160+
self, ignore_exceptions_cache: ValkeyCache, subtests
161+
):
162+
for m in methods_taking_dictionary:
163+
method = getattr(ignore_exceptions_cache, m)
164+
with subtests.test(method=method):
165+
if (
166+
isinstance(default_cache.client, ShardClient)
167+
and m in no_shard_methods
168+
):
169+
pytest.skip(f"shard client doesn't support {m}")
170+
method({"abc": "def"})
67171

68-
@pytest.mark.skipif(
69-
isinstance(default_cache.client, DefaultClusterClient),
70-
reason="cluster client doesn't support ignore exception",
71-
)
72-
def test_get_django_omit_exceptions_priority_1(settings: SettingsWrapper):
73-
caches_setting = copy.deepcopy(settings.CACHES)
74-
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
75-
settings.CACHES = caches_setting
76-
settings.DJANGO_VALKEY_IGNORE_EXCEPTIONS = False
77-
cache = cast(ValkeyCache, caches["doesnotexist"])
78-
assert cache._ignore_exceptions is True
79-
assert cache.get("key") is None
172+
def test_iterator_methods(self, ignore_exceptions_cache: ValkeyCache, subtests):
173+
for m in iter_methods:
174+
method = getattr(ignore_exceptions_cache, m)
175+
with subtests.test(method=method):
176+
if (
177+
isinstance(default_cache.client, ShardClient)
178+
and m in no_shard_methods
179+
):
180+
pytest.skip(f"shard client doesn't support {m}")
181+
for _ in method("abc"):
182+
pass
80183

184+
def test_get_django_omit_exceptions_many_returns_default_arg(
185+
self,
186+
ignore_exceptions_cache: ValkeyCache,
187+
):
188+
assert ignore_exceptions_cache._ignore_exceptions is True
189+
assert ignore_exceptions_cache.get_many(["key1", "key2", "key3"]) == {}
81190

82-
@pytest.mark.skipif(
83-
isinstance(default_cache.client, DefaultClusterClient),
84-
reason="cluster client doesn't support ignore exception",
85-
)
86-
def test_get_django_omit_exceptions_priority_2(settings: SettingsWrapper):
87-
caches_setting = copy.deepcopy(settings.CACHES)
88-
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = False
89-
settings.CACHES = caches_setting
90-
settings.DJANGO_VALKEY_IGNORE_EXCEPTIONS = True
91-
cache = cast(ValkeyCache, caches["doesnotexist"])
92-
assert cache._ignore_exceptions is False
93-
with pytest.raises(ConnectionError):
94-
cache.get("key")
191+
def test_get_django_omit_exceptions(
192+
self, caplog: LogCaptureFixture, ignore_exceptions_cache: ValkeyCache
193+
):
194+
assert ignore_exceptions_cache._ignore_exceptions is True
195+
assert ignore_exceptions_cache._log_ignored_exceptions is True
196+
197+
assert ignore_exceptions_cache.get("key") is None
198+
assert ignore_exceptions_cache.get("key", "default") == "default"
199+
assert ignore_exceptions_cache.get("key", default="default") == "default"
200+
201+
assert len(caplog.records) == 3
202+
assert all(
203+
record.levelname == "ERROR" and record.msg == "Exception ignored"
204+
for record in caplog.records
205+
)
206+
207+
def test_get_django_omit_exceptions_priority_1(self, settings: SettingsWrapper):
208+
caches_setting = copy.deepcopy(settings.CACHES)
209+
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
210+
settings.CACHES = caches_setting
211+
settings.DJANGO_VALKEY_IGNORE_EXCEPTIONS = False
212+
cache = cast(ValkeyCache, caches["doesnotexist"])
213+
assert cache._ignore_exceptions is True
214+
assert cache.get("key") is None
215+
216+
def test_get_django_omit_exceptions_priority_2(self, settings: SettingsWrapper):
217+
caches_setting = copy.deepcopy(settings.CACHES)
218+
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = False
219+
settings.CACHES = caches_setting
220+
settings.DJANGO_VALKEY_IGNORE_EXCEPTIONS = True
221+
cache = cast(ValkeyCache, caches["doesnotexist"])
222+
assert cache._ignore_exceptions is False
223+
with pytest.raises(ConnectionError):
224+
cache.get("key")
225+
226+
def test_error_raised_when_ignore_is_not_set(self, conf_cache):
227+
cache = caches["doesnotexist"]
228+
assert cache._ignore_exceptions is False
229+
assert cache._log_ignored_exceptions is False
230+
with pytest.raises(ConnectionError):
231+
cache.get("key")
95232

96233

97234
class TestDjangoValkeyCacheEscapePrefix:

0 commit comments

Comments
 (0)