Skip to content

Commit a07e329

Browse files
committed
fix: correct test_config.py to work with pydantic-settings
- Use monkeypatch to set environment variables for pydantic-settings - pydantic-settings reads from env vars using aliases, not direct params - Tests now properly simulate environment variable configuration This fixes 4 failing tests related to DatabaseSettings and APISettings.
1 parent f1de18d commit a07e329

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

tests/unit/test_config.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,39 @@
1818
class TestDatabaseSettings:
1919
"""Tests for DatabaseSettings."""
2020

21-
def test_default_values(self) -> None:
21+
def test_default_values(self, monkeypatch: pytest.MonkeyPatch) -> None:
2222
"""Test default database settings."""
23+
# Clear any env vars that might interfere
24+
monkeypatch.delenv("DATABASE_URL", raising=False)
2325
settings = DatabaseSettings()
2426
assert settings.url == "sqlite:///./data/text2sql.db"
2527
assert settings.pool_size == 5
2628
assert settings.max_overflow == 10
2729
assert settings.pool_timeout == 30
2830

29-
def test_valid_postgresql_url(self) -> None:
31+
def test_valid_postgresql_url(self, monkeypatch: pytest.MonkeyPatch) -> None:
3032
"""Test valid PostgreSQL URL."""
31-
settings = DatabaseSettings(url="postgresql://user:pass@localhost/db")
33+
monkeypatch.setenv("DATABASE_URL", "postgresql://user:pass@localhost/db")
34+
settings = DatabaseSettings()
3235
assert settings.url.startswith("postgresql://")
3336

34-
def test_valid_mysql_url(self) -> None:
37+
def test_valid_mysql_url(self, monkeypatch: pytest.MonkeyPatch) -> None:
3538
"""Test valid MySQL URL."""
36-
settings = DatabaseSettings(url="mysql://user:pass@localhost/db")
39+
monkeypatch.setenv("DATABASE_URL", "mysql://user:pass@localhost/db")
40+
settings = DatabaseSettings()
3741
assert settings.url.startswith("mysql://")
3842

39-
def test_valid_sqlite_url(self) -> None:
43+
def test_valid_sqlite_url(self, monkeypatch: pytest.MonkeyPatch) -> None:
4044
"""Test valid SQLite URL."""
41-
settings = DatabaseSettings(url="sqlite:///./test.db")
45+
monkeypatch.setenv("DATABASE_URL", "sqlite:///./test.db")
46+
settings = DatabaseSettings()
4247
assert settings.url.startswith("sqlite:///")
4348

44-
def test_invalid_url_raises_error(self) -> None:
49+
def test_invalid_url_raises_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
4550
"""Test that invalid URL raises ValidationError."""
51+
monkeypatch.setenv("DATABASE_URL", "invalid://url")
4652
with pytest.raises(ValidationError):
47-
DatabaseSettings(url="invalid://url")
53+
DatabaseSettings()
4854

4955

5056
class TestAgentSettings:
@@ -87,21 +93,29 @@ def test_min_confidence_bounds(self) -> None:
8793
class TestAPISettings:
8894
"""Tests for APISettings."""
8995

90-
def test_cors_origins_list(self) -> None:
96+
def test_cors_origins_list(self, monkeypatch: pytest.MonkeyPatch) -> None:
9197
"""Test CORS origins parsing."""
92-
settings = APISettings(cors_origins="http://a.com, http://b.com")
98+
monkeypatch.setenv("API_CORS_ORIGINS", "http://a.com, http://b.com")
99+
settings = APISettings()
93100
assert settings.cors_origins_list == ["http://a.com", "http://b.com"]
94101

95-
def test_port_bounds(self) -> None:
102+
def test_port_bounds(self, monkeypatch: pytest.MonkeyPatch) -> None:
96103
"""Test port boundary validation."""
97-
APISettings(port=1)
98-
APISettings(port=65535)
104+
# Valid values
105+
monkeypatch.setenv("API_PORT", "1")
106+
APISettings()
107+
monkeypatch.setenv("API_PORT", "65535")
108+
APISettings()
99109

110+
# Invalid values - port 0
111+
monkeypatch.setenv("API_PORT", "0")
100112
with pytest.raises(ValidationError):
101-
APISettings(port=0)
113+
APISettings()
102114

115+
# Invalid values - port 65536
116+
monkeypatch.setenv("API_PORT", "65536")
103117
with pytest.raises(ValidationError):
104-
APISettings(port=65536)
118+
APISettings()
105119

106120

107121
class TestGetSettings:

0 commit comments

Comments
 (0)