Joseph Pollack commited on
Commit
8fcb613
·
unverified ·
1 Parent(s): 8fa2ce6

attempts fix 403 and settings, adds openai tests and huggingface to ci

Browse files
tests/conftest.py CHANGED
@@ -79,66 +79,62 @@ def default_to_huggingface(monkeypatch):
79
  # Set a dummy HF_TOKEN if not set (prevents errors, but tests should mock actual API calls)
80
  if "HF_TOKEN" not in os.environ:
81
  monkeypatch.setenv("HF_TOKEN", "dummy_token_for_testing")
 
 
 
 
 
 
 
 
82
 
83
 
84
  @pytest.fixture
85
  def mock_hf_model():
86
- """Create a mock HuggingFace model for testing.
87
 
88
- This fixture provides a mock model that can be used in agent tests
89
- to avoid requiring actual API keys.
90
  """
91
- model = MagicMock()
92
- model.name = "meta-llama/Llama-3.1-8B-Instruct"
93
- model.model_name = "meta-llama/Llama-3.1-8B-Instruct"
 
 
 
 
94
  return model
95
 
96
 
97
  @pytest.fixture(autouse=True)
98
- def auto_mock_get_model(mock_hf_model, request):
99
- """Automatically mock get_model() in all agent modules.
100
 
101
  This fixture runs automatically for all tests (except OpenAI tests) and
102
- mocks get_model() where it's imported in each agent module, preventing
103
- tests from requiring actual API keys.
 
104
 
105
  Tests marked with @pytest.mark.openai will skip this fixture.
106
- Tests can override by explicitly patching get_model() themselves.
107
  """
108
  # Skip auto-mocking for OpenAI tests
109
  if "openai" in request.keywords:
110
  return
111
 
112
- # Patch get_model in all agent modules where it's imported
113
- agent_modules = [
114
- "src.agents.input_parser",
115
- "src.agents.writer",
116
- "src.agents.long_writer",
117
- "src.agents.proofreader",
118
- "src.agents.knowledge_gap",
119
- "src.agents.tool_selector",
120
- "src.agents.thinking",
121
- "src.agents.hypothesis_agent",
122
- "src.agents.report_agent",
123
- "src.agents.judge_agent_llm",
124
- "src.orchestrator.planner_agent",
125
- "src.services.statistical_analyzer",
126
- ]
127
-
128
- patches = []
129
- for module in agent_modules:
130
- try:
131
- patches.append(patch(f"{module}.get_model", return_value=mock_hf_model))
132
- except (ImportError, AttributeError):
133
- # Module might not exist or get_model might not be imported
134
- pass
135
 
136
- # Start all patches
137
- for patch_obj in patches:
138
- patch_obj.start()
 
139
 
140
  yield
141
 
142
- # Stop all patches
143
- for patch_obj in patches:
144
- patch_obj.stop()
 
79
  # Set a dummy HF_TOKEN if not set (prevents errors, but tests should mock actual API calls)
80
  if "HF_TOKEN" not in os.environ:
81
  monkeypatch.setenv("HF_TOKEN", "dummy_token_for_testing")
82
+
83
+ # Unset OpenAI/Anthropic keys to prevent fallback (unless explicitly set for specific tests)
84
+ # This ensures get_model() uses HuggingFace
85
+ if "OPENAI_API_KEY" in os.environ and os.environ.get("OPENAI_API_KEY"):
86
+ # Only unset if it's not explicitly needed (tests can set it if needed)
87
+ monkeypatch.delenv("OPENAI_API_KEY", raising=False)
88
+ if "ANTHROPIC_API_KEY" in os.environ and os.environ.get("ANTHROPIC_API_KEY"):
89
+ monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False)
90
 
91
 
92
  @pytest.fixture
93
  def mock_hf_model():
94
+ """Create a real HuggingFace model instance for testing.
95
 
96
+ This fixture provides a real HuggingFaceModel instance that uses
97
+ the HF API/client. The InferenceClient is mocked to prevent real API calls.
98
  """
99
+ from pydantic_ai.models.huggingface import HuggingFaceModel
100
+ from pydantic_ai.providers.huggingface import HuggingFaceProvider
101
+
102
+ # Create a real HuggingFace model with dummy token
103
+ # The InferenceClient will be mocked by auto_mock_hf_inference_client
104
+ provider = HuggingFaceProvider(api_key="dummy_token_for_testing")
105
+ model = HuggingFaceModel("meta-llama/Llama-3.1-8B-Instruct", provider=provider)
106
  return model
107
 
108
 
109
  @pytest.fixture(autouse=True)
110
+ def auto_mock_hf_inference_client(request):
111
+ """Automatically mock HuggingFace InferenceClient to prevent real API calls.
112
 
113
  This fixture runs automatically for all tests (except OpenAI tests) and
114
+ mocks the InferenceClient so tests use HuggingFace models but don't make
115
+ real API calls. This allows tests to use the actual HF model/client setup
116
+ without requiring API keys or making network requests.
117
 
118
  Tests marked with @pytest.mark.openai will skip this fixture.
119
+ Tests can override by explicitly patching InferenceClient themselves.
120
  """
121
  # Skip auto-mocking for OpenAI tests
122
  if "openai" in request.keywords:
123
  return
124
 
125
+ # Mock InferenceClient to prevent real API calls
126
+ # This allows get_model() to create real HuggingFaceModel instances
127
+ # but prevents actual network requests
128
+ mock_client = MagicMock()
129
+ mock_client.text_generation = AsyncMock(return_value="Mocked response")
130
+ mock_client.chat_completion = AsyncMock(return_value={"choices": [{"message": {"content": "Mocked response"}}]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
+ # Patch InferenceClient at its source (huggingface_hub)
133
+ # This will affect all imports of InferenceClient, including in pydantic_ai
134
+ inference_client_patch = patch("huggingface_hub.InferenceClient", return_value=mock_client)
135
+ inference_client_patch.start()
136
 
137
  yield
138
 
139
+ # Stop patch
140
+ inference_client_patch.stop()
 
tests/unit/agents/test_input_parser.py CHANGED
@@ -11,11 +11,13 @@ from src.utils.models import ParsedQuery
11
 
12
 
13
  @pytest.fixture
14
- def mock_model() -> MagicMock:
15
- """Create a mock Pydantic AI model."""
16
- model = MagicMock()
17
- model.name = "test-model"
18
- return model
 
 
19
 
20
 
21
  @pytest.fixture
 
11
 
12
 
13
  @pytest.fixture
14
+ def mock_model(mock_hf_model):
15
+ """Create a HuggingFace model for testing.
16
+
17
+ Uses the mock_hf_model from conftest which is a real HuggingFaceModel
18
+ instance with mocked InferenceClient to prevent real API calls.
19
+ """
20
+ return mock_hf_model
21
 
22
 
23
  @pytest.fixture