Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipeline DISCARD support #1565

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,12 @@ def execute(self, raise_on_error=True):
finally:
self.reset()

def discard(self):
"""Flushes all previously queued commands
See: https://redis.io/commands/DISCARD
"""
self.execute_command("DISCARD")

def watch(self, *names):
"Watches the values at keys ``names``"
if self.explicit_transaction:
Expand Down
28 changes: 27 additions & 1 deletion tests/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

import redis
from .conftest import wait_for_command
from .conftest import wait_for_command, skip_if_server_version_lt


class TestPipeline:
Expand Down Expand Up @@ -353,3 +353,29 @@ def test_pipeline_with_bitfield(self, r):

assert pipe == pipe2
assert response == [True, [0, 0, 15, 15, 14], b'1']

@skip_if_server_version_lt('2.0.0')
def test_pipeline_discard(self, r):

# empty pipeline should raise an error
with r.pipeline() as pipe:
pipe.set('key', 'someval')
pipe.discard()
with pytest.raises(redis.exceptions.ResponseError):
pipe.execute()

# setting a pipeline and discarding should do the same
with r.pipeline() as pipe:
pipe.set('key', 'someval')
pipe.set('someotherkey', 'val')
response = pipe.execute()
pipe.set('key', 'another value!')
pipe.discard()
pipe.set('key', 'another vae!')
with pytest.raises(redis.exceptions.ResponseError):
pipe.execute()

pipe.set('foo', 'bar')
response = pipe.execute()
assert response[0]
assert r.get('foo') == b'bar'