velofy/tests/test_config.py

136 lines
4.6 KiB
Python
Raw Normal View History

"""Test the package's configuration module."""
2026-11-24 17:15:43 +01:00
import warnings
import pytest
from velofy import configuration
2026-11-28 03:08:55 +01:00
envs = ["production", "testing"]
2026-11-28 03:08:55 +01:00
@pytest.mark.parametrize("env", envs)
def test_config_repr(env):
"""Config objects have the text representation '<configuration>'."""
config = configuration.make_config(env)
2026-11-28 03:08:55 +01:00
assert str(config) == "<configuration>"
def test_invalid_config():
"""There are only 'production' and 'testing' configurations."""
with pytest.raises(ValueError, match="'production' or 'testing'"):
2026-11-28 03:08:55 +01:00
configuration.make_config("invalid")
2026-11-28 03:08:55 +01:00
@pytest.mark.parametrize("env", envs)
def test_database_uri_set(env, monkeypatch):
"""Package does NOT emit warning if DATABASE_URI is set."""
2026-11-28 03:08:55 +01:00
uri = "postgresql://user:password@localhost/db"
monkeypatch.setattr(configuration.ProductionConfig, "DATABASE_URI", uri)
monkeypatch.setattr(configuration.TestingConfig, "DATABASE_URI", uri)
# Prevent that a warning is emitted for a missing R_LIBS_PATH.
2026-11-28 03:08:55 +01:00
monkeypatch.setattr(configuration.Config, "R_LIBS_PATH", ".cache/r_libs")
2026-11-24 17:15:43 +01:00
with warnings.catch_warnings(record=True) as record:
2026-11-28 03:08:55 +01:00
warnings.simplefilter("always")
configuration.make_config(env)
2026-11-28 02:01:12 +01:00
assert len(record) == 0
2026-11-28 03:08:55 +01:00
@pytest.mark.parametrize("env", envs)
def test_no_database_uri_set_with_testing_env_var(env, monkeypatch):
"""Package does not work without DATABASE_URI set in the environment."""
2026-11-28 03:08:55 +01:00
monkeypatch.setattr(configuration.ProductionConfig, "DATABASE_URI", None)
monkeypatch.setattr(configuration.TestingConfig, "DATABASE_URI", None)
2026-11-28 03:08:55 +01:00
monkeypatch.setenv("TESTING", "true")
# Prevent that a warning is emitted for a missing R_LIBS_PATH.
2026-11-28 03:08:55 +01:00
monkeypatch.setattr(configuration.Config, "R_LIBS_PATH", ".cache/r_libs")
2026-11-24 17:15:43 +01:00
with warnings.catch_warnings(record=True) as record:
2026-11-28 03:08:55 +01:00
warnings.simplefilter("always")
configuration.make_config(env)
2026-11-28 02:01:12 +01:00
assert len(record) == 0
2026-11-28 03:08:55 +01:00
@pytest.mark.parametrize("env", envs)
def test_no_database_uri_set_without_testing_env_var(env, monkeypatch):
"""Package does not work without DATABASE_URI set in the environment."""
2026-11-28 03:08:55 +01:00
monkeypatch.setattr(configuration.ProductionConfig, "DATABASE_URI", None)
monkeypatch.setattr(configuration.TestingConfig, "DATABASE_URI", None)
2026-11-28 03:08:55 +01:00
monkeypatch.delenv("TESTING", raising=False)
# Prevent that a warning is emitted for a missing R_LIBS_PATH.
2026-11-28 03:08:55 +01:00
monkeypatch.setattr(configuration.Config, "R_LIBS_PATH", ".cache/r_libs")
2026-11-28 03:08:55 +01:00
with pytest.warns(UserWarning, match="no DATABASE_URI"):
configuration.make_config(env)
2026-11-28 03:08:55 +01:00
@pytest.mark.parametrize("env", envs)
def test_r_libs_path_set(env, monkeypatch):
"""Package does NOT emit a warning if R_LIBS is set in the environment."""
2026-11-28 03:08:55 +01:00
monkeypatch.setattr(configuration.Config, "R_LIBS_PATH", ".cache/r_libs")
# Prevent that a warning is emitted for a missing DATABASE_URI.
2026-11-28 03:08:55 +01:00
uri = "postgresql://user:password@localhost/db"
monkeypatch.setattr(configuration.ProductionConfig, "DATABASE_URI", uri)
2026-11-24 17:15:43 +01:00
with warnings.catch_warnings(record=True) as record:
2026-11-28 03:08:55 +01:00
warnings.simplefilter("always")
configuration.make_config(env)
2026-11-28 02:01:12 +01:00
assert len(record) == 0
2026-11-28 03:08:55 +01:00
@pytest.mark.parametrize("env", envs)
def test_no_r_libs_path_set_with_testing_env_var(env, monkeypatch):
"""Package emits a warning if no R_LIBS is set in the environment ...
... when not testing.
"""
2026-11-28 03:08:55 +01:00
monkeypatch.setattr(configuration.Config, "R_LIBS_PATH", None)
monkeypatch.setenv("TESTING", "true")
# Prevent that a warning is emitted for a missing DATABASE_URI.
2026-11-28 03:08:55 +01:00
uri = "postgresql://user:password@localhost/db"
monkeypatch.setattr(configuration.ProductionConfig, "DATABASE_URI", uri)
2026-11-24 17:15:43 +01:00
with warnings.catch_warnings(record=True) as record:
2026-11-28 03:08:55 +01:00
warnings.simplefilter("always")
configuration.make_config(env)
2026-11-28 02:01:12 +01:00
assert len(record) == 0
2026-11-28 03:08:55 +01:00
@pytest.mark.parametrize("env", envs)
def test_no_r_libs_path_set_without_testing_env_var(env, monkeypatch):
"""Package emits a warning if no R_LIBS is set in the environment ...
... when not testing.
"""
2026-11-28 03:08:55 +01:00
monkeypatch.setattr(configuration.Config, "R_LIBS_PATH", None)
monkeypatch.delenv("TESTING", raising=False)
# Prevent that a warning is emitted for a missing DATABASE_URI.
2026-11-28 03:08:55 +01:00
uri = "postgresql://user:password@localhost/db"
monkeypatch.setattr(configuration.ProductionConfig, "DATABASE_URI", uri)
2026-11-28 03:08:55 +01:00
with pytest.warns(UserWarning, match="no R_LIBS"):
configuration.make_config(env)
def test_random_testing_schema():
"""CLEAN_SCHEMA is randomized if not set explicitly."""
result = configuration.random_schema_name()
assert isinstance(result, str)
2026-11-28 03:08:55 +01:00
assert result.startswith("temp_")
assert len(result) == 15