From 2fef051fc81c28e1f4cc399b40af87d071ecac38 Mon Sep 17 00:00:00 2001 From: amirreza Date: Mon, 30 Sep 2024 19:28:39 +0330 Subject: [PATCH] test mset, mget --- tests/settings/sqlite_cluster.py | 39 +++++++++++++++++++++++++++++++ tests/test_backend.py | 25 ++++++++++++++++---- tests/tests_async/test_backend.py | 31 ++++++++++++++++++++---- 3 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 tests/settings/sqlite_cluster.py diff --git a/tests/settings/sqlite_cluster.py b/tests/settings/sqlite_cluster.py new file mode 100644 index 0000000..3031c83 --- /dev/null +++ b/tests/settings/sqlite_cluster.py @@ -0,0 +1,39 @@ +SECRET_KEY = "django_tests_secret_key" + +CACHES = { + "default": { + "BACKEND": "django_valkey.cache.ValkeyCache", + "LOCATION": ["valkey://127.0.0.1:6379", "valkey://127.0.0.1:6379"], + "OPTIONS": {"CLIENT_CLASS": "django_valkey.client.DefaultClient"}, + }, + "doesnotexist": { + "BACKEND": "django_valkey.cache.ValkeyCache", + "LOCATION": "valkey://127.0.0.1:56379?db=1", + "OPTIONS": {"CLIENT_CLASS": "django_valkey.client.DefaultClient"}, + }, + "sample": { + "BACKEND": "django_valkey.cache.ValkeyCache", + "LOCATION": "valkey://127.0.0.1:6379:1,valkey://127.0.0.1:6379:1", + "OPTIONS": {"CLIENT_CLASS": "django_valkey.client.DefaultClient"}, + }, + "with_prefix": { + "BACKEND": "django_valkey.cache.ValkeyCache", + "LOCATION": "valkey://127.0.0.1:6379?db=1", + "OPTIONS": {"CLIENT_CLASS": "django_valkey.client.DefaultClient"}, + "KEY_PREFIX": "test-prefix", + }, +} + +# Include `django.contrib.auth` and `django.contrib.contenttypes` for mypy / +# django-stubs. + +# See: +# - https://github.com/typeddjango/django-stubs/issues/318 +# - https://github.com/typeddjango/django-stubs/issues/534 +INSTALLED_APPS = [ + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", +] + +USE_TZ = False diff --git a/tests/test_backend.py b/tests/test_backend.py index 2a91792..80b2667 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -196,19 +196,36 @@ def test_get_many(self, cache: ValkeyCache): res = cache.get_many(["a", "b", "c"]) assert res == {"a": 1, "b": 2, "c": 3} + def test_mget(self, cache: ValkeyCache): + if isinstance(cache.client, ShardClient): + pytest.skip() + cache.set("a", 1) + cache.set("b", 2) + cache.set("c", 3) + + res = cache.mget(["a", "b", "c"]) + assert res == {"a": 1, "b": 2, "c": 3} + def test_get_many_unicode(self, cache: ValkeyCache): cache.set("a", "1") - cache.set("b", "2") - cache.set("c", "3") + cache.set("ب", "2") + cache.set("c", "الف") - res = cache.get_many(["a", "b", "c"]) - assert res == {"a": "1", "b": "2", "c": "3"} + res = cache.get_many(["a", "ب", "c"]) + assert res == {"a": "1", "ب": "2", "c": "الف"} def test_set_many(self, cache: ValkeyCache): cache.set_many({"a": 1, "b": 2, "c": 3}) res = cache.get_many(["a", "b", "c"]) assert res == {"a": 1, "b": 2, "c": 3} + def test_mset(self, cache: ValkeyCache): + if isinstance(cache.client, ShardClient): + pytest.skip() + cache.mset({"a": 1, "b": 2, "c": 3}) + res = cache.mget(["a", "b", "c"]) + assert res == {"a": 1, "b": 2, "c": 3} + def test_set_call_empty_pipeline( self, cache: ValkeyCache, diff --git a/tests/tests_async/test_backend.py b/tests/tests_async/test_backend.py index 8e5ef9e..fa945a9 100644 --- a/tests/tests_async/test_backend.py +++ b/tests/tests_async/test_backend.py @@ -199,19 +199,40 @@ async def test_get_many(self, cache: AsyncValkeyCache): res = await cache.aget_many(["a", "b", "c"]) assert res == {"a": 1, "b": 2, "c": 3} + async def test_mget(self, cache: AsyncValkeyCache): + await cache.aset("a", 1) + await cache.aset("b", 2) + await cache.aset("c", 3) + + res = await cache.mget(["a", "b", "c"]) + assert res == {"a": 1, "b": 2, "c": 3} + async def test_get_many_unicode(self, cache: AsyncValkeyCache): await cache.aset("a", "1") - await cache.aset("b", "2") - await cache.aset("c", "3") + await cache.aset("ب", "2") + await cache.aset("c", "الف") - res = await cache.aget_many(["a", "b", "c"]) - assert res == {"a": "1", "b": "2", "c": "3"} + res = await cache.aget_many(["a", "ب", "c"]) + assert res == {"a": "1", "ب": "2", "c": "الف"} + + async def test_mget_unicode(self, cache: AsyncValkeyCache): + await cache.aset("a", "1") + await cache.aset("ب", "2") + await cache.aset("c", "الف") + + res = await cache.mget(["a", "ب", "c"]) + assert res == {"a": "1", "ب": "2", "c": "الف"} async def test_set_many(self, cache: AsyncValkeyCache): await cache.aset_many({"a": 1, "b": 2, "c": 3}) res = await cache.aget_many(["a", "b", "c"]) assert res == {"a": 1, "b": 2, "c": 3} + async def test_mset(self, cache: AsyncValkeyCache): + await cache.amset({"a": 1, "b": 2, "c": 3}) + res = await cache.aget_many(["a", "b", "c"]) + assert res == {"a": 1, "b": 2, "c": 3} + async def test_set_call_empty_pipeline( self, cache: AsyncValkeyCache, @@ -823,7 +844,7 @@ async def test_clear(self, cache: AsyncValkeyCache): async def test_hset(self, cache: AsyncValkeyCache): # if isinstance(cache.client, ShardClient): # pytest.skip("ShardClient doesn't support get_client") - await cache.ahset("foo_hash1", "foo1", "bar1") + assert await cache.ahset("foo_hash1", "foo1", "bar1") == 1 await cache.ahset("foo_hash1", "foo2", "bar2") assert await cache.ahlen("foo_hash1") == 2 assert await cache.ahexists("foo_hash1", "foo1")