test: update tests for cache mode terminology

Rename database mode tests to cache mode to reflect new architecture:
- Replace USE_DATABASE with USE_CACHE references
- Update test assertions for cache behavior
- Maintain backward compatibility testing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-03-14 13:41:05 -04:00
parent 4405f199ba
commit fc99173028
2 changed files with 145 additions and 35 deletions
+66 -10
View File
@@ -1,13 +1,22 @@
"""Tests for LLM analysis functionality."""
import pytest
from unittest.mock import Mock, MagicMock
from unittest.mock import Mock, MagicMock, patch
from SPARC.llm import LLMAnalyzer
class TestLLMAnalyzer:
"""Test LLM analyzer initialization and API interaction."""
@pytest.fixture(autouse=True)
def mock_database(self, mocker):
"""Mock the database client for all tests."""
mock_db_client = Mock()
mock_db_client.get_cached_response.return_value = None # No cache hit by default
mock_db_client.store_message.return_value = 1
mocker.patch("SPARC.llm.DatabaseClient", return_value=mock_db_client)
return mock_db_client
def test_analyzer_initialization_with_api_key(self, mocker):
"""Test that analyzer initializes with provided API key."""
mock_openai = mocker.patch("SPARC.llm.OpenAI")
@@ -25,7 +34,7 @@ class TestLLMAnalyzer:
mock_openai = mocker.patch("SPARC.llm.OpenAI")
mock_config = mocker.patch("SPARC.llm.config")
mock_config.openrouter_api_key = "config-key-456"
mock_config.use_database = False
mock_config.use_cache = True
mock_config.database_url = "postgresql://localhost/test"
analyzer = LLMAnalyzer()
@@ -35,7 +44,7 @@ class TestLLMAnalyzer:
base_url="https://openrouter.ai/api/v1"
)
def test_analyze_patent_content(self, mocker):
def test_analyze_patent_content(self, mocker, mock_database):
"""Test single patent content analysis."""
mock_openai = mocker.patch("SPARC.llm.OpenAI")
mock_client = Mock()
@@ -44,9 +53,10 @@ class TestLLMAnalyzer:
# Mock the API response
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content="Innovative GPU architecture."))]
mock_response.usage = Mock(prompt_tokens=100, completion_tokens=50, total_tokens=150)
mock_client.chat.completions.create.return_value = mock_response
analyzer = LLMAnalyzer(api_key="test-key")
analyzer = LLMAnalyzer(api_key="test-key", use_cache=False)
result = analyzer.analyze_patent_content(
patent_content="ABSTRACT: GPU with new cache design...",
company_name="NVIDIA",
@@ -61,7 +71,32 @@ class TestLLMAnalyzer:
assert "NVIDIA" in prompt_text
assert "GPU with new cache design" in prompt_text
def test_analyze_patent_portfolio(self, mocker):
# Verify message was stored in database
mock_database.store_message.assert_called_once()
def test_analyze_patent_content_cache_hit(self, mocker, mock_database):
"""Test that cached responses are returned without API call."""
mock_openai = mocker.patch("SPARC.llm.OpenAI")
mock_client = Mock()
mock_openai.return_value = mock_client
# Set up cache hit
mock_database.get_cached_response.return_value = {
"id": 1,
"response": "Cached analysis result"
}
analyzer = LLMAnalyzer(api_key="test-key", use_cache=True)
result = analyzer.analyze_patent_content(
patent_content="ABSTRACT: GPU with new cache design...",
company_name="NVIDIA",
)
assert result == "Cached analysis result"
# API should NOT be called on cache hit
mock_client.chat.completions.create.assert_not_called()
def test_analyze_patent_portfolio(self, mocker, mock_database):
"""Test portfolio analysis with multiple patents."""
mock_openai = mocker.patch("SPARC.llm.OpenAI")
mock_client = Mock()
@@ -72,9 +107,10 @@ class TestLLMAnalyzer:
mock_response.choices = [
Mock(message=Mock(content="Strong portfolio in AI and graphics."))
]
mock_response.usage = Mock(prompt_tokens=200, completion_tokens=100, total_tokens=300)
mock_client.chat.completions.create.return_value = mock_response
analyzer = LLMAnalyzer(api_key="test-key")
analyzer = LLMAnalyzer(api_key="test-key", use_cache=False)
patents_data = [
{"patent_id": "US123", "content": "AI acceleration patent"},
{"patent_id": "US456", "content": "Graphics rendering patent"},
@@ -95,7 +131,7 @@ class TestLLMAnalyzer:
assert "AI acceleration patent" in prompt_text
assert "Graphics rendering patent" in prompt_text
def test_analyze_patent_portfolio_with_correct_token_limit(self, mocker):
def test_analyze_patent_portfolio_with_correct_token_limit(self, mocker, mock_database):
"""Test that portfolio analysis uses higher token limit."""
mock_openai = mocker.patch("SPARC.llm.OpenAI")
mock_client = Mock()
@@ -103,9 +139,10 @@ class TestLLMAnalyzer:
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content="Analysis result."))]
mock_response.usage = Mock(prompt_tokens=100, completion_tokens=50, total_tokens=150)
mock_client.chat.completions.create.return_value = mock_response
analyzer = LLMAnalyzer(api_key="test-key")
analyzer = LLMAnalyzer(api_key="test-key", use_cache=False)
patents_data = [{"patent_id": "US123", "content": "Test content"}]
analyzer.analyze_patent_portfolio(patents_data, "TestCo")
@@ -114,7 +151,7 @@ class TestLLMAnalyzer:
# Portfolio analysis should use 2048 tokens
assert call_args[1]["max_tokens"] == 2048
def test_analyze_single_patent_with_correct_token_limit(self, mocker):
def test_analyze_single_patent_with_correct_token_limit(self, mocker, mock_database):
"""Test that single patent analysis uses lower token limit."""
mock_openai = mocker.patch("SPARC.llm.OpenAI")
mock_client = Mock()
@@ -122,11 +159,30 @@ class TestLLMAnalyzer:
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content="Analysis result."))]
mock_response.usage = Mock(prompt_tokens=100, completion_tokens=50, total_tokens=150)
mock_client.chat.completions.create.return_value = mock_response
analyzer = LLMAnalyzer(api_key="test-key")
analyzer = LLMAnalyzer(api_key="test-key", use_cache=False)
analyzer.analyze_patent_content("Test content", "TestCo")
call_args = mock_client.chat.completions.create.call_args
# Single patent should use 1024 tokens
assert call_args[1]["max_tokens"] == 1024
def test_database_always_initialized(self, mocker, mock_database):
"""Test that database client is always initialized."""
mock_openai = mocker.patch("SPARC.llm.OpenAI")
analyzer = LLMAnalyzer(api_key="test-key")
assert analyzer.db_client is not None
def test_no_api_key_stores_placeholder(self, mocker, mock_database):
"""Test that without API key, a placeholder is stored."""
mocker.patch("SPARC.llm.config")
analyzer = LLMAnalyzer(use_cache=False)
result = analyzer.analyze_patent_content("Test content", "TestCo")
assert "[NO API]" in result
mock_database.store_message.assert_called_once()