Skip to content

Commit 966e27f

Browse files
committed
test: switch to placeholder-based validation for DB path
1 parent b4f2d00 commit 966e27f

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

Makefile

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/config.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
from pathlib import Path
22

3-
from pydantic_settings import BaseSettings, SettingsConfigDict
3+
from pydantic import field_validator
4+
from pydantic_settings import BaseSettings
45

56
UPLOAD_DIR = Path("data")
67

8+
# Used to catch missing DB_PATH.
9+
# If this value is still set, it means DB_PATH wasn't configured.
10+
DEFAULT_DB_PLACEHOLDER = "__MISSING_DB_PATH__"
11+
712

813
class Settings(BaseSettings):
9-
db_path: str = "database.db"
14+
db_path: str = DEFAULT_DB_PLACEHOLDER
1015

11-
model_config = SettingsConfigDict(env_file=".env") #' Optional
16+
@field_validator("db_path", mode="before")
17+
@classmethod
18+
def validate_db_path(cls, value):
19+
if value == DEFAULT_DB_PLACEHOLDER:
20+
raise ValueError("You must set DB_PATH in your environment or .env file.")
21+
return value
1222

1323

1424
settings = Settings()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ dependencies = [
1818
dev = [
1919
"ipykernel>=6.29.5",
2020
"pytest>=8.3.5",
21+
"pytest-env>=1.1.5",
2122
]

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
env =
3+
ENV=test
4+
DB_PATH=test.db

tests/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616

1717
@pytest.fixture(scope="session", autouse=True)
1818
def setup_test_db():
19-
os.environ["DB_PATH"] = "test.db"
2019
init_db()
2120
yield
22-
Path("test.db").unlink(missing_ok=True)
21+
Path(os.getenv("DB_PATH")).unlink(missing_ok=True)
2322

2423

2524
@pytest.fixture

uv.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)