Skip to content

Commit e2fbcf7

Browse files
authored
Make DCApp tests hermetic (#164)
1 parent 8ab52f9 commit e2fbcf7

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

packages/datacommons-mcp/datacommons_mcp/data_models/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __init__(self, **kwargs: dict[str, Any]) -> None:
123123
api_base_url: str | None = Field(
124124
default=None,
125125
alias="DC_API_BASE_URL",
126-
description="API base URL (computed from base_url if not provided)",
126+
description="API base URL (computed from CUSTOM_DC_URL if not provided)",
127127
)
128128
search_scope: SearchScope = Field(
129129
default=SearchScope.BASE_AND_CUSTOM,

packages/datacommons-mcp/tests/test_app.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
# limitations under the License.
1414
"""
1515
Unit tests for the DCApp class.
16+
17+
NOTE: We import DCApp locally within each test function intentionally.
18+
Importing DCApp at the module level would trigger settings initialization
19+
(and potential .env loading) before the `clean_env` fixture in `conftest.py`
20+
has a chance to isolate the environment. This ensures hermetic tests.
1621
"""
1722

1823
from unittest.mock import MagicMock, patch
1924

2025
import pytest
21-
from datacommons_mcp.app import DCApp
2226

2327

2428
@pytest.fixture
@@ -48,6 +52,8 @@ def mock_fastmcp():
4852

4953
def test_app_initialization_default(mock_settings, mock_fastmcp): # noqa: ARG001
5054
"""Test that DCApp initializes with default instructions."""
55+
from datacommons_mcp.app import DCApp
56+
5157
_ = DCApp()
5258

5359
# Verify FastMCP was initialized with default instructions
@@ -68,6 +74,8 @@ def test_app_initialization_override(
6874
# Configure settings to use custom dir
6975
mock_settings.return_value.instructions_dir = str(custom_dir)
7076

77+
from datacommons_mcp.app import DCApp
78+
7179
_ = DCApp()
7280

7381
# Verify FastMCP was initialized with custom instructions
@@ -85,6 +93,8 @@ def test_load_instruction_tool_override(mock_settings, tmp_path, create_test_fil
8593
# Configure settings to use custom dir
8694
mock_settings.return_value.instructions_dir = str(custom_dir)
8795

96+
from datacommons_mcp.app import DCApp
97+
8898
app = DCApp()
8999
content = app._load_instruction("tools/test_tool.md")
90100
assert content == "Custom Tool Instructions"
@@ -99,6 +109,8 @@ def test_load_instruction_fallback(mock_settings, tmp_path):
99109
# Configure settings to use custom dir
100110
mock_settings.return_value.instructions_dir = str(custom_dir)
101111

112+
from datacommons_mcp.app import DCApp
113+
102114
app = DCApp()
103115

104116
# Should fall back to default package resource (server.md exists in package)
@@ -112,6 +124,8 @@ def test_register_tool(mock_settings, mock_fastmcp, tmp_path, create_test_file):
112124
create_test_file("instructions/tools/sample.md", "Sample Tool Description")
113125
mock_settings.return_value.instructions_dir = str(tmp_path / "instructions")
114126

127+
from datacommons_mcp.app import DCApp
128+
115129
app = DCApp()
116130
mock_mcp_instance = mock_fastmcp.return_value
117131

0 commit comments

Comments
 (0)