Skip to content

Commit 8f6b02b

Browse files
authored
Merge pull request #31 from getmarkus/cm-branch-29
feat: enhance settings management and clean up config
2 parents c8730f9 + b27f422 commit 8f6b02b

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

app/tests/conftest.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@
44

55
import pytest
66
from _pytest.config import Config
7+
78
from dotenv import load_dotenv
89
from fastapi import FastAPI
910
from fastapi.testclient import TestClient
1011
from loguru import logger
1112
from sqlmodel import Session, delete
12-
13+
from config import get_settings # noqa: E402
1314

1415
# Specify the custom .env file
1516
# don't change ordering here, settings must be called prior to initialization of app.core.factory
1617
dotenv_path = Path(".env.testing")
1718
load_dotenv(dotenv_path=dotenv_path, override=True)
1819

20+
settings = get_settings()
21+
1922
from app.core.factory import create_app # noqa: E402
2023
from app.resource_adapters.persistence.sqlmodel.database import get_engine # noqa: E402
2124
from app.resource_adapters.persistence.sqlmodel.issues import Issue # noqa: E402
22-
from config import get_settings # noqa: E402
23-
24-
25-
settings = get_settings()
2625

2726

2827
def pytest_unconfigure(config: Config) -> None:
2928
"""Clean up after each test."""
3029
# Extract database path from the database URL
3130
db_url = settings.database_url
32-
if db_url.startswith("sqlite:///") and not db_url.endswith(":memory:"):
31+
if "memory" not in db_url:
3332
# Remove the sqlite:/// prefix to get the file path
3433
db_path = db_url.replace("sqlite:///", "")
3534
# Remove ./ prefix if present

config.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from typing import List, Literal
22

33
from pydantic import AnyHttpUrl
4-
from pydantic_settings import BaseSettings, SettingsConfigDict
4+
from pydantic_settings import (
5+
BaseSettings,
6+
SettingsConfigDict,
7+
PydanticBaseSettingsSource,
8+
)
9+
from typing import Type
510
from functools import lru_cache
611

712

@@ -15,13 +20,6 @@ class Settings(BaseSettings):
1520
database_type: str = "sqlmodel"
1621
current_env: Literal["testing", "default"]
1722

18-
@property
19-
def get_table_schema(self) -> str | None:
20-
"""Return table_schema if database_url does not start with sqlite, otherwise return None."""
21-
if self.database_url.startswith("sqlite"):
22-
return None
23-
return self.database_schema
24-
2523
# CORS settings
2624
backend_cors_origins: List[str] = ["http://localhost:8000", "http://localhost:3000"]
2725
cors_allow_credentials: bool = False
@@ -43,19 +41,33 @@ def get_table_schema(self) -> str | None:
4341
extra="ignore",
4442
)
4543

46-
""" @field_validator("database_schema")
47-
def validate_schema(cls, v: str | None, values) -> str | None:
48-
# Only set database_schema if database_url doesn't start with sqlite
49-
if values.data.get("database_url", "").startswith("sqlite"):
44+
@property
45+
def get_table_schema(self) -> str | None:
46+
"""Return table_schema if database_url does not start with sqlite, otherwise return None."""
47+
if self.database_url.startswith("sqlite"):
5048
return None
51-
return v """
49+
return self.database_schema
5250

5351
@property
5452
def backend_cors_origins_list(self) -> List[AnyHttpUrl]:
5553
return [AnyHttpUrl(origin) for origin in self.backend_cors_origins]
5654

55+
@classmethod
56+
def settings_customise_sources(
57+
cls,
58+
settings_cls: Type[BaseSettings],
59+
init_settings: PydanticBaseSettingsSource,
60+
env_settings: PydanticBaseSettingsSource,
61+
dotenv_settings: PydanticBaseSettingsSource,
62+
file_secret_settings: PydanticBaseSettingsSource,
63+
) -> tuple[PydanticBaseSettingsSource, ...]:
64+
return (
65+
env_settings,
66+
dotenv_settings,
67+
file_secret_settings,
68+
)
69+
5770

58-
# Singleton pattern to ensure only one settings instance
5971
@lru_cache()
6072
def get_settings() -> "Settings":
6173
return Settings()

0 commit comments

Comments
 (0)