-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 QA: Add test for load_knowledge_base edge case #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This QA note recommends a "fallback patch" with Prompt for AI agents
Suggested change
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The target Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: This patch target points to a non-existent module path ( Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+304
to
+323
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |
| with patch.object(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.object(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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This QA note recommends patching
app.api.embedding_agent..., but the repository does not contain anapppackage, so this guidance is likely incorrect and may lead to brittle tests. Consider updating the note to recommend patching the real import path used in tests/runtime (e.g.,api.embedding_agent.service.CLINICAL_EMBEDDER_AVAILABLE) and avoid suggesting fallback patches to non-importable module paths.