Skip to content

Commit

Permalink
Merge pull request burnash#1438 from burnash/feature/improve_cassette…
Browse files Browse the repository at this point in the history
…_error_handling

Add user friendly message when we can't override a test cassette
  • Loading branch information
lavigne958 authored Mar 15, 2024
2 parents 6e864b8 + 52a5989 commit 719619f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import itertools
import os
import unittest
from typing import Any

import pytest
import vcr
from google.oauth2.credentials import Credentials as UserCredentials
from google.oauth2.service_account import Credentials as ServiceAccountCredentials
from requests import Response
from vcr import VCR
from vcr.errors import CannotOverwriteExistingCassetteException

import gspread
from gspread.client import Client
Expand Down Expand Up @@ -52,7 +55,7 @@ def vcr_config():
"decode_compressed_response": True, # decode requests to save clear content
"record_mode": RECORD_MODE,
"serializer": "json",
"path_transformer": vcr.VCR.ensure_suffix(".json"),
"path_transformer": VCR.ensure_suffix(".json"),
"before_record_response": ignore_retry_requests,
"ignore_hosts": [
"oauth2.googleapis.com", # skip oauth requests, in replay mode we don't use them
Expand Down Expand Up @@ -80,14 +83,35 @@ def _sequence_generator(self):
return prefixed_counter(get_method_name(self.id()))


class VCRHTTPClient(BackOffHTTPClient):
def request(self, *args: Any, **kwargs: Any) -> Response:
try:
return super().request(*args, **kwargs)
except CannotOverwriteExistingCassetteException as e:
if CREDS_FILENAME is None or RECORD_MODE is None:
raise RuntimeError(
"""
cannot make new HTTP requests in replay-mode. Please run tests with env variables CREDS_FILENAME and RECORD_MODE
Please refer to contributing guide for details:
https://github.com/burnash/gspread/blob/master/.github/CONTRIBUTING.md
"""
)

# necessary env variables were provided, this is a real error
# like missing access rights on the actual file/folder where to save the cassette
raise e


@pytest.fixture(scope="module")
def client():
if CREDS_FILENAME:
auth_credentials = read_credentials(CREDS_FILENAME)
else:
auth_credentials = DummyCredentials(DUMMY_ACCESS_TOKEN)

gc = Client(auth=auth_credentials, http_client=BackOffHTTPClient)
gc = Client(auth=auth_credentials, http_client=VCRHTTPClient)
if not isinstance(gc, gspread.client.Client) is True:
raise AssertionError

Expand Down

0 comments on commit 719619f

Please sign in to comment.