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 user friendly message when we can't override a test cassette #1438

Merged
merged 1 commit into from
Mar 15, 2024

Conversation

lavigne958
Copy link
Collaborator

I tested it, when you add new HTTP requests into a test, or a new test and just run the test suite with no env variables then it prints the new exception.

the output is quiet verbose though 🙄 it still prints the output from the VCR exception.

It looks like this:

collected 135 items / 134 deselected / 1 selected                                                                                                                                                                                            

tests/worksheet_test.py::WorksheetTest::test_acell FAILED

================================================================================================================== FAILURES ==================================================================================================================
__________________________________________________________________________________________________________ WorksheetTest.test_acell __________________________________________________________________________________________________________

self = <tests.conftest.VCRHTTPClient object at 0x7ccdd119a650>, args = ('get', 'https://sheets.googleapis.com/v4/spreadsheets/1CIKsfS4Dl5NH6zGSlOA3hbfnNElaKnk-64G03TcmAl8/values/%27Sheet1%27%21A1')
kwargs = {'params': {'dateTimeRenderOption': None, 'majorDimension': None, 'valueRenderOption': <ValueRenderOption.formatted: 'FORMATTED_VALUE'>}}

    def request(self, *args: Any, **kwargs: Any) -> Response:
        try:
>           return super().request(*args, **kwargs)

tests/conftest.py:89: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
gspread/http_client.py:576: in request
    return super().request(*args, **kwargs)
gspread/http_client.py:114: in request
    response = self.session.request(
.tox/py/lib/python3.10/site-packages/google/auth/transport/requests.py:549: in request
    response = super(AuthorizedSession, self).request(
.tox/py/lib/python3.10/site-packages/requests/sessions.py:589: in request
    resp = self.send(prep, **send_kwargs)
.tox/py/lib/python3.10/site-packages/requests/sessions.py:703: in send
    r = adapter.send(request, **kwargs)
.tox/py/lib/python3.10/site-packages/requests/adapters.py:486: in send
    resp = conn.urlopen(
.tox/py/lib/python3.10/site-packages/urllib3/connectionpool.py:703: in urlopen
    httplib_response = self._make_request(
.tox/py/lib/python3.10/site-packages/urllib3/connectionpool.py:440: in _make_request
    httplib_response = conn.getresponse(buffering=True)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <vcr.patch.VCRRequestsHTTPSConnection/gspread/tests/cassettes/WorksheetTest.test_acell.json object at 0x7ccdd0fde5f0>, _ = False, kwargs = {'buffering': True}

    def getresponse(self, _=False, **kwargs):
        """Retrieve the response"""
        # Check to see if the cassette has a response for this request. If so,
        # then return it
        if self.cassette.can_play_response_for(self._vcr_request):
            log.info(f"Playing response for {self._vcr_request} from cassette")
            response = self.cassette.play_response(self._vcr_request)
            return VCRHTTPResponse(response)
        else:
            if self.cassette.write_protected and self.cassette.filter_request(self._vcr_request):
>               raise CannotOverwriteExistingCassetteException(
                    cassette=self.cassette,
                    failed_request=self._vcr_request,
                )
E               vcr.errors.CannotOverwriteExistingCassetteException: Can't overwrite existing cassette ('gspread/tests/cassettes/WorksheetTest.test_acell.json') in your current record mode ('none').
E               No match for the request (<Request (GET) https://sheets.googleapis.com/v4/spreadsheets/1CIKsfS4Dl5NH6zGSlOA3hbfnNElaKnk-64G03TcmAl8/values/%27Sheet1%27%21A1?valueRenderOption=FORMATTED_VALUE>) was found.
E               Found 1 similar requests with 0 different matcher(s) :
E               
E               1 - (<Request (GET) https://sheets.googleapis.com/v4/spreadsheets/1CIKsfS4Dl5NH6zGSlOA3hbfnNElaKnk-64G03TcmAl8/values/%27Sheet1%27%21A1?valueRenderOption=FORMATTED_VALUE>).
E               Matchers succeeded : ['uri', 'method']
E               Matchers failed :

.tox/py/lib/python3.10/site-packages/vcr/stubs/__init__.py:263: CannotOverwriteExistingCassetteException

During handling of the above exception, another exception occurred:

self = <tests.worksheet_test.WorksheetTest testMethod=test_acell>

    @pytest.mark.vcr()
    def test_acell(self):
        cell = self.sheet.acell("A1")
        self.assertIsInstance(cell, gspread.cell.Cell)
>       cell = self.sheet.acell("A1")

tests/worksheet_test.py:40: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
gspread/worksheet.py:319: in acell
    return self.cell(
gspread/worksheet.py:351: in cell
    data = self.get(
gspread/worksheet.py:951: in get
    response = self.client.values_get(
gspread/http_client.py:236: in values_get
    r = self.request("get", url, params=params)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <tests.conftest.VCRHTTPClient object at 0x7ccdd119a650>, args = ('get', 'https://sheets.googleapis.com/v4/spreadsheets/1CIKsfS4Dl5NH6zGSlOA3hbfnNElaKnk-64G03TcmAl8/values/%27Sheet1%27%21A1')
kwargs = {'params': {'dateTimeRenderOption': None, 'majorDimension': None, 'valueRenderOption': <ValueRenderOption.formatted: 'FORMATTED_VALUE'>}}

        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
    """
                    )
E                   RuntimeError: 
E                   
E                   cannot make new HTTP requests in replay-mode. Please run tests with env variables CREDS_FILENAME and RECORD_MODE
E                   Please refer to contributing guide for details:
E                   
E                   https://github.com/burnash/gspread/blob/master/.github/CONTRIBUTING.md

tests/conftest.py:92: RuntimeError
============================================================================================================== warnings summary ==============================================================================================================
tests/worksheet_test.py:33
  gspread/tests/worksheet_test.py:33: PytestRemovedIn9Warning: Marks applied to fixtures have no effect
  See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function
    def reset_sheet(self):

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========================================================================================================== short test summary info ===========================================================================================================
FAILED tests/worksheet_test.py::WorksheetTest::test_acell - RuntimeError: 
================================================================================================ 1 failed, 134 deselected, 1 warning in 0.19s ================================================================================================
py: exit 1 (0.57 seconds) gspread > pytest -s -v -k test_acell tests/ pid=728068
.pkg: _exit> python /usr/.local/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
  py: FAIL code 1 (2.94=setup[2.37]+cmd[0.57] seconds)
  evaluation failed :( (3.00 seconds)

closes #1304

@lavigne958 lavigne958 requested a review from alifeee March 14, 2024 19:03
@lavigne958
Copy link
Collaborator Author

I am open to suggestion for the error message I tried something straight forward and explicit, but it could be improved for sure !

Copy link
Collaborator

@alifeee alifeee left a comment

Choose a reason for hiding this comment

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

This looks great to me!

I think failed test output is bound to be quite verbose. Hopefully it is more noticeable as the error is specific, as opposed to "this VCR library that I've not used is complaining and I'm not sure why".

I enjoy that we have been able to make a new HTTPClient... ;) how very useful that feature is :)

@lavigne958 lavigne958 merged commit 719619f into master Mar 15, 2024
10 checks passed
@lavigne958 lavigne958 deleted the feature/improve_cassette_error_handling branch March 15, 2024 07:44
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.

Improve test error when cassette is missing or outdated
2 participants