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

Add an option to remove unused requests from cassette #763

Merged
merged 7 commits into from
Jan 11, 2025

Conversation

danielnsilva
Copy link
Contributor

This PR adds an option to remove previously recorded interactions if they are not used. I believe this feature can address the issue discussed in #208. We don't need to focus on ignoring all requests in the cassette file, but instead, we can write to file only replayed interactions and new ones. If no previous request is used, VCR will save only new interactions.

There is a similar feature in Ruby VCR.

Replaces #677

Introduce the `drop_unused_requests` option (False by default). If True, it will force the `Cassette` saving operation with only played old interactions and new ones if they exist. As a result, unused old requests are dropped.

Add `_old_interactions`, `_played_interactions` and `_new_interactions()`.  The `_old_interactions` are previously recorded interactions loaded from Cassette files. The `_played_interactions` is a set of old interactions that were marked as played.  A new interaction is a tuple (request, response) in `self.data` that is not in `_old_interactions` list.
@jairhenrique jairhenrique reopened this Jan 4, 2025
@jairhenrique
Copy link
Collaborator

LGTM 🚀

@danielnsilva can you fix some lint erros?

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.38%. Comparing base (42d79b1) to head (d64cdd3).
Report is 267 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #763      +/-   ##
==========================================
+ Coverage   89.81%   92.38%   +2.56%     
==========================================
  Files          26       27       +1     
  Lines        1738     1864     +126     
  Branches      311      249      -62     
==========================================
+ Hits         1561     1722     +161     
+ Misses        141       95      -46     
- Partials       36       47      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jairhenrique
Copy link
Collaborator

@hartwork can you help review this pull request?

vcr/cassette.py Outdated Show resolved Hide resolved
vcr/cassette.py Outdated Show resolved Hide resolved
docs/advanced.rst Outdated Show resolved Hide resolved
Copy link
Collaborator

@hartwork hartwork left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielnsilva thanks for the adjustments! 👍

@hartwork hartwork merged commit 5f1b20c into kevin1024:master Jan 11, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants