Skip to content

Commit 6756c9a

Browse files
committed
refactor: move hash tests to separate test_backend_hash.py file
Moved all hash operation tests from test_backend.py to a new dedicated test_backend_hash.py file for better organization and maintainability. Tests moved: - test_hset - test_hdel - test_hlen - test_hkeys - test_hexists - test_hash_version_support - test_hash_key_structure_in_redis All 84 hash tests (7 tests × 12 cache configurations) pass.
1 parent d95e930 commit 6756c9a

2 files changed

Lines changed: 120 additions & 112 deletions

File tree

tests/test_backend.py

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -823,118 +823,6 @@ def test_clear(self, cache: RedisCache):
823823
value_from_cache_after_clear = cache.get("foo")
824824
assert value_from_cache_after_clear is None
825825

826-
def test_hset(self, cache: RedisCache):
827-
if isinstance(cache.client, ShardClient):
828-
pytest.skip("ShardClient doesn't support get_client")
829-
cache.hset("foo_hash1", "foo1", "bar1")
830-
cache.hset("foo_hash1", "foo2", "bar2")
831-
assert cache.hlen("foo_hash1") == 2
832-
assert cache.hexists("foo_hash1", "foo1")
833-
assert cache.hexists("foo_hash1", "foo2")
834-
835-
def test_hdel(self, cache: RedisCache):
836-
if isinstance(cache.client, ShardClient):
837-
pytest.skip("ShardClient doesn't support get_client")
838-
cache.hset("foo_hash2", "foo1", "bar1")
839-
cache.hset("foo_hash2", "foo2", "bar2")
840-
assert cache.hlen("foo_hash2") == 2
841-
deleted_count = cache.hdel("foo_hash2", "foo1")
842-
assert deleted_count == 1
843-
assert cache.hlen("foo_hash2") == 1
844-
assert not cache.hexists("foo_hash2", "foo1")
845-
assert cache.hexists("foo_hash2", "foo2")
846-
847-
def test_hlen(self, cache: RedisCache):
848-
if isinstance(cache.client, ShardClient):
849-
pytest.skip("ShardClient doesn't support get_client")
850-
assert cache.hlen("foo_hash3") == 0
851-
cache.hset("foo_hash3", "foo1", "bar1")
852-
assert cache.hlen("foo_hash3") == 1
853-
cache.hset("foo_hash3", "foo2", "bar2")
854-
assert cache.hlen("foo_hash3") == 2
855-
856-
def test_hkeys(self, cache: RedisCache):
857-
if isinstance(cache.client, ShardClient):
858-
pytest.skip("ShardClient doesn't support get_client")
859-
cache.hset("foo_hash4", "foo1", "bar1")
860-
cache.hset("foo_hash4", "foo2", "bar2")
861-
cache.hset("foo_hash4", "foo3", "bar3")
862-
keys = cache.hkeys("foo_hash4")
863-
assert len(keys) == 3
864-
for i in range(len(keys)):
865-
assert keys[i] == f"foo{i + 1}"
866-
867-
def test_hexists(self, cache: RedisCache):
868-
if isinstance(cache.client, ShardClient):
869-
pytest.skip("ShardClient doesn't support get_client")
870-
cache.hset("foo_hash5", "foo1", "bar1")
871-
assert cache.hexists("foo_hash5", "foo1")
872-
assert not cache.hexists("foo_hash5", "foo")
873-
874-
def test_hash_version_support(self, cache: RedisCache):
875-
"""Test that version parameter works correctly for hash methods."""
876-
if isinstance(cache.client, ShardClient):
877-
pytest.skip("ShardClient doesn't support get_client")
878-
879-
# Set values with different versions
880-
cache.hset("my_hash", "field1", "value1", version=1)
881-
cache.hset("my_hash", "field2", "value2", version=1)
882-
cache.hset("my_hash", "field1", "different_value", version=2)
883-
884-
# Verify both versions exist independently
885-
assert cache.hexists("my_hash", "field1", version=1)
886-
assert cache.hexists("my_hash", "field2", version=1)
887-
assert cache.hexists("my_hash", "field1", version=2)
888-
assert not cache.hexists("my_hash", "field2", version=2)
889-
890-
# Verify hlen works with versions
891-
assert cache.hlen("my_hash", version=1) == 2
892-
assert cache.hlen("my_hash", version=2) == 1
893-
894-
# Verify hkeys works with versions
895-
keys_v1 = cache.hkeys("my_hash", version=1)
896-
assert len(keys_v1) == 2
897-
assert "field1" in keys_v1
898-
assert "field2" in keys_v1
899-
900-
keys_v2 = cache.hkeys("my_hash", version=2)
901-
assert len(keys_v2) == 1
902-
assert "field1" in keys_v2
903-
904-
# Verify hdel works with versions
905-
cache.hdel("my_hash", "field1", version=1)
906-
assert not cache.hexists("my_hash", "field1", version=1)
907-
assert cache.hexists("my_hash", "field1", version=2) # v2 should still exist
908-
909-
def test_hash_key_structure_in_redis(self, cache: RedisCache):
910-
"""Test that hash keys are prefixed but fields are not."""
911-
if isinstance(cache.client, ShardClient):
912-
pytest.skip("ShardClient doesn't support get_client")
913-
914-
# Get raw Redis client
915-
client = cache.client.get_client(write=False)
916-
917-
# Set some hash data
918-
cache.hset("user:1000", "email", "alice@example.com", version=2)
919-
cache.hset("user:1000", "name", "Alice", version=2)
920-
921-
# Get the actual Redis key that was created
922-
expected_key = cache.client.make_key("user:1000", version=2)
923-
924-
# Verify the hash exists in Redis with the prefixed key
925-
assert client.exists(expected_key)
926-
assert client.type(expected_key) == b"hash"
927-
928-
# Verify fields are stored WITHOUT prefix
929-
actual_fields = client.hkeys(expected_key)
930-
# Fields should be plain "email" and "name", not prefixed
931-
assert b"email" in actual_fields
932-
assert b"name" in actual_fields
933-
934-
# Verify field values are correct
935-
assert client.hget(expected_key, b"email") is not None
936-
assert client.hget(expected_key, b"name") is not None
937-
938826
def test_sadd(self, cache: RedisCache):
939827
assert cache.sadd("foo", "bar") == 1
940828
assert cache.smembers("foo") == {"bar"}

