mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-06-22 00:14:01 +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,16 +1,14 @@
|
||||
"""Tests the sql connector."""
|
||||
import os.path
|
||||
from collections.abc import Generator
|
||||
from typing import Any
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
||||
from aki_prj23_transparenzregister.config.config_template import PostgreConnectionString
|
||||
from aki_prj23_transparenzregister.utils.sql.connector import (
|
||||
get_pg_engine,
|
||||
get_engine,
|
||||
get_session,
|
||||
init_db,
|
||||
)
|
||||
@ -24,7 +22,13 @@ def test_get_engine_pg() -> None:
|
||||
) as mock_create_engine:
|
||||
result = "someThing"
|
||||
mock_create_engine.return_value = result
|
||||
assert get_pg_engine(conn_args) == result
|
||||
assert get_engine(conn_args) == result
|
||||
|
||||
|
||||
def test_get_engine_fail() -> None:
|
||||
"""Tests what happens if the wrong type is given to the engine factory."""
|
||||
with pytest.raises(TypeError, match="The type of the configuration is invalid."):
|
||||
get_engine(None) # type: ignore
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@ -44,29 +48,10 @@ def delete_sqlite_table() -> Generator[str, None, None]:
|
||||
os.remove(sqlite_test_path)
|
||||
|
||||
|
||||
def test_get_sqlite_init(delete_sqlite_table: str) -> None:
|
||||
"""Tests if a sql table file can be initiated."""
|
||||
assert not os.path.exists(delete_sqlite_table)
|
||||
session = get_session(f"sqlite:///{delete_sqlite_table}")
|
||||
init_db(session)
|
||||
session.close()
|
||||
engine = session.bind
|
||||
assert isinstance(engine, Engine)
|
||||
engine.dispose()
|
||||
assert os.path.exists(delete_sqlite_table)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("connection", ["faulty-name", 0, 9.2, True])
|
||||
def test_get_invalid_connection(connection: Any) -> None:
|
||||
"""Tests if an error is thrown on a faulty connections."""
|
||||
with pytest.raises(TypeError):
|
||||
get_session(connection)
|
||||
|
||||
|
||||
def test_init_pd_db() -> None:
|
||||
"""Tests if a pg sql database can be connected and initiated to."""
|
||||
with patch(
|
||||
"aki_prj23_transparenzregister.utils.sql.connector.get_pg_engine"
|
||||
"aki_prj23_transparenzregister.utils.sql.connector.get_engine"
|
||||
) as mock_get_engine, patch(
|
||||
"aki_prj23_transparenzregister.utils.sql.connector.declarative_base"
|
||||
) as mock_declarative_base:
|
||||
@ -77,6 +62,6 @@ def test_init_pd_db() -> None:
|
||||
mock_declarative_base.return_value = mock_value
|
||||
|
||||
mock_value = Mock(spec=JsonFileConfigProvider)
|
||||
mock_value.get_postgre_connection_string.return_value = ""
|
||||
mock_value.get_sql_connection_string.return_value = ""
|
||||
|
||||
init_db(get_session(mock_value))
|
||||
|
@ -1,13 +1,20 @@
|
||||
"""Test if the sql db can be copied."""
|
||||
import os
|
||||
import sys
|
||||
from collections.abc import Generator
|
||||
|
||||
import pandas as pd
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from aki_prj23_transparenzregister.utils.sql.connector import Base, get_session, init_db
|
||||
from aki_prj23_transparenzregister.config.config_template import SQLiteConnectionString
|
||||
from aki_prj23_transparenzregister.utils.sql.connector import (
|
||||
Base,
|
||||
get_engine,
|
||||
init_db,
|
||||
)
|
||||
from aki_prj23_transparenzregister.utils.sql.copy_sql import (
|
||||
copy_db_cli,
|
||||
transfer_db_function,
|
||||
@ -19,7 +26,13 @@ def destination_db() -> Generator[Session, None, None]:
|
||||
"""Generates a db Session to a sqlite db to copy data to."""
|
||||
if os.path.exists("secondary.db"):
|
||||
os.remove("secondary.db")
|
||||
db = get_session("sqlite:///secondary.db")
|
||||
|
||||
db = sessionmaker(
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
bind=get_engine(SQLiteConnectionString("secondary.db")),
|
||||
)()
|
||||
|
||||
init_db(db)
|
||||
yield db
|
||||
db.close()
|
||||
@ -44,13 +57,15 @@ def test_transfer_db(full_db: Session, destination_db: Session) -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_copy_db_cli_help1() -> None:
|
||||
def test_copy_db_cli_help1(monkeypatch: MonkeyPatch) -> None:
|
||||
"""Tests if the help argument exits the software gracefully."""
|
||||
with pytest.raises(SystemExit):
|
||||
copy_db_cli(["-h"])
|
||||
with monkeypatch.context() as m, pytest.raises(SystemExit): # noqa: PT012
|
||||
m.setattr(sys, "argv", [sys.argv[0], "-h"])
|
||||
copy_db_cli()
|
||||
|
||||
|
||||
def test_copy_db_cli_help2() -> None:
|
||||
def test_copy_db_cli_help2(monkeypatch: MonkeyPatch) -> None:
|
||||
"""Tests if the help argument exits the software gracefully."""
|
||||
with pytest.raises(SystemExit):
|
||||
copy_db_cli(["eskse", "-h", "asdf"])
|
||||
with monkeypatch.context() as m, pytest.raises(SystemExit): # noqa: PT012
|
||||
m.setattr(sys, "argv", [sys.argv[0], "eskse", "-h", "asdf"])
|
||||
copy_db_cli()
|
||||
|
Reference in New Issue
Block a user