|
1 | 1 | import datetime |
2 | 2 | import json |
3 | 3 | import os |
| 4 | +import warnings |
4 | 5 | from typing import ( |
5 | 6 | Any, |
6 | 7 | Dict, |
@@ -70,26 +71,28 @@ def partner_prod_api_key(): |
70 | 71 | easypost.api_key = default_key |
71 | 72 |
|
72 | 73 |
|
73 | | -def check_expired_cassettes(expiration_days: int = 180): |
| 74 | +@pytest.fixture(autouse=True) |
| 75 | +def check_expired_cassettes(expiration_days: int = 180, throw_error: bool = False): |
74 | 76 | """Checks for expired cassettes and throws errors if they are too old and must be re-recorded.""" |
75 | 77 | test_name = os.environ.get("PYTEST_CURRENT_TEST").split(":")[-1].split(" ")[0] # type: ignore |
76 | 78 | cassette_filepath = os.path.join("tests", "cassettes", f"{test_name}.yaml") |
77 | 79 |
|
78 | 80 | if os.path.exists(cassette_filepath): |
79 | | - expiration_age = datetime.datetime.now() - datetime.timedelta(days=expiration_days) |
80 | | - cassette_age = datetime.datetime.fromtimestamp(os.stat(cassette_filepath).st_mtime) |
81 | | - |
82 | | - if cassette_age < expiration_age: |
83 | | - raise Exception( |
84 | | - f"{cassette_filepath} is older than {expiration_days} and has expired. Please re-record the cassette." |
85 | | - ) |
| 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) |
86 | 91 |
|
87 | 92 |
|
88 | 93 | @pytest.fixture(scope="session") |
89 | 94 | def vcr_config(): |
90 | 95 | """Setup the VCR config for the test suite.""" |
91 | | - check_expired_cassettes() |
92 | | - |
93 | 96 | return { |
94 | 97 | "match_on": [ |
95 | 98 | "body", |
|
0 commit comments