fix: Use monkeypatch for proper test isolation in model restrictions
Replace @patch.dict decorator with pytest monkeypatch fixture in test_fallback_with_shorthand_restrictions to ensure proper environment variable cleanup between tests. This prevents OPENAI_ALLOWED_MODELS from leaking into subsequent tests. Also remove the manual clearing of _restriction_service singleton as it's no longer needed with proper environment variable isolation. This fixes test isolation issues where o3-pro tests would fail when run after restriction tests due to environment variable persistence. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -656,9 +656,13 @@ class TestAutoModeWithRestrictions:
|
||||
model = ModelProviderRegistry.get_preferred_fallback_model(ToolModelCategory.FAST_RESPONSE)
|
||||
assert model == "o4-mini"
|
||||
|
||||
@patch.dict(os.environ, {"OPENAI_ALLOWED_MODELS": "mini", "GEMINI_API_KEY": "", "OPENAI_API_KEY": "test-key"})
|
||||
def test_fallback_with_shorthand_restrictions(self):
|
||||
def test_fallback_with_shorthand_restrictions(self, monkeypatch):
|
||||
"""Test fallback model selection with shorthand restrictions."""
|
||||
# Use monkeypatch to set environment variables with automatic cleanup
|
||||
monkeypatch.setenv("OPENAI_ALLOWED_MODELS", "mini")
|
||||
monkeypatch.setenv("GEMINI_API_KEY", "")
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "test-key")
|
||||
|
||||
# Clear caches and reset registry
|
||||
import utils.model_restrictions
|
||||
from providers.registry import ModelProviderRegistry
|
||||
@@ -694,6 +698,3 @@ class TestAutoModeWithRestrictions:
|
||||
registry._initialized_providers.clear()
|
||||
registry._providers.update(original_providers)
|
||||
registry._initialized_providers.update(original_initialized)
|
||||
|
||||
# Clear the restriction service to prevent state leakage
|
||||
utils.model_restrictions._restriction_service = None
|
||||
|
||||
Reference in New Issue
Block a user