Skip to content

Commit 2f48ecc

Browse files
committed
Fix pytest for config
Signed-off-by: Mihai Criveti <[email protected]>
1 parent f9e5716 commit 2f48ecc

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

mcpgateway/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
It loads configuration from environment variables with sensible defaults.
1010
1111
Environment variables:
12-
- APP_NAME: Gateway name (default: "MCP Gateway")
13-
- HOST: Host to bind to (default: "0.0.0.0")
12+
- APP_NAME: Gateway name (default: "MCP_Gateway")
13+
- HOST: Host to bind to (default: "127.0.0.1")
1414
- PORT: Port to listen on (default: 4444)
1515
- DATABASE_URL: SQLite database URL (default: "sqlite:///./mcp.db")
1616
- BASIC_AUTH_USER: Admin username (default: "admin")
@@ -47,7 +47,7 @@ class Settings(BaseSettings):
4747
"""MCP Gateway configuration settings."""
4848

4949
# Basic Settings
50-
app_name: str = Field("MCP Gateway", env="APP_NAME")
50+
app_name: str = Field("MCP_Gateway", env="APP_NAME")
5151
host: str = Field("127.0.0.1", env="HOST")
5252
port: int = Field(4444, env="PORT")
5353
database_url: str = "sqlite:///./mcp.db"

tests/unit/mcpgateway/test_config.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,29 @@
77
88
"""
99

10-
from unittest.mock import patch
10+
from unittest.mock import patch, MagicMock
1111

1212
from mcpgateway.config import Settings, get_settings
1313

1414

1515
def test_settings_default_values():
16-
"""Test that Settings has expected default values."""
17-
settings = Settings()
18-
assert settings.app_name == "MCP Gateway"
19-
assert settings.host == "0.0.0.0"
16+
"""
17+
Verify the class defaults only, independent of anything in the
18+
developer's local .env file. Passing ``_env_file=None`` tells
19+
Pydantic not to load any environment file.
20+
"""
21+
settings = Settings(_env_file=None)
22+
23+
assert settings.app_name == "MCP_Gateway"
24+
assert settings.host == "127.0.0.1"
2025
assert settings.port == 4444
2126
assert settings.database_url == "sqlite:///./mcp.db"
2227
assert settings.basic_auth_user == "admin"
2328
assert settings.basic_auth_password == "changeme"
2429
assert settings.auth_required is True
2530

2631

32+
2733
def test_api_key_property():
2834
"""Test the api_key property."""
2935
settings = Settings(basic_auth_user="test_user", basic_auth_password="test_pass")
@@ -53,17 +59,35 @@ def test_supports_transport_properties():
5359

5460
@patch("mcpgateway.config.Settings")
5561
def test_get_settings_caching(mock_settings):
56-
"""Test that get_settings caches the result."""
57-
mock_settings.return_value = "test_settings"
62+
"""
63+
Ensure get_settings() calls the Settings constructor exactly once and
64+
then serves the cached object on every additional call.
65+
"""
66+
# Clear the cache created at import-time.
67+
get_settings.cache_clear()
68+
69+
# Two distinct mock Settings instances, each with the methods that
70+
# get_settings() invokes (validate_transport / validate_database).
71+
settings_instance_1 = MagicMock()
72+
settings_instance_2 = MagicMock()
73+
74+
# Each mock must expose these methods so AttributeError is not raised.
75+
settings_instance_1.validate_transport.return_value = None
76+
settings_instance_1.validate_database.return_value = None
77+
settings_instance_2.validate_transport.return_value = None
78+
settings_instance_2.validate_database.return_value = None
79+
80+
# First call should return instance_1; any further constructor calls
81+
# would return instance_2 (but they shouldn't happen).
82+
mock_settings.side_effect = [settings_instance_1, settings_instance_2]
5883

59-
# First call should create settings
6084
result1 = get_settings()
61-
assert result1 == "test_settings"
85+
assert result1 is settings_instance_1
6286

63-
# Second call should use cached value
64-
mock_settings.return_value = "new_settings"
87+
# Even after we change what the constructor would return, the cached
88+
# object must still be served.
6589
result2 = get_settings()
66-
assert result2 == "test_settings" # Should still be the first value
90+
assert result2 is settings_instance_1
6791

68-
# Settings should only be created once
92+
# The constructor should have been invoked exactly once.
6993
assert mock_settings.call_count == 1

0 commit comments

Comments
 (0)