Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin

- forward `catalog_dependency` in `OGCFeaturesFactory` and `OGCTilesFactory` when using `Endpoints` factory
- allow Factory's prefix with path parameter
- changed `database_url` type in `PostgresSettings` to always be of `pydantic.PostgresDsn` type
- `postgres_port` type in `PostgresSettings` to be of `integer` type
- remove additional `/` prefix for dbname when constructing the database url from individual parameters

### changed

Expand Down
50 changes: 50 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""test tipg settings classes."""

import pytest
from pydantic import ValidationError

from tipg.settings import PostgresSettings


def test_pg_settings(monkeypatch):
"""test PostgresSettings class."""
# Makes sure we don't have any pg env set
monkeypatch.delenv("DATABASE_URL", raising=False)
monkeypatch.delenv("POSTGRES_USER", raising=False)
monkeypatch.delenv("POSTGRES_PASS", raising=False)
monkeypatch.delenv("POSTGRES_HOST", raising=False)
monkeypatch.delenv("POSTGRES_PORT", raising=False)
monkeypatch.delenv("POSTGRES_DBNAME", raising=False)

# Should raises a validation error if no env or parameters is passed
with pytest.raises(ValidationError):
# we use `_env_file=None` to make sure pydantic do not use any `.env` files in local environment
PostgresSettings(_env_file=None)

settings = PostgresSettings(
postgres_user="user",
postgres_pass="secret",
postgres_host="0.0.0.0",
postgres_port=8888,
postgres_dbname="db",
_env_file=None,
)
assert str(settings.database_url) == "postgresql://user:secret@0.0.0.0:8888/db"

# Make sure pydantic will cast the port to integer
settings = PostgresSettings(
postgres_user="user",
postgres_pass="secret",
postgres_host="0.0.0.0",
postgres_port="8888",
postgres_dbname="db",
_env_file=None,
)
assert str(settings.database_url) == "postgresql://user:secret@0.0.0.0:8888/db"
assert settings.postgres_port == 8888

settings = PostgresSettings(
database_url="postgresql://user:secret@0.0.0.0:8888/db", _env_file=None
)
assert str(settings.database_url) == "postgresql://user:secret@0.0.0.0:8888/db"
assert not settings.postgres_port
12 changes: 7 additions & 5 deletions tipg/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""tipg config."""

import pathlib
from typing import Any, Dict, List, Optional
from typing import Dict, List, Optional

from pydantic import (
DirectoryPath,
Expand Down Expand Up @@ -117,7 +117,7 @@ class PostgresSettings(BaseSettings):
postgres_user: Optional[str] = None
postgres_pass: Optional[str] = None
postgres_host: Optional[str] = None
postgres_port: Optional[str] = None
postgres_port: Optional[int] = None
postgres_dbname: Optional[str] = None

database_url: Optional[PostgresDsn] = None
Expand All @@ -131,18 +131,20 @@ class PostgresSettings(BaseSettings):

# https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/master/%7B%7Bcookiecutter.project_slug%7D%7D/backend/app/app/core/config.py#L42
@field_validator("database_url", mode="before")
def assemble_db_connection(cls, v: Optional[str], info: FieldValidationInfo) -> Any:
def assemble_db_connection(
cls, v: Optional[str], info: FieldValidationInfo
) -> PostgresDsn:
"""Validate db url settings."""
if isinstance(v, str):
return v
return PostgresDsn(v)

return PostgresDsn.build(
scheme="postgresql",
username=info.data.get("postgres_user"),
password=info.data.get("postgres_pass"),
host=info.data.get("postgres_host", ""),
port=info.data.get("postgres_port", 5432),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pydantic will always cast the host to integer so there is no real need to add int() here, even if it would be technically correct.

path=f"/{info.data.get('postgres_dbname') or ''}",
path=info.data.get("postgres_dbname", ""),
)


Expand Down