Fix CI: mock lazy anthropic import via sys.modules instead of module attribute

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Safi
2026-04-10 15:48:54 +01:00
parent 53d0ceb471
commit db07e9f93a
+10 -3
View File
@@ -6,6 +6,8 @@ import os
from pathlib import Path
from unittest.mock import MagicMock, patch
import sys
import pytest
from graphify.transcribe import (
@@ -52,10 +54,12 @@ def test_build_whisper_prompt_llm_success():
fake_response = MagicMock()
fake_response.content = [MagicMock(text="Machine learning and deep learning research")]
mock_anthropic = MagicMock()
mock_anthropic.Anthropic.return_value.messages.create.return_value = fake_response
with patch.dict(os.environ, {}, clear=False):
os.environ.pop("GRAPHIFY_WHISPER_PROMPT", None)
with patch("anthropic.Anthropic") as MockClient:
MockClient.return_value.messages.create.return_value = fake_response
with patch.dict(sys.modules, {"anthropic": mock_anthropic}):
prompt = build_whisper_prompt(god_nodes)
assert "Machine learning" in prompt
@@ -66,9 +70,12 @@ def test_build_whisper_prompt_llm_failure_fallback():
"""If LLM call raises, falls back to topic-based prompt."""
god_nodes = [{"label": "kubernetes"}, {"label": "docker"}, {"label": "helm"}]
mock_anthropic = MagicMock()
mock_anthropic.Anthropic.return_value.messages.create.side_effect = Exception("API error")
with patch.dict(os.environ, {}, clear=False):
os.environ.pop("GRAPHIFY_WHISPER_PROMPT", None)
with patch("anthropic.Anthropic", side_effect=Exception("API error")):
with patch.dict(sys.modules, {"anthropic": mock_anthropic}):
prompt = build_whisper_prompt(god_nodes)
assert "kubernetes" in prompt.lower() or "docker" in prompt.lower()