tests/test_backend_hash.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import pytest
2+
3+
from django_redis.cache import RedisCache
4+
from django_redis.client import ShardClient
5+
6+
7+
class TestHashOperations:
8+
"""Tests for Redis hash operations."""
9+
10+
def test_hset(self, cache: RedisCache):
11+
if isinstance(cache.client, ShardClient):
12+
pytest.skip("ShardClient doesn't support get_client")
13+
cache.hset("foo_hash1", "foo1", "bar1")
14+
cache.hset("foo_hash1", "foo2", "bar2")
15+
assert cache.hlen("foo_hash1") == 2
16+
assert cache.hexists("foo_hash1", "foo1")
17+
assert cache.hexists("foo_hash1", "foo2")
18+
19+
def test_hdel(self, cache: RedisCache):
20+
if isinstance(cache.client, ShardClient):
21+
pytest.skip("ShardClient doesn't support get_client")
22+
cache.hset("foo_hash2", "foo1", "bar1")
23+
cache.hset("foo_hash2", "foo2", "bar2")
24+
assert cache.hlen("foo_hash2") == 2
25+
deleted_count = cache.hdel("foo_hash2", "foo1")
26+
assert deleted_count == 1
27+
assert cache.hlen("foo_hash2") == 1
28+
assert not cache.hexists("foo_hash2", "foo1")
29+
assert cache.hexists("foo_hash2", "foo2")
30+
31+
def test_hlen(self, cache: RedisCache):
32+
if isinstance(cache.client, ShardClient):
33+
pytest.skip("ShardClient doesn't support get_client")
34+
assert cache.hlen("foo_hash3") == 0
35+
cache.hset("foo_hash3", "foo1", "bar1")
36+
assert cache.hlen("foo_hash3") == 1
37+
cache.hset("foo_hash3", "foo2", "bar2")
38+
assert cache.hlen("foo_hash3") == 2
39+
40+
def test_hkeys(self, cache: RedisCache):
41+
if isinstance(cache.client, ShardClient):
42+
pytest.skip("ShardClient doesn't support get_client")
43+
cache.hset("foo_hash4", "foo1", "bar1")
44+
cache.hset("foo_hash4", "foo2", "bar2")
45+
cache.hset("foo_hash4", "foo3", "bar3")
46+
keys = cache.hkeys("foo_hash4")
47+
assert len(keys) == 3
48+
for i in range(len(keys)):
49+
assert keys[i] == f"foo{i + 1}"
50+
51+
def test_hexists(self, cache: RedisCache):
52+
if isinstance(cache.client, ShardClient):
53+
pytest.skip("ShardClient doesn't support get_client")
54+
cache.hset("foo_hash5", "foo1", "bar1")
55+
assert cache.hexists("foo_hash5", "foo1")
56+
assert not cache.hexists("foo_hash5", "foo")
57+
58+
def test_hash_version_support(self, cache: RedisCache):
59+
"""Test that version parameter works correctly for hash methods."""
60+
if isinstance(cache.client, ShardClient):
61+
pytest.skip("ShardClient doesn't support get_client")
62+
63+
# Set values with different versions
64+
cache.hset("my_hash", "field1", "value1", version=1)
65+
cache.hset("my_hash", "field2", "value2", version=1)
66+
cache.hset("my_hash", "field1", "different_value", version=2)
67+
68+
# Verify both versions exist independently
69+
assert cache.hexists("my_hash", "field1", version=1)
70+
assert cache.hexists("my_hash", "field2", version=1)
71+
assert cache.hexists("my_hash", "field1", version=2)
72+
assert not cache.hexists("my_hash", "field2", version=2)
73+
74+
# Verify hlen works with versions
75+
assert cache.hlen("my_hash", version=1) == 2
76+
assert cache.hlen("my_hash", version=2) == 1
77+
78+
# Verify hkeys works with versions
79+
keys_v1 = cache.hkeys("my_hash", version=1)
80+
assert len(keys_v1) == 2
81+
assert "field1" in keys_v1
82+
assert "field2" in keys_v1
83+
84+
keys_v2 = cache.hkeys("my_hash", version=2)
85+
assert len(keys_v2) == 1
86+
assert "field1" in keys_v2
87+
88+
# Verify hdel works with versions
89+
cache.hdel("my_hash", "field1", version=1)
90+
assert not cache.hexists("my_hash", "field1", version=1)
91+
assert cache.hexists("my_hash", "field1", version=2) # v2 should still exist
92+
93+
def test_hash_key_structure_in_redis(self, cache: RedisCache):
94+
"""Test that hash keys are prefixed but fields are not."""
95+
if isinstance(cache.client, ShardClient):
96+
pytest.skip("ShardClient doesn't support get_client")
97+
98+
# Get raw Redis client
99+
client = cache.client.get_client(write=False)
100+
101+
# Set some hash data
102+
cache.hset("user:1000", "email", "alice@example.com", version=2)
103+
cache.hset("user:1000", "name", "Alice", version=2)
104+
105+
# Get the actual Redis key that was created
106+
expected_key = cache.client.make_key("user:1000", version=2)
107+
108+
# Verify the hash exists in Redis with the prefixed key
109+
assert client.exists(expected_key)
110+
assert client.type(expected_key) == b"hash"
111+
112+
# Verify fields are stored WITHOUT prefix
113+
actual_fields = client.hkeys(expected_key)
114+
# Fields should be plain "email" and "name", not prefixed
115+
assert b"email" in actual_fields
116+
assert b"name" in actual_fields
117+
118+
# Verify field values are correct
119+
assert client.hget(expected_key, b"email") is not None
120+
assert client.hget(expected_key, b"name") is not None

0 commit comments

Comments
 (0)