~homeworkprod/byceps

d216b81aa0b76a6874eaa35c4a5c6595a7351075 — Jochen Kupperschmidt 9 months ago 2c36ca1
Inline `byceps.util.system.get_env_value` into sole callsite
3 files changed, 9 insertions(+), 64 deletions(-)

M app_metrics.py
D byceps/util/system.py
D tests/unit/util/test_system.py
M app_metrics.py => app_metrics.py +9 -7
@@ 6,18 6,20 @@ metrics application instance
:License: Revised BSD (see `LICENSE` file for details)
"""

import os

from byceps.config import ConfigurationError
from byceps.metrics.application import create_app
from byceps.util.system import get_env_value


ENV_VAR_NAME_DATABASE_URI = 'DATABASE_URI'


database_uri = get_env_value(
    ENV_VAR_NAME_DATABASE_URI,
    f"No database URI was specified via the '{ENV_VAR_NAME_DATABASE_URI}' "
    "environment variable.",
)

database_uri = os.environ.get(ENV_VAR_NAME_DATABASE_URI)
if not database_uri:
    raise ConfigurationError(
        f"No database URI was specified via the '{ENV_VAR_NAME_DATABASE_URI}' "
        "environment variable.",
    )

app = create_app(database_uri)

D byceps/util/system.py => byceps/util/system.py +0 -24
@@ 1,24 0,0 @@
"""
byceps.util.system
~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

import os

from ..config import ConfigurationError


def get_env_value(name: str, error_message: str) -> str:
    """Return the value of the environment variable.

    Raise an exception if it isn't set or if it is empty.
    """
    env = os.environ.get(name)

    if not env:
        raise ConfigurationError(error_message)

    return env

D tests/unit/util/test_system.py => tests/unit/util/test_system.py +0 -33
@@ 1,33 0,0 @@
"""
:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

import pytest

from byceps.config import ConfigurationError
from byceps.util.system import get_env_value


def test_get_env_value_with_value(monkeypatch):
    monkeypatch.setenv('LAUNCH_CODE', '3-2-1-LIFT-OFF')

    actual = get_env_value('LAUNCH_CODE', 'Invalid launch code')

    assert actual == '3-2-1-LIFT-OFF'


def test_get_env_value_with_empty_value(monkeypatch):
    monkeypatch.setenv('LAUNCH_CODE', '')

    with pytest.raises(ConfigurationError) as excinfo:
        get_env_value('LAUNCH_CODE', 'Invalid launch code')

    assert str(excinfo.value) == 'Invalid launch code'


def test_get_env_value_without_value():
    with pytest.raises(ConfigurationError) as excinfo:
        get_env_value('LAUNCH_CODE', 'Invalid launch code')

    assert str(excinfo.value) == 'Invalid launch code'