Skip to content

Commit

Permalink
test: subclass vcr.cassette.Cassette to handle identical requests wit…
Browse files Browse the repository at this point in the history
…h different responses

Thanks to @vickyliin for suggesting this workaround for
kevin1024/vcrpy#753.
  • Loading branch information
cfm authored and legoktm committed Feb 29, 2024
1 parent 49b3ec1 commit 0b6b0f2
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion client/tests/sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@
VCR = vcr.VCR(cassette_library_dir="tests/sdk/data/")


class Cassette(vcr.cassette.Cassette):
"""Subclass `Cassette` to be able to handle two or more identical requests
with different responses at different positions in the cassette. Thanks to
@vickyliin for suggesting this workaround for kevin1024/vcrpy#753."""

def append_and_play(self, request, response) -> None:
prev_len_data = len(self.data)
super().append(request, response)
is_appended = len(self.data) == prev_len_data + 1
if is_appended:
self.play_counts[prev_len_data] += 1

def _load(self) -> None:
super()._load()
self.append = self.append_and_play
self._load = None


class VCRAPI(API):
"""Subclass `API` so that `_send_json_request()` is instrumented to record
and replay VCR.py cassettes.
Expand Down Expand Up @@ -53,7 +71,12 @@ def use_cassette(func):
def wrapper(self, *args, **kwargs):
# We have to specify an explicit path for each cassette because
# we're not using vcr.use_cassette() directly as a decorator.
with VCR.use_cassette(f"{func.__name__}.yaml") as cassette:
context = VCR.use_cassette(f"{func.__name__}.yml")

# Override `Cassette` to use our subclass before we enter the
# context manager.
context.cls = Cassette
with context as cassette:
with patch.object(VCRAPI, "_cassette", cassette, create=True):
return func(self, *args, **kwargs)

Expand Down

0 comments on commit 0b6b0f2

Please sign in to comment.