Skip to content

Commit

Permalink
test: add tests to drop_unused_requests option
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnsilva committed Jan 4, 2023
1 parent 99c0384 commit 010fa26
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/integration/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

import vcr
from vcr.cassette import Cassette


def test_set_serializer_default_config(tmpdir, httpbin):
Expand Down Expand Up @@ -80,3 +81,20 @@ def some_test():
assert b"Not in content" in urlopen("http://httpbin.org/get").read()

assert not os.path.exists(str(tmpdir.join("dontsave2.yml")))

def test_set_drop_unused_requests(tmpdir, httpbin):
my_vcr = vcr.VCR(drop_unused_requests=True)
file = str(tmpdir.join("test.yaml"))

with my_vcr.use_cassette(file):
urlopen(httpbin.url)
urlopen(httpbin.url + "/get")

cassette = Cassette.load(path=file)
assert len(cassette) == 2

with my_vcr.use_cassette(file):
urlopen(httpbin.url)

cassette = Cassette.load(path=file)
assert len(cassette) == 1
23 changes: 23 additions & 0 deletions tests/unit/test_cassettes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from vcr.cassette import Cassette
from vcr.errors import UnhandledHTTPRequestError
from vcr.patch import force_reset
from vcr.request import Request
from vcr.stubs import VCRHTTPSConnection


Expand Down Expand Up @@ -394,3 +395,25 @@ def test_find_requests_with_most_matches_many_similar_requests(mock_get_matchers
(1, ["method", "path"], [("query", "failed : query")]),
(3, ["method", "path"], [("query", "failed : query")]),
]


def test_used_interactions(tmpdir):
interactions = [
{"request": {"body": "", "uri": "foo1", "method": "GET", "headers": {}}, "response": "bar1"},
{"request": {"body": "", "uri": "foo2", "method": "GET", "headers": {}}, "response": "bar2"},
{"request": {"body": "", "uri": "foo3", "method": "GET", "headers": {}}, "response": "bar3"}
]
file = tmpdir.join("test_cassette.yml")
file.write(yaml.dump({"interactions": [interactions[0], interactions[1]]}))

cassette = Cassette.load(path=str(file))
request = Request._from_dict(interactions[1]["request"])
cassette.play_response(request)
assert len(cassette._played_interactions) < len(cassette._old_interactions)

request = Request._from_dict(interactions[2]["request"])
cassette.append(request, interactions[2]["response"])
assert len(cassette._new_interactions()) == 1

used_interactions = cassette._played_interactions + cassette._new_interactions()
assert len(used_interactions) == 2

0 comments on commit 010fa26

Please sign in to comment.