Skip to content

Commit 08812e3

Browse files
committed
Added test case
1 parent c081bd3 commit 08812e3

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

redis/cluster.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,16 +2983,24 @@ def _determine_nodes(self, *args, **kwargs) -> List["ClusterNode"]:
29832983
return [node]
29842984

29852985
def multi(self):
2986-
raise RedisClusterException("method multi() is not supported outside of transactional context")
2986+
raise RedisClusterException(
2987+
"method multi() is not supported outside of transactional context"
2988+
)
29872989

29882990
def discard(self):
2989-
raise RedisClusterException("method discard() is not supported outside of transactional context")
2991+
raise RedisClusterException(
2992+
"method discard() is not supported outside of transactional context"
2993+
)
29902994

29912995
def watch(self, *names):
2992-
raise RedisClusterException("method watch() is not supported outside of transactional context")
2996+
raise RedisClusterException(
2997+
"method watch() is not supported outside of transactional context"
2998+
)
29932999

29943000
def unwatch(self, *names):
2995-
raise RedisClusterException("method unwatch() is not supported outside of transactional context")
3001+
raise RedisClusterException(
3002+
"method unwatch() is not supported outside of transactional context"
3003+
)
29963004

29973005
def delete(self, *names):
29983006
if len(names) != 1:
@@ -3136,7 +3144,7 @@ def _send_command_parse_response(
31363144
def _reinitialize_on_error(self, error):
31373145
if self._watching:
31383146
if self.slot_migrating and self._executing:
3139-
raise WatchError("Slot rebalancing ocurred while watching keys")
3147+
raise WatchError("Slot rebalancing occurred while watching keys")
31403148

31413149
if self.slot_migrating or self._cluster_error:
31423150
if self._transaction_connection:

tests/test_cluster.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3420,7 +3420,9 @@ def test_pipeline_discard(self, r):
34203420
with pytest.raises(redis.exceptions.RedisClusterException) as ex:
34213421
pipe.discard()
34223422

3423-
assert str(ex.value).startswith("method discard() is not supported outside of transactional context")
3423+
assert str(ex.value).startswith(
3424+
"method discard() is not supported outside of transactional context"
3425+
)
34243426

34253427
# setting a pipeline and discarding should do the same
34263428
with r.pipeline() as pipe:
@@ -3431,7 +3433,9 @@ def test_pipeline_discard(self, r):
34313433
with pytest.raises(redis.exceptions.RedisClusterException) as ex:
34323434
pipe.discard()
34333435

3434-
assert str(ex.value).startswith("method discard() is not supported outside of transactional context")
3436+
assert str(ex.value).startswith(
3437+
"method discard() is not supported outside of transactional context"
3438+
)
34353439

34363440
pipe.set(f"{hashkey}:foo", "bar")
34373441
response = pipe.execute()

tests/test_cluster_transaction.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
import redis
8-
from redis import CrossSlotTransactionError, ConnectionPool
8+
from redis import CrossSlotTransactionError, ConnectionPool, RedisClusterException
99
from redis.backoff import NoBackoff
1010
from redis.client import Redis
1111
from redis.cluster import PRIMARY, ClusterNode, NodesManager, RedisCluster
@@ -36,14 +36,12 @@ def _find_source_and_target_node_for_slot(
3636

3737

3838
class TestClusterTransaction:
39-
4039
@pytest.mark.onlycluster
4140
def test_pipeline_is_true(self, r):
4241
"Ensure pipeline instances are not false-y"
4342
with r.pipeline(transaction=True) as pipe:
4443
assert pipe
4544

46-
4745
@pytest.mark.onlycluster
4846
def test_pipeline_empty_transaction(self, r):
4947
r["a"] = 0
@@ -92,6 +90,15 @@ def test_throws_exception_on_different_hash_slots(self, r):
9290
):
9391
tx.execute()
9492

93+
@pytest.mark.onlycluster
94+
def test_throws_exception_with_watch_on_different_hash_slots(self, r):
95+
with r.pipeline(transaction=True) as tx:
96+
with pytest.raises(
97+
RedisClusterException,
98+
match="WATCH - all keys must map to the same key slot",
99+
):
100+
tx.watch("key1", "key2")
101+
95102
@pytest.mark.onlycluster
96103
def test_transaction_with_watched_keys(self, r):
97104
r["a"] = 0
@@ -257,7 +264,7 @@ def ask_redirect_effect(conn, *args, **options):
257264
pipe.execute()
258265

259266
assert str(ex.value).startswith(
260-
"Slot rebalancing ocurred while watching keys"
267+
"Slot rebalancing occurred while watching keys"
261268
)
262269

263270
@pytest.mark.onlycluster
@@ -284,7 +291,9 @@ def test_retry_transaction_on_connection_error(self, r, mock_connection):
284291
assert pipe.execute() == [b"OK"]
285292

286293
@pytest.mark.onlycluster
287-
def test_retry_transaction_on_connection_error_with_watched_keys(self, r, mock_connection):
294+
def test_retry_transaction_on_connection_error_with_watched_keys(
295+
self, r, mock_connection
296+
):
288297
key = "book"
289298
slot = r.keyslot(key)
290299

@@ -342,7 +351,6 @@ def test_parse_error_raised(self, r):
342351
assert pipe.set(f"{hashkey}:z", "zzz").execute() == [b"OK"]
343352
assert r[f"{hashkey}:z"] == b"zzz"
344353

345-
346354
@pytest.mark.onlycluster
347355
def test_transaction_callable(self, r):
348356
hashkey = "{key}"

0 commit comments

Comments
 (0)