Skip to content

Commit

Permalink
Merge pull request #25 from community-of-python/vrslev-patch-6
Browse files Browse the repository at this point in the history
Add env-var aliases to improve compatibility with existing internal services and pipelines
  • Loading branch information
vrslev authored Oct 10, 2024
2 parents cc1edab + 9120ab7 commit a921dd6
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 22 deletions.
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
repos:
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.1
rev: v1.11.2
hooks:
- id: mypy
name: mypy
always_run: true
additional_dependencies: [pydantic>=2.3.4]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
rev: v0.6.9
hooks:
- id: ruff
name: ruff-check
Expand Down
4 changes: 2 additions & 2 deletions microbootstrap/instruments/prometheus_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ def is_ready(self) -> bool:
)

@classmethod
def get_config_type(cls) -> type[BasePrometheusConfig]:
return BasePrometheusConfig
def get_config_type(cls) -> type[PrometheusConfigT]:
return BasePrometheusConfig # type: ignore[return-value]
14 changes: 10 additions & 4 deletions microbootstrap/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import typing

import pydantic
import pydantic_settings

from microbootstrap import (
Expand All @@ -16,7 +17,8 @@


SettingsT = typing.TypeVar("SettingsT", bound="BaseServiceSettings")
ENVIRONMENT_PREFIX: typing.Final = "ENVIRONMENT_PREFIX"
ENV_PREFIX_VAR_NAME: typing.Final = "ENVIRONMENT_PREFIX"
ENV_PREFIX: typing.Final = os.getenv(ENV_PREFIX_VAR_NAME, "")


# TODO: add offline docs and cors support # noqa: TD002
Expand All @@ -25,9 +27,13 @@ class BaseServiceSettings(
):
service_debug: bool = True
service_environment: str | None = None
service_name: str = "micro-service"
service_name: str = pydantic.Field(
"micro-service", validation_alias=pydantic.AliasChoices("SERVICE_NAME", f"{ENV_PREFIX}SERVICE_NAME")
)
service_description: str = "Micro service description"
service_version: str = "1.0.0"
service_version: str = pydantic.Field(
"1.0.0", validation_alias=pydantic.AliasChoices("CI_COMMIT_TAG", f"{ENV_PREFIX}SERVICE_VERSION")
)
service_static_path: str = "/static"

server_host: str = "0.0.0.0" # noqa: S104
Expand All @@ -37,7 +43,7 @@ class BaseServiceSettings(

model_config = pydantic_settings.SettingsConfigDict(
env_file=".env",
env_prefix=os.getenv(ENVIRONMENT_PREFIX, ""),
env_prefix=ENV_PREFIX,
env_file_encoding="utf-8",
populate_by_name=True,
)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.mypy]
plugins = ["pydantic.mypy"]
files = ["microbootstrap", "tests"]
python_version = "3.9"
strict = true
Expand Down
37 changes: 23 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import annotations
import importlib
import typing
from unittest.mock import AsyncMock, MagicMock

import litestar
import pytest

import microbootstrap.settings
from microbootstrap import (
FastApiPrometheusConfig,
LitestarPrometheusConfig,
Expand All @@ -26,47 +29,47 @@ def anyio_backend() -> str:
return "asyncio"


@pytest.fixture()
@pytest.fixture
def default_litestar_app() -> litestar.Litestar:
return litestar.Litestar()


@pytest.fixture()
@pytest.fixture
def minimal_sentry_config() -> SentryConfig:
return SentryConfig(sentry_dsn="https://[email protected]/0")


@pytest.fixture()
@pytest.fixture
def minimal_logging_config() -> LoggingConfig:
return LoggingConfig(service_debug=False)


@pytest.fixture()
@pytest.fixture
def minimal_base_prometheus_config() -> BasePrometheusConfig:
return BasePrometheusConfig()


@pytest.fixture()
@pytest.fixture
def minimal_fastapi_prometheus_config() -> FastApiPrometheusConfig:
return FastApiPrometheusConfig()


@pytest.fixture()
@pytest.fixture
def minimal_litestar_prometheus_config() -> LitestarPrometheusConfig:
return LitestarPrometheusConfig()


@pytest.fixture()
@pytest.fixture
def minimal_swagger_config() -> SwaggerConfig:
return SwaggerConfig()


@pytest.fixture()
def minimal_cors_config() -> SwaggerConfig:
@pytest.fixture
def minimal_cors_config() -> CorsConfig:
return CorsConfig(cors_allowed_origins=["*"])


@pytest.fixture()
@pytest.fixture
def minimal_opentelemetry_config() -> OpentelemetryConfig:
return OpentelemetryConfig(
service_name="test-micro-service",
Expand All @@ -77,21 +80,27 @@ def minimal_opentelemetry_config() -> OpentelemetryConfig:
)


@pytest.fixture()
@pytest.fixture
def base_settings() -> BaseServiceSettings:
return BaseServiceSettings()


@pytest.fixture()
@pytest.fixture
def magic_mock() -> MagicMock:
return MagicMock()


@pytest.fixture()
@pytest.fixture
def async_mock() -> AsyncMock:
return AsyncMock()


@pytest.fixture()
@pytest.fixture
def console_writer() -> ConsoleWriter:
return ConsoleWriter(writer_enabled=False)


@pytest.fixture
def reset_reloaded_settings_module() -> typing.Iterator[None]:
yield
importlib.reload(microbootstrap.settings)
38 changes: 38 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import importlib

import pytest

import microbootstrap.settings


pytestmark = [pytest.mark.usefixtures("reset_reloaded_settings_module")]


@pytest.mark.parametrize("alias", ["SERVICE_NAME", "MY_SERVICE_SERVICE_NAME"])
def test_settings_service_name_aliases(monkeypatch: pytest.MonkeyPatch, alias: str) -> None:
monkeypatch.setenv("ENVIRONMENT_PREFIX", "MY_SERVICE_")
monkeypatch.setenv(alias, "my service")
importlib.reload(microbootstrap.settings)

settings = microbootstrap.settings.BaseServiceSettings()
assert settings.service_name == "my service"


def test_settings_service_name_default() -> None:
settings = microbootstrap.settings.BaseServiceSettings()
assert settings.service_name == "micro-service"


@pytest.mark.parametrize("alias", ["CI_COMMIT_TAG", "MY_SERVICE_SERVICE_VERSION"])
def test_settings_service_version_aliases(monkeypatch: pytest.MonkeyPatch, alias: str) -> None:
monkeypatch.setenv("ENVIRONMENT_PREFIX", "MY_SERVICE_")
monkeypatch.setenv(alias, "1.2.3")
importlib.reload(microbootstrap.settings)

settings = microbootstrap.settings.BaseServiceSettings()
assert settings.service_version == "1.2.3"


def test_settings_service_version_default() -> None:
settings = microbootstrap.settings.BaseServiceSettings()
assert settings.service_version == "1.0.0"

0 comments on commit a921dd6

Please sign in to comment.