From e768f17a0335713da88fcfdf0b6227dd145f385f Mon Sep 17 00:00:00 2001 From: vickyliin Date: Thu, 3 Aug 2023 15:26:19 +0800 Subject: [PATCH 1/3] use rewind() after _load --- vcr/cassette.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vcr/cassette.py b/vcr/cassette.py index 1b8b1f2ab..a8e0c923e 100644 --- a/vcr/cassette.py +++ b/vcr/cassette.py @@ -243,7 +243,10 @@ def append(self, request, response): response = self._before_record_response(response) if response is None: return + self.data.append((request, response)) + self.play_counts[len(self.data) - 1] += 1 + self.dirty = True def filter_request(self, request): @@ -294,6 +297,7 @@ def responses_of(self, request): def rewind(self): self.play_counts = collections.Counter() + self.rewound = True def find_requests_with_most_matches(self, request): """ @@ -345,7 +349,7 @@ def _load(self): for request, response in zip(requests, responses): self.append(request, response) self.dirty = False - self.rewound = True + self.rewind() except (CassetteDecodeError, CassetteNotFoundError): pass From 3572b34d780b443a95024aa03ca761bdba17ab6e Mon Sep 17 00:00:00 2001 From: vickyliin Date: Thu, 3 Aug 2023 15:28:46 +0800 Subject: [PATCH 2/3] add test --- tests/integration/test_disksaver.py | 6 +++--- tests/unit/test_cassettes.py | 7 +++++++ tests/unit/test_unittest.py | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_disksaver.py b/tests/integration/test_disksaver.py index f9127d91b..d837c669b 100644 --- a/tests/integration/test_disksaver.py +++ b/tests/integration/test_disksaver.py @@ -20,7 +20,7 @@ def test_disk_saver_nowrite(tmpdir, mockbin_request_url): fname = str(tmpdir.join("synopsis.yaml")) with vcr.use_cassette(fname) as cass: urlopen(mockbin_request_url).read() - assert cass.play_count == 0 + assert cass.play_count == 1 last_mod = os.path.getmtime(fname) with vcr.use_cassette(fname) as cass: @@ -41,7 +41,7 @@ def test_disk_saver_write(tmpdir, mockbin_request_url): fname = str(tmpdir.join("synopsis.yaml")) with vcr.use_cassette(fname) as cass: urlopen(mockbin_request_url).read() - assert cass.play_count == 0 + assert cass.play_count == 1 last_mod = os.path.getmtime(fname) # Make sure at least 1 second passes, otherwise sometimes @@ -51,7 +51,7 @@ def test_disk_saver_write(tmpdir, mockbin_request_url): with vcr.use_cassette(fname, record_mode=vcr.mode.ANY) as cass: urlopen(mockbin_request_url).read() urlopen(mockbin_request_url + "/get").read() - assert cass.play_count == 1 + assert cass.play_count == 2 assert cass.dirty last_mod2 = os.path.getmtime(fname) diff --git a/tests/unit/test_cassettes.py b/tests/unit/test_cassettes.py index 1d58d4ac7..657cd5ee1 100644 --- a/tests/unit/test_cassettes.py +++ b/tests/unit/test_cassettes.py @@ -55,6 +55,7 @@ def test_cassette_append(): a.append("foo", "bar") assert a.requests == ["foo"] assert a.responses == ["bar"] + assert a.play_count == 1 def test_cassette_len(): @@ -72,6 +73,7 @@ def _mock_requests_match(request1, request2, matchers): def test_cassette_contains(): a = Cassette("test") a.append("foo", "bar") + a.rewind() assert "foo" in a @@ -93,6 +95,7 @@ def test_cassette_get_missing_response(): def test_cassette_cant_read_same_request_twice(): a = Cassette("test") a.append("foo", "bar") + a.rewind() a.play_response("foo") with pytest.raises(UnhandledHTTPRequestError): a.play_response("foo") @@ -143,6 +146,7 @@ def function(): def test_cassette_not_all_played(): a = Cassette("test") a.append("foo", "bar") + a.rewind() assert not a.all_played @@ -150,6 +154,7 @@ def test_cassette_not_all_played(): def test_cassette_all_played(): a = Cassette("test") a.append("foo", "bar") + a.rewind() a.play_response("foo") assert a.all_played @@ -159,6 +164,7 @@ def test_cassette_allow_playback_repeats(): a = Cassette("test", allow_playback_repeats=True) a.append("foo", "bar") a.append("other", "resp") + a.rewind() for _ in range(10): assert a.play_response("foo") == "bar" assert a.play_count == 10 @@ -183,6 +189,7 @@ def test_cassette_allow_playback_repeats(): def test_cassette_rewound(): a = Cassette("test") a.append("foo", "bar") + a.rewind() a.play_response("foo") assert a.all_played diff --git a/tests/unit/test_unittest.py b/tests/unit/test_unittest.py index 1ecdee362..dc20ef6a7 100644 --- a/tests/unit/test_unittest.py +++ b/tests/unit/test_unittest.py @@ -180,7 +180,7 @@ def _get_cassette_library_dir(self): test = run_testcase(MyTest)[0][0] assert b"illustrative examples" in test.response assert len(test.cassette.requests) == 1 - assert test.cassette.play_count == 0 + assert test.cassette.play_count == 1 # Second test reads from cassette. From facceff1b6219ba050e9a2ce9bc108223e2036a5 Mon Sep 17 00:00:00 2001 From: vickyliin Date: Thu, 3 Aug 2023 16:47:42 +0800 Subject: [PATCH 3/3] Revert "add test" This reverts commit 3572b34d780b443a95024aa03ca761bdba17ab6e. --- tests/integration/test_disksaver.py | 6 +++--- tests/unit/test_cassettes.py | 7 ------- tests/unit/test_unittest.py | 2 +- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/tests/integration/test_disksaver.py b/tests/integration/test_disksaver.py index d837c669b..f9127d91b 100644 --- a/tests/integration/test_disksaver.py +++ b/tests/integration/test_disksaver.py @@ -20,7 +20,7 @@ def test_disk_saver_nowrite(tmpdir, mockbin_request_url): fname = str(tmpdir.join("synopsis.yaml")) with vcr.use_cassette(fname) as cass: urlopen(mockbin_request_url).read() - assert cass.play_count == 1 + assert cass.play_count == 0 last_mod = os.path.getmtime(fname) with vcr.use_cassette(fname) as cass: @@ -41,7 +41,7 @@ def test_disk_saver_write(tmpdir, mockbin_request_url): fname = str(tmpdir.join("synopsis.yaml")) with vcr.use_cassette(fname) as cass: urlopen(mockbin_request_url).read() - assert cass.play_count == 1 + assert cass.play_count == 0 last_mod = os.path.getmtime(fname) # Make sure at least 1 second passes, otherwise sometimes @@ -51,7 +51,7 @@ def test_disk_saver_write(tmpdir, mockbin_request_url): with vcr.use_cassette(fname, record_mode=vcr.mode.ANY) as cass: urlopen(mockbin_request_url).read() urlopen(mockbin_request_url + "/get").read() - assert cass.play_count == 2 + assert cass.play_count == 1 assert cass.dirty last_mod2 = os.path.getmtime(fname) diff --git a/tests/unit/test_cassettes.py b/tests/unit/test_cassettes.py index 657cd5ee1..1d58d4ac7 100644 --- a/tests/unit/test_cassettes.py +++ b/tests/unit/test_cassettes.py @@ -55,7 +55,6 @@ def test_cassette_append(): a.append("foo", "bar") assert a.requests == ["foo"] assert a.responses == ["bar"] - assert a.play_count == 1 def test_cassette_len(): @@ -73,7 +72,6 @@ def _mock_requests_match(request1, request2, matchers): def test_cassette_contains(): a = Cassette("test") a.append("foo", "bar") - a.rewind() assert "foo" in a @@ -95,7 +93,6 @@ def test_cassette_get_missing_response(): def test_cassette_cant_read_same_request_twice(): a = Cassette("test") a.append("foo", "bar") - a.rewind() a.play_response("foo") with pytest.raises(UnhandledHTTPRequestError): a.play_response("foo") @@ -146,7 +143,6 @@ def function(): def test_cassette_not_all_played(): a = Cassette("test") a.append("foo", "bar") - a.rewind() assert not a.all_played @@ -154,7 +150,6 @@ def test_cassette_not_all_played(): def test_cassette_all_played(): a = Cassette("test") a.append("foo", "bar") - a.rewind() a.play_response("foo") assert a.all_played @@ -164,7 +159,6 @@ def test_cassette_allow_playback_repeats(): a = Cassette("test", allow_playback_repeats=True) a.append("foo", "bar") a.append("other", "resp") - a.rewind() for _ in range(10): assert a.play_response("foo") == "bar" assert a.play_count == 10 @@ -189,7 +183,6 @@ def test_cassette_allow_playback_repeats(): def test_cassette_rewound(): a = Cassette("test") a.append("foo", "bar") - a.rewind() a.play_response("foo") assert a.all_played diff --git a/tests/unit/test_unittest.py b/tests/unit/test_unittest.py index dc20ef6a7..1ecdee362 100644 --- a/tests/unit/test_unittest.py +++ b/tests/unit/test_unittest.py @@ -180,7 +180,7 @@ def _get_cassette_library_dir(self): test = run_testcase(MyTest)[0][0] assert b"illustrative examples" in test.response assert len(test.cassette.requests) == 1 - assert test.cassette.play_count == 1 + assert test.cassette.play_count == 0 # Second test reads from cassette.