Skip to content

Commit 85efece

Browse files
committed
Patch VCR aiohttp stub to work with latest google-genai
1 parent ca325a4 commit 85efece

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from contextlib import contextmanager
1111
from dataclasses import dataclass
1212
from datetime import datetime
13+
from functools import cached_property
1314
from pathlib import Path
1415
from types import ModuleType
1516
from typing import TYPE_CHECKING, Any, Callable
@@ -20,6 +21,7 @@
2021
from pytest_mock import MockerFixture
2122
from typing_extensions import TypeAlias
2223
from vcr import VCR, request as vcr_request
24+
from vcr.stubs import aiohttp_stubs
2325

2426
import pydantic_ai.models
2527
from pydantic_ai.messages import BinaryContent
@@ -201,6 +203,18 @@ def method_matcher(r1: vcr_request.Request, r2: vcr_request.Request) -> None:
201203
vcr.register_matcher('method', method_matcher)
202204

203205

206+
@pytest.fixture(autouse=True)
207+
def mock_vcr_aiohttp_content(mocker: MockerFixture):
208+
# google-genai calls `self.response_stream.content.readline()` where `self.response_stream` is a `MockClientResponse`,
209+
# which creates a new `MockStream` each time instead of returning the same one, resulting in the readline cursor not being respected.
210+
# So we turn `content` into a cached property to return the same one each time.
211+
# VCR issue: https://github.com/kevin1024/vcrpy/issues/927. Once that's is resolved, we can remove this patch.
212+
cached_content = cached_property(aiohttp_stubs.MockClientResponse.content.fget) # type: ignore
213+
cached_content.__set_name__(aiohttp_stubs.MockClientResponse, 'content')
214+
mocker.patch('vcr.stubs.aiohttp_stubs.MockClientResponse.content', new=cached_content)
215+
mocker.patch('vcr.stubs.aiohttp_stubs.MockStream.set_exception', return_value=None)
216+
217+
204218
@pytest.fixture(scope='module')
205219
def vcr_config():
206220
return {

0 commit comments

Comments
 (0)