Skip to content

Commit

Permalink
Merge pull request #87 from caarmen/custom-app-config-for-tests
Browse files Browse the repository at this point in the history
Issue #83 - Step 2 - Provide an alternate app config yaml file for tests.
  • Loading branch information
caarmen authored Dec 15, 2024
2 parents f1988a0 + 63dc371 commit 36500b0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
17 changes: 12 additions & 5 deletions slackhealthbot/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import dataclasses
import datetime as dt
import os
from pathlib import Path
from tempfile import NamedTemporaryFile

import yaml
from pydantic import AnyHttpUrl, BaseModel, HttpUrl
Expand Down Expand Up @@ -73,7 +75,6 @@ class AppSettings(BaseSettings):
fitbit: Fitbit
model_config = SettingsConfigDict(
env_nested_delimiter="__",
yaml_file="config/app-merged.yaml",
)

@classmethod
Expand All @@ -93,7 +94,10 @@ def _load_yaml_file(
@classmethod
def _load_merged_config(cls) -> dict:
default_config = cls._load_yaml_file("config/app-default.yaml", required=True)
custom_config = cls._load_yaml_file("config/app-custom.yaml", required=False)
custom_config = cls._load_yaml_file(
os.environ.get("SHB_CUSTOM_CONFIG_PATH", "config/app-custom.yaml"),
required=False,
)
return deep_update(default_config, custom_config)

@classmethod
Expand All @@ -105,9 +109,12 @@ def settings_customise_sources(
**kwargs,
) -> tuple[PydanticBaseSettingsSource, ...]:
merged_config = cls._load_merged_config()
with open("config/app-merged.yaml", "w", encoding="utf-8") as file:
yaml.safe_dump(merged_config, file)
yaml_settings_source = YamlConfigSettingsSource(settings_cls)
with NamedTemporaryFile(mode="w") as file:
yaml.safe_dump(merged_config, file.file)
yaml_settings_source = YamlConfigSettingsSource(
settings_cls,
yaml_file=file.name,
)
return (env_settings, yaml_settings_source)

@property
Expand Down
32 changes: 32 additions & 0 deletions tests/testsupport/config/app-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# App config for test scenarios

logging:
sql_log_level: "DEBUG"

fitbit:
activities:
activity_types:
- name: Dancing
id: 123
report_daily: true
report_realtime: true

- name: Treadmill
id: 90019
report_daily: true
report_realtime: false

- name: Spinning
id: 55001
report_daily: false
report_realtime: true

- name: Walking
id: 55001
report_daily: false
report_realtime: true

- name: Walk
id: 90013
report_daily: false
report_realtime: true
14 changes: 12 additions & 2 deletions tests/testsupport/fixtures/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def client(mocked_async_session) -> TestClient:
return TestClient(app)


@pytest.fixture
def settings() -> Settings:
@pytest.fixture(autouse=True)
def settings(monkeypatch: pytest.MonkeyPatch) -> Settings:
monkeypatch.setenv(
"SHB_CUSTOM_CONFIG_PATH", "tests/testsupport/config/app-test.yaml"
)
return app.container.settings.provided()


@pytest.fixture(autouse=True)
def reset_container():
# Reset singletons for each test.
# https://github.com/ets-labs/python-dependency-injector/issues/421
app.container.reset_singletons()

0 comments on commit 36500b0

Please sign in to comment.