Skip to content

Commit 6d0357d

Browse files
authored
Merge pull request #233 from EasyPost/expire_cassettes
feat: adds cassette expirations
2 parents 48ec491 + d2d6e25 commit 6d0357d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ git submodule update --remote
9595

9696
### Testing
9797

98-
The test suite in this project was specifically built to produce consistent results on every run, regardless of when they run or who is running them. This project uses [VCR](https://github.com/kevin1024/vcrpy) to record and replay HTTP requests and responses via "cassettes". When the suite is run, the HTTP requests and responses for each test function will be saved to a cassette if they do not exist already and replayed from this saved file if they do, which saves the need to make live API calls on every test run.
98+
The test suite in this project was specifically built to produce consistent results on every run, regardless of when they run or who is running them. This project uses [VCR](https://github.com/kevin1024/vcrpy) to record and replay HTTP requests and responses via "cassettes". When the suite is run, the HTTP requests and responses for each test function will be saved to a cassette if they do not exist already and replayed from this saved file if they do, which saves the need to make live API calls on every test run. If you receive errors about a cassette expiring, delete and re-record the cassette to ensure the data is up-to-date.
9999

100100
**Sensitive Data:** We've made every attempt to include scrubbers for sensitive data when recording cassettes so that PII or sensitive info does not persist in version control; however, please ensure when recording or re-recording cassettes that prior to committing your changes, no PII or sensitive information gets persisted by inspecting the cassette.
101101

tests/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import datetime
12
import json
23
import os
4+
import warnings
35
from typing import (
46
Any,
57
Dict,
@@ -69,6 +71,25 @@ def partner_prod_api_key():
6971
easypost.api_key = default_key
7072

7173

74+
@pytest.fixture(autouse=True)
75+
def check_expired_cassettes(expiration_days: int = 180, throw_error: bool = False):
76+
"""Checks for expired cassettes and throws errors if they are too old and must be re-recorded."""
77+
test_name = os.environ.get("PYTEST_CURRENT_TEST").split(":")[-1].split(" ")[0] # type: ignore
78+
cassette_filepath = os.path.join("tests", "cassettes", f"{test_name}.yaml")
79+
80+
if os.path.exists(cassette_filepath):
81+
cassette_timestamp = datetime.datetime.fromtimestamp(os.stat(cassette_filepath).st_mtime)
82+
expiration_timestamp = cassette_timestamp + datetime.timedelta(days=expiration_days)
83+
current_timestamp = datetime.datetime.now()
84+
85+
if current_timestamp > expiration_timestamp:
86+
error_message = f"{cassette_filepath} is older than {expiration_days} days and has expired. Please re-record the cassette." # noqa
87+
if throw_error:
88+
raise Exception(error_message)
89+
else:
90+
warnings.warn(error_message)
91+
92+
7293
@pytest.fixture(scope="session")
7394
def vcr_config():
7495
"""Setup the VCR config for the test suite."""

tests/test_event.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def test_event_retrieve(page_size):
2424
assert str.startswith(event.id, "evt_")
2525

2626

27-
@pytest.mark.vcr()
2827
def test_event_receive(event_json):
2928
event = easypost.Event.receive(event_json)
3029

0 commit comments

Comments
 (0)