|
10 | 10 | from contextlib import contextmanager
|
11 | 11 | from dataclasses import dataclass
|
12 | 12 | from datetime import datetime
|
| 13 | +from functools import cached_property |
13 | 14 | from pathlib import Path
|
14 | 15 | from types import ModuleType
|
15 | 16 | from typing import TYPE_CHECKING, Any, Callable
|
|
20 | 21 | from pytest_mock import MockerFixture
|
21 | 22 | from typing_extensions import TypeAlias
|
22 | 23 | from vcr import VCR, request as vcr_request
|
| 24 | +from vcr.stubs import aiohttp_stubs |
23 | 25 |
|
24 | 26 | import pydantic_ai.models
|
25 | 27 | from pydantic_ai.messages import BinaryContent
|
@@ -201,6 +203,18 @@ def method_matcher(r1: vcr_request.Request, r2: vcr_request.Request) -> None:
|
201 | 203 | vcr.register_matcher('method', method_matcher)
|
202 | 204 |
|
203 | 205 |
|
| 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 | + |
204 | 218 | @pytest.fixture(scope='module')
|
205 | 219 | def vcr_config():
|
206 | 220 | return {
|
|
0 commit comments