Repaired the SQL copy and reduced the log volume a bit (#141)

- Added a cli interface to the SQL copy
- Repaired the SQL copy function
- Added the SQL copy function to the scripts
- Reduced the logging verbosity
This commit is contained in:
2023-09-24 15:11:49 +02:00
committed by GitHub
parent d9ca808efa
commit 820fb3e52b
12 changed files with 239 additions and 97 deletions

View File

@@ -571,23 +571,25 @@ def test_add_relationship_company_unknown(
city: str | None,
zip_code: str | None,
full_db: Session,
mocker: MockerFixture,
) -> None:
"""Tests if a relationship to another company can be added."""
with pytest.raises(
KeyError, match=f"No corresponding company could be found to {company_name}."
):
data_transfer.add_relationship(
{
"description": company_name,
"location": {
"zip_code": zip_code,
"city": city,
},
"role": "organisation",
spy_warning = mocker.spy(data_transfer.logger, "warning")
spy_info = mocker.spy(data_transfer.logger, "info")
data_transfer.add_relationship(
{
"description": company_name,
"location": {
"zip_code": zip_code,
"city": city,
},
company_id,
full_db,
)
"role": "organisation",
},
company_id,
full_db,
)
spy_warning.assert_called_once()
spy_info.assert_not_called()
@pytest.mark.parametrize("empty_relations", [[], [{}], [{"relationship": []}]])
@@ -778,7 +780,7 @@ def test_add_annual_financial_reports_no_call(
) -> None:
"""Testing if financial reports are added correctly to the db."""
spy_warning = mocker.spy(data_transfer.logger, "warning")
info_warning = mocker.spy(data_transfer.logger, "info")
spy_info = mocker.spy(data_transfer.logger, "info")
mocker.patch("aki_prj23_transparenzregister.utils.data_transfer.add_annual_report")
data_transfer.add_annual_financial_reports(companies, full_db)
@@ -786,7 +788,7 @@ def test_add_annual_financial_reports_no_call(
input_kwargs = mocker.call.kwargs
assert len(input_args) == len(input_kwargs)
spy_warning.assert_not_called()
info_warning.assert_called_once()
spy_info.assert_called_once()
@pytest.mark.parametrize(
@@ -821,7 +823,7 @@ def test_add_annual_financial_reports_defect_year(
) -> None:
"""Testing if financial reports are added correctly to the db."""
spy_warning = mocker.spy(data_transfer.logger, "warning")
info_warning = mocker.spy(data_transfer.logger, "info")
spy_info = mocker.spy(data_transfer.logger, "info")
mocker.patch("aki_prj23_transparenzregister.utils.data_transfer.add_annual_report")
data_transfer.add_annual_financial_reports(companies, full_db)
@@ -829,7 +831,7 @@ def test_add_annual_financial_reports_defect_year(
input_kwargs = mocker.call.kwargs
assert len(input_args) == len(input_kwargs)
spy_warning.assert_called_once()
info_warning.assert_called_once()
spy_info.assert_called_once()
def test_add_annual_financial_reports(full_db: Session, mocker: MockerFixture) -> None:
@@ -864,7 +866,7 @@ def test_add_annual_financial_reports(full_db: Session, mocker: MockerFixture) -
]
spy_warning = mocker.spy(data_transfer.logger, "warning")
info_warning = mocker.spy(data_transfer.logger, "info")
spy_info = mocker.spy(data_transfer.logger, "info")
mocked = mocker.patch(
"aki_prj23_transparenzregister.utils.data_transfer.add_annual_report"
)
@@ -890,7 +892,7 @@ def test_add_annual_financial_reports(full_db: Session, mocker: MockerFixture) -
for input_args in mocked.call_args_list:
assert isinstance(input_args.kwargs["db"], Session)
info_warning.assert_called_once()
spy_info.assert_called_once()
@pytest.mark.parametrize("year", list(range(2000, 2025, 5)))

View File

@@ -0,0 +1,26 @@
"""Smoke-test over the logger config."""
from pathlib import Path
import pytest
from aki_prj23_transparenzregister.utils.logger_config import configer_logger
@pytest.mark.parametrize("path", [None, "test-log.log", ""])
@pytest.mark.parametrize("upper", [True, False])
@pytest.mark.parametrize("level", ["info", "debug", "error", "warning"])
def test_configer_logger(
level: str,
upper: bool,
path: Path | str | None,
) -> None:
"""Tests the configuration of the logger.
Args:
level: The log-level to configure.
upper: If the upper variant of the level should be used.
path: The path where to save the log.
"""
if level.upper():
level = level.upper()
configer_logger(level, path) # type: ignore

View File

@@ -4,19 +4,15 @@ from collections.abc import Generator
from typing import Any
from unittest.mock import Mock, patch
import pandas as pd
import pytest
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session
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 (
Base,
get_pg_engine,
get_session,
init_db,
transfer_db,
)
@@ -31,36 +27,6 @@ def test_get_engine_pg() -> None:
assert get_pg_engine(conn_args) == result
@pytest.fixture()
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")
init_db(db)
yield db
db.close()
bind = db.bind
assert isinstance(bind, Engine)
bind.dispose()
os.remove("secondary.db")
def test_transfer_db(full_db: Session, destination_db: Session) -> None:
"""Tests if the data transfer between two sql tables works."""
transfer_db(source=full_db, destination=destination_db)
sbind = full_db.bind
dbind = destination_db.bind
assert isinstance(sbind, Engine)
assert isinstance(dbind, Engine)
for table in Base.metadata.sorted_tables:
pd.testing.assert_frame_equal(
pd.read_sql_table(str(table), dbind),
pd.read_sql_table(str(table), sbind),
)
@pytest.fixture()
def delete_sqlite_table() -> Generator[str, None, None]:
"""Cleans a path before and deletes the table after a test.

View File

@@ -0,0 +1,56 @@
"""Test if the sql db can be copied."""
import os
from collections.abc import Generator
import pandas as pd
import pytest
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session
from aki_prj23_transparenzregister.utils.sql.connector import Base, get_session, init_db
from aki_prj23_transparenzregister.utils.sql.copy_sql import (
copy_db_cli,
transfer_db_function,
)
@pytest.fixture()
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")
init_db(db)
yield db
db.close()
bind = db.bind
assert isinstance(bind, Engine)
bind.dispose()
os.remove("secondary.db")
def test_transfer_db(full_db: Session, destination_db: Session) -> None:
"""Tests if the data transfer between two sql tables works."""
transfer_db_function(source=full_db, destination=destination_db)
sbind = full_db.bind
dbind = destination_db.bind
assert isinstance(sbind, Engine)
assert isinstance(dbind, Engine)
assert Base.metadata.sorted_tables
for table in Base.metadata.sorted_tables + ["company"]:
pd.testing.assert_frame_equal(
pd.read_sql_table(str(table), dbind),
pd.read_sql_table(str(table), sbind),
)
def test_copy_db_cli_help1() -> None:
"""Tests if the help argument exits the software gracefully."""
with pytest.raises(SystemExit):
copy_db_cli(["-h"])
def test_copy_db_cli_help2() -> None:
"""Tests if the help argument exits the software gracefully."""
with pytest.raises(SystemExit):
copy_db_cli(["eskse", "-h", "asdf"])