mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-08-11 19:18:28 +02:00
Add a cli interface to choose a configuration (#163)
- [x] add a cli to the webserver to take env variables into account - [x] add a cli to the data processing that takes enviromental variable as a valid source into account - [x] rework the cli for the reset sql command - [x] rework the cli for the copying of sql data from one db to another
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
"""Tests the config provers."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from unittest.mock import mock_open, patch
|
||||
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
from aki_prj23_transparenzregister.config.config_providers import (
|
||||
EnvironmentConfigProvider,
|
||||
JsonFileConfigProvider,
|
||||
get_config_provider,
|
||||
)
|
||||
from aki_prj23_transparenzregister.config.config_template import (
|
||||
PostgreConnectionString,
|
||||
SQLiteConnectionString,
|
||||
)
|
||||
|
||||
|
||||
@@ -19,7 +26,7 @@ def test_json_provider_init_fail() -> None:
|
||||
def test_json_provider_init_no_json() -> None:
|
||||
"""Tests if a non json file throws the correct error."""
|
||||
with patch("os.path.isfile") as mock_isfile, patch(
|
||||
"builtins.open", mock_open(read_data="fhdaofhdoas")
|
||||
"builtins.open", mock_open(read_data="fh1da23of4!hdo4s")
|
||||
):
|
||||
mock_isfile.return_value = True
|
||||
with pytest.raises(TypeError):
|
||||
@@ -30,20 +37,47 @@ def test_json_provider_init() -> None:
|
||||
"""Tests the JsonFileConfigProvider creation."""
|
||||
data = {"hello": "world"}
|
||||
input_data = json.dumps(data)
|
||||
with patch("os.path.isfile") as mock_isfile, patch(
|
||||
"builtins.open", mock_open(read_data=input_data)
|
||||
):
|
||||
mock_isfile.return_value = True
|
||||
provider = JsonFileConfigProvider("someWhere")
|
||||
assert provider.__data__ == data
|
||||
|
||||
|
||||
@pytest.mark.parametrize("as_path", [True, False])
|
||||
def test_get_config_provider_json(as_path: bool) -> None:
|
||||
"""Tests the JsonFileConfigProvider creation by the factory method."""
|
||||
data = {"hello": "world"}
|
||||
input_data = json.dumps(data)
|
||||
with patch("os.path.isfile") as mock_isfile:
|
||||
mock_isfile.return_value = True
|
||||
with patch("builtins.open", mock_open(read_data=input_data)):
|
||||
provider = JsonFileConfigProvider("someWhere")
|
||||
assert provider.__data__ == data
|
||||
config_provider = get_config_provider(
|
||||
Path("some_where.json") if as_path else "some_where.json"
|
||||
)
|
||||
assert isinstance(config_provider, JsonFileConfigProvider)
|
||||
assert config_provider.__data__ == data
|
||||
|
||||
|
||||
def test_json_provider_get_postgres() -> None:
|
||||
@pytest.mark.parametrize("args", ["ENV", "env", "", None, "PYTHON_", "ELSE_"])
|
||||
def test_get_config_provider_env(args: str | None) -> None:
|
||||
env_provider = get_config_provider(args)
|
||||
assert isinstance(env_provider, EnvironmentConfigProvider)
|
||||
|
||||
|
||||
def test_get_config_provider_fail() -> None:
|
||||
with pytest.raises(ValueError, match="No configuration found"):
|
||||
get_config_provider("something-else")
|
||||
|
||||
|
||||
def test_json_provider_get_sql() -> None:
|
||||
"""Tests if the config provider can return the postgre config string."""
|
||||
data = {
|
||||
"postgres": {
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"host": "locahost",
|
||||
"host": "localhost",
|
||||
"database": "postgres",
|
||||
"port": 420,
|
||||
}
|
||||
@@ -52,7 +86,8 @@ def test_json_provider_get_postgres() -> None:
|
||||
with patch("os.path.isfile") as mock_isfile:
|
||||
mock_isfile.return_value = True
|
||||
with patch("builtins.open", mock_open(read_data=input_data)):
|
||||
config = JsonFileConfigProvider("someWhere").get_postgre_connection_string()
|
||||
config = JsonFileConfigProvider("someWhere").get_sql_connection_string()
|
||||
assert isinstance(config, PostgreConnectionString)
|
||||
assert config.username == data["postgres"]["username"]
|
||||
assert config.password == data["postgres"]["password"]
|
||||
assert config.host == data["postgres"]["host"]
|
||||
@@ -67,14 +102,39 @@ def test_json_provider_get_postgres() -> None:
|
||||
assert "Mongo" not in str(JsonFileConfigProvider("someWhere"))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("additional_data", [True, False])
|
||||
def test_json_provider_get_sqlit3(additional_data: bool) -> None:
|
||||
"""Tests if the config provider can return the sqlite config string."""
|
||||
data = {
|
||||
"sqlite": "some.db",
|
||||
}
|
||||
if additional_data:
|
||||
data |= {
|
||||
"postgres": { # type: ignore
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"host": "localhost",
|
||||
"database": "postgres",
|
||||
"port": 420,
|
||||
}
|
||||
}
|
||||
input_data = json.dumps(data)
|
||||
with patch("os.path.isfile") as mock_isfile:
|
||||
mock_isfile.return_value = True
|
||||
with patch("builtins.open", mock_open(read_data=input_data)):
|
||||
assert JsonFileConfigProvider(
|
||||
"someWhere"
|
||||
).get_sql_connection_string() == SQLiteConnectionString("some.db")
|
||||
|
||||
|
||||
def test_json_provider_get_mongo() -> None:
|
||||
"""Tests the JsonConfigProvider for the mongo db."""
|
||||
data = {
|
||||
"mongo": {
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"host": "locahost",
|
||||
"database": "postgres",
|
||||
"host": "localhost",
|
||||
"database": "mongo",
|
||||
"port": 420,
|
||||
}
|
||||
}
|
||||
@@ -98,35 +158,59 @@ def test_json_provider_get_mongo() -> None:
|
||||
assert "Postgre" not in str(JsonFileConfigProvider("someWhere"))
|
||||
|
||||
|
||||
def test_env_provider_constructor() -> None:
|
||||
with patch("aki_prj23_transparenzregister.config.config_providers.os") as mock_os:
|
||||
keys = {"PYTHON_TEST": "test", "NOT_PYTHON_TEST": ""}
|
||||
mock_os.environ = keys
|
||||
provider = EnvironmentConfigProvider()
|
||||
assert provider.__data__ == {"TEST": "test"}
|
||||
|
||||
|
||||
def test_env_provider_postgres() -> None:
|
||||
def test_env_provider_constructor(monkeypatch: MonkeyPatch) -> None:
|
||||
env_configs = {"PYTHON_TEST": "test", "NOT_PYTHON_TEST": ""}
|
||||
for key, value in env_configs.items():
|
||||
monkeypatch.setenv(key, value)
|
||||
provider = EnvironmentConfigProvider()
|
||||
env_data = {
|
||||
"POSTGRES_USERNAME": "postgres",
|
||||
"POSTGRES_PASSWORD": "postgres",
|
||||
"POSTGRES_HOST": "localhost",
|
||||
"POSTGRES_DATABASE": "postgres",
|
||||
"POSTGRES_PORT": "5432",
|
||||
assert "TEST" in provider.__data__
|
||||
assert provider.__data__["TEST"] == "test"
|
||||
assert "NOT_PYTHON_TEST" not in provider.__data__
|
||||
|
||||
|
||||
def test_env_provider_postgres(monkeypatch: MonkeyPatch) -> None:
|
||||
env_configs = {
|
||||
"PYTHON_POSTGRES_USERNAME": "postgres-user",
|
||||
"PYTHON_POSTGRES_PASSWORD": "postgres-pw",
|
||||
"PYTHON_POSTGRES_HOST": "localhost",
|
||||
"PYTHON_POSTGRES_DATABASE": "postgres",
|
||||
"PYTHON_POSTGRES_PORT": "5432",
|
||||
}
|
||||
provider.__data__ = env_data
|
||||
conn_string = provider.get_postgre_connection_string()
|
||||
for env_config in env_configs.items():
|
||||
monkeypatch.setenv(*env_config)
|
||||
|
||||
assert conn_string.database == env_data["POSTGRES_DATABASE"]
|
||||
assert conn_string.host == env_data["POSTGRES_HOST"]
|
||||
assert conn_string.password == env_data["POSTGRES_PASSWORD"]
|
||||
assert conn_string.port == env_data["POSTGRES_PORT"]
|
||||
assert conn_string.username == env_data["POSTGRES_USERNAME"]
|
||||
|
||||
|
||||
def test_env_provider_mongodb() -> None:
|
||||
provider = EnvironmentConfigProvider()
|
||||
|
||||
conn_string = provider.get_sql_connection_string()
|
||||
assert isinstance(conn_string, PostgreConnectionString)
|
||||
|
||||
assert conn_string.database == env_configs["PYTHON_POSTGRES_DATABASE"]
|
||||
assert conn_string.host == env_configs["PYTHON_POSTGRES_HOST"]
|
||||
assert conn_string.password == env_configs["PYTHON_POSTGRES_PASSWORD"]
|
||||
assert conn_string.port == env_configs["PYTHON_POSTGRES_PORT"]
|
||||
assert conn_string.username == env_configs["PYTHON_POSTGRES_USERNAME"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("additional_args", [True, False])
|
||||
def test_env_provider_sqlite(additional_args: bool, monkeypatch: MonkeyPatch) -> None:
|
||||
env_configs = {"PYTHON_SQLITE_PATH": "some.db"}
|
||||
if additional_args:
|
||||
env_configs |= {
|
||||
"PYTHON_POSTGRES_USERNAME": "postgres-user",
|
||||
"PYTHON_POSTGRES_PASSWORD": "postgres-pw",
|
||||
"PYTHON_POSTGRES_HOST": "localhost",
|
||||
"PYTHON_POSTGRES_DATABASE": "postgres",
|
||||
"PYTHON_POSTGRES_PORT": "5432",
|
||||
}
|
||||
for env_config in env_configs.items():
|
||||
monkeypatch.setenv(*env_config)
|
||||
|
||||
provider = EnvironmentConfigProvider()
|
||||
assert provider.get_sql_connection_string() == SQLiteConnectionString("some.db")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("prefix", ["", "NON_PYTHON", "ELSE"])
|
||||
def test_env_provider_mongodb(prefix: str, monkeypatch: MonkeyPatch) -> None:
|
||||
env_data = {
|
||||
"MONGO_USERNAME": "username",
|
||||
"MONGO_HOST": "localhost",
|
||||
@@ -134,6 +218,13 @@ def test_env_provider_mongodb() -> None:
|
||||
"MONGO_PORT": 27017,
|
||||
"MONGO_DATABASE": "transparenzregister",
|
||||
}
|
||||
for key, value in env_data.items():
|
||||
monkeypatch.setenv(f"{prefix if prefix else 'PYTHON_'}{key}", str(value))
|
||||
if prefix:
|
||||
provider = EnvironmentConfigProvider(prefix)
|
||||
else:
|
||||
provider = EnvironmentConfigProvider()
|
||||
|
||||
provider.__data__ = env_data
|
||||
conn_string = provider.get_mongo_connection_string()
|
||||
|
||||
|
34
tests/config/config_template_test.py
Normal file
34
tests/config/config_template_test.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""Test for config templates."""
|
||||
from aki_prj23_transparenzregister.config.config_template import (
|
||||
MongoConnection,
|
||||
SQLiteConnectionString,
|
||||
)
|
||||
|
||||
|
||||
def test_sqlite_connection_string() -> None:
|
||||
"""Tests if the sqlite protocol is correctly added to a path."""
|
||||
assert str(SQLiteConnectionString("some-path-to.db")) == "sqlite:///some-path-to.db"
|
||||
assert (
|
||||
str(SQLiteConnectionString("other-path-to.db")) == "sqlite:///other-path-to.db"
|
||||
)
|
||||
assert SQLiteConnectionString("some-path-to.db") == SQLiteConnectionString(
|
||||
"some-path-to.db"
|
||||
)
|
||||
|
||||
|
||||
def test_get_conn_string_no_credentials() -> None:
|
||||
"""Tests the mongo connection string generation."""
|
||||
conn = MongoConnection("localhost", "", 27017, None, None)
|
||||
assert conn.get_conn_string() == "mongodb://localhost:27017"
|
||||
|
||||
|
||||
def test_get_conn_string_no_port_but_credentials() -> None:
|
||||
"""Tests the mongo connection string generation."""
|
||||
conn = MongoConnection("localhost", "", None, "admin", "password")
|
||||
assert conn.get_conn_string() == "mongodb+srv://admin:password@localhost"
|
||||
|
||||
|
||||
def test_get_conn_simple() -> None:
|
||||
"""Tests the mongo connection string generation."""
|
||||
conn = MongoConnection("localhost", "", None, None, None)
|
||||
assert conn.get_conn_string() == "mongodb+srv://localhost"
|
Reference in New Issue
Block a user