Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Berlakovich committed Jan 2, 2025
1 parent 33b7529 commit 7ac36ac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
Empty file added tests/__init__.py
Empty file.
17 changes: 8 additions & 9 deletions tests/config_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import pydantic
import pytest

from kptncook.config import Settings
from tests.conftest import temp_env
from tests.conftest import MockSettings


def test_config_accepts_mealie_username_password():
with temp_env(MEALIE_USERNAME="test", MEALIE_PASSWORD="test", KPTNCOOK_API_KEY="test"):
settings = Settings()
MockSettings(
mealie_username="test", mealie_password="password", kptncook_api_key="test"
)


def test_config_accepts_mealie_token():
with temp_env(MEALIE_API_TOKEN="test", KPTNCOOK_API_KEY="test"):
settings = Settings()
MockSettings(mealie_api_token="test", kptncook_api_key="test")


def test_config_rejects_empty_mealie_auth():
with pytest.raises(pydantic.ValidationError) as exception_info:
with temp_env(KPTNCOOK_API_KEY="test"):
settings = Settings()
MockSettings(kptncook_api_key="test")

assert "mealie" in str(exception_info.value)
assert "must specify either" in str(exception_info.value)
35 changes: 16 additions & 19 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import contextlib
import json
import os
from pathlib import Path

import pytest
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource

import kptncook.config as config

# make sure settings need be specified explicitly during tests
if "env_file" in config.Settings.model_config:
del config.Settings.model_config["env_file"]

class MockSettings(config.Settings):
model_config = config.Settings.model_config

@contextlib.contextmanager
def temp_env(**environ):
original = dict(os.environ)
os.environ.update(environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(original)
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (init_settings,)


@pytest.fixture(scope="function")
def test_settings():
with temp_env(
MEALIE_USERNAME="test", MEALIE_PASSWORD="test", KPTNCOOK_API_KEY="test"
):
settings = config.Settings()
yield settings
return MockSettings(
mealie_username="test", mealie_password="password", kptncook_api_key="test"
)


@pytest.fixture
Expand Down

0 comments on commit 7ac36ac

Please sign in to comment.