refactor: Simplify o3-pro test by removing fixture and monkey patching boilerplate
- Remove over-engineered allow_all_models fixture (6 operations → 1 line API key setting) - Replace 10 lines of monkey patching boilerplate with 1-line inject_transport helper - Remove cargo-cult error handling that allowed test to pass with API failures - Create reusable transport_helpers.py for HTTP transport injection patterns - Fix provider registration state pollution between batch test runs - Test now works reliably in both individual and batch execution modes The test is significantly cleaner and addresses root cause (provider registration timing) rather than symptoms (cache clearing). 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
47
tests/transport_helpers.py
Normal file
47
tests/transport_helpers.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Helper functions for HTTP transport injection in tests."""
|
||||
|
||||
from tests.http_transport_recorder import TransportFactory
|
||||
|
||||
|
||||
def inject_transport(monkeypatch, cassette_path: str):
|
||||
"""Inject HTTP transport into OpenAICompatibleProvider for testing.
|
||||
|
||||
This helper simplifies the monkey patching pattern used across tests
|
||||
to inject custom HTTP transports for recording/replaying API calls.
|
||||
|
||||
Also ensures OpenAI provider is properly registered for tests that need it.
|
||||
|
||||
Args:
|
||||
monkeypatch: pytest monkeypatch fixture
|
||||
cassette_path: Path to cassette file for recording/replay
|
||||
|
||||
Returns:
|
||||
The created transport instance
|
||||
|
||||
Example:
|
||||
transport = inject_transport(monkeypatch, "path/to/cassette.json")
|
||||
"""
|
||||
# Ensure OpenAI provider is registered if API key is available
|
||||
import os
|
||||
if os.getenv("OPENAI_API_KEY"):
|
||||
from providers.registry import ModelProviderRegistry
|
||||
from providers.base import ProviderType
|
||||
from providers.openai_provider import OpenAIModelProvider
|
||||
ModelProviderRegistry.register_provider(ProviderType.OPENAI, OpenAIModelProvider)
|
||||
|
||||
# Create transport
|
||||
transport = TransportFactory.create_transport(str(cassette_path))
|
||||
|
||||
# Inject transport using the established pattern
|
||||
from providers.openai_compatible import OpenAICompatibleProvider
|
||||
|
||||
original_client_property = OpenAICompatibleProvider.client
|
||||
|
||||
def patched_client_getter(self):
|
||||
if self._client is None:
|
||||
self._test_transport = transport
|
||||
return original_client_property.fget(self)
|
||||
|
||||
monkeypatch.setattr(OpenAICompatibleProvider, "client", property(patched_client_getter))
|
||||
|
||||
return transport
|
||||
Reference in New Issue
Block a user