From 7186ed326106e024d2500423ea94503cb8ab90e9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 2 Apr 2026 02:00:56 +0000 Subject: [PATCH] Add tests for EmbeddingAgentService load_knowledge_base Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com> --- .Jules/qa.md | 1 + api/embedding_agent/tests/test_service.py | 35 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/.Jules/qa.md b/.Jules/qa.md index 4fdf367a..0d04b932 100644 --- a/.Jules/qa.md +++ b/.Jules/qa.md @@ -1 +1,2 @@ ## 2026-03-27 - Mocking File I/O for TranscriptCorrector | Pattern: Explicitly mock Path.exists and builtins.open when initializing utilities that read config files to avoid FileNotFoundError | Action: Use @patch('pathlib.Path.exists', return_value=True) and @patch('builtins.open', new_callable=mock_open) with a valid JSON string +## 2026-04-02 - Mocking Module-Level Variables for EmbeddingAgentService | Pattern: Patching module-level flags requires defensive mocking across multiple potential namespaces (e.g., `api.embedding_agent...` and `app.api.embedding_agent...` with `create=True`) depending on test runner execution path | Action: Apply `patch` directly to the module attribute path and add a fallback patch with `create=True` when testing behavior dependent on global flags. diff --git a/api/embedding_agent/tests/test_service.py b/api/embedding_agent/tests/test_service.py index 9162886d..1fdd225c 100644 --- a/api/embedding_agent/tests/test_service.py +++ b/api/embedding_agent/tests/test_service.py @@ -11,6 +11,8 @@ import pytest from pathlib import Path +from unittest.mock import MagicMock, patch +import api.embedding_agent.service from ..models import ( EmbeddingAgentConfig, @@ -288,6 +290,39 @@ def test_empty_batch(self, service: EmbeddingAgentService): BatchEmbeddingRequest(texts=[]) +class TestKnowledgeBase: + """Tests for knowledge base loading.""" + + def test_load_knowledge_base_not_available(self, config: EmbeddingAgentConfig): + """Test loading when clinical embedder is not available.""" + with patch.object(api.embedding_agent.service, 'CLINICAL_EMBEDDER_AVAILABLE', False): + service = EmbeddingAgentService(config) + assert service.load_knowledge_base() == 0 + + def test_load_knowledge_base_available(self, config: EmbeddingAgentConfig): + """Test loading when clinical embedder is available.""" + with patch('app.api.embedding_agent.service.CLINICAL_EMBEDDER_AVAILABLE', True, create=True): + with patch('api.embedding_agent.service.CLINICAL_EMBEDDER_AVAILABLE', True): + with patch.object(EmbeddingAgentService, '_initialize_clinical_embedder'): + service = EmbeddingAgentService(config) + service._clinical_embedder = MagicMock() + service._clinical_embedder.process_all_knowledge.return_value = (["item1", "item2"], None) + + assert service.load_knowledge_base() == 2 + assert service._knowledge_items == ["item1", "item2"] + + def test_load_knowledge_base_exception(self, config: EmbeddingAgentConfig): + """Test exception handling during knowledge base loading.""" + with patch('app.api.embedding_agent.service.CLINICAL_EMBEDDER_AVAILABLE', True, create=True): + with patch('api.embedding_agent.service.CLINICAL_EMBEDDER_AVAILABLE', True): + with patch.object(EmbeddingAgentService, '_initialize_clinical_embedder'): + service = EmbeddingAgentService(config) + service._clinical_embedder = MagicMock() + service._clinical_embedder.process_all_knowledge.side_effect = Exception("Test error") + + assert service.load_knowledge_base() == 0 + + if __name__ == "__main__": pytest.main([__file__, "-v"])