Merge branch 'main' into feature/additional-stammdaten

This commit is contained in:
TrisNol
2023-09-23 10:32:09 +02:00
36 changed files with 3086 additions and 725 deletions

View File

@@ -1,3 +1,4 @@
"""Tests if the bundesanzeiger can be accessed and read."""
from unittest.mock import Mock, patch
import pandas as pd

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,40 @@
"""Tests for the enumeration types."""
import pytest
from aki_prj23_transparenzregister.utils import enum_types
def test_import() -> None:
"""Tests if enum_types can be imported."""
assert enum_types
@pytest.mark.parametrize("relation_name", ["Vorstand", "Prokurist", "Direktor"])
@pytest.mark.parametrize("changes", ["lower", "upper", None])
def test_relation_type_enum_from_string(
relation_name: str, changes: str | None
) -> None:
"""Tests the transformation of a name to an enumeration type."""
if changes == "lower":
relation_name = relation_name.lower()
elif changes == "upper":
relation_name = relation_name.upper()
assert isinstance(
enum_types.RelationTypeEnum.get_enum_from_name(relation_name),
enum_types.RelationTypeEnum,
)
@pytest.mark.parametrize("relation_name", ["does Not Exists", "Also not"])
@pytest.mark.parametrize("changes", ["lower", "upper", None])
def test_relation_type_enum_from_string_wrong(
relation_name: str, changes: str | None
) -> None:
"""Tests the transformation of a name to an enumeration type if no equivalent can be found."""
if changes == "lower":
relation_name = relation_name.lower()
elif changes == "upper":
relation_name = relation_name.upper()
with pytest.raises(ValueError, match='Relation type ".*" is not yet implemented!'):
enum_types.RelationTypeEnum.get_enum_from_name(relation_name)

View File

@@ -1,3 +1,4 @@
"""Tests for connecting to the mongodb."""
from unittest.mock import patch
from aki_prj23_transparenzregister.utils.mongo.connector import (
@@ -7,21 +8,25 @@ from aki_prj23_transparenzregister.utils.mongo.connector import (
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"
def test_mongo_connector() -> None:
"""Tests the MongoConnector."""
with patch("pymongo.MongoClient") as mock_mongo_client:
expected_result = 42
mock_mongo_client.return_value = {"db": expected_result}

View File

@@ -1,3 +1,4 @@
"""Tests for the mongo news service."""
from unittest.mock import Mock, patch
import pytest
@@ -50,6 +51,7 @@ def test_init(mock_mongo_connector: Mock, mock_collection: Mock) -> None:
def test_get_all(mock_mongo_connector: Mock, mock_collection: Mock) -> None:
"""Tests the get_all function from the mongo connector."""
mock_mongo_connector.database = {"news": mock_collection}
service = MongoNewsService(mock_mongo_connector)
@@ -60,6 +62,7 @@ def test_get_all(mock_mongo_connector: Mock, mock_collection: Mock) -> None:
def test_get_by_id_with_result(
mock_mongo_connector: Mock, mock_collection: Mock
) -> None:
"""Tests the get_by_id_with_result function from the mongo connector."""
mock_mongo_connector.database = {"news": mock_collection}
service = MongoNewsService(mock_mongo_connector)
@@ -72,6 +75,7 @@ def test_get_by_id_with_result(
def test_get_by_id_no_result(mock_mongo_connector: Mock, mock_collection: Mock) -> None:
"""Test if the mongo connector can get an object by id."""
mock_mongo_connector.database = {"news": mock_collection}
service = MongoNewsService(mock_mongo_connector)
@@ -80,6 +84,7 @@ def test_get_by_id_no_result(mock_mongo_connector: Mock, mock_collection: Mock)
def test_insert(mock_mongo_connector: Mock, mock_collection: Mock) -> None:
"""Tests the insert function from the mongo connector."""
mock_mongo_connector.database = {"news": mock_collection}
service = MongoNewsService(mock_mongo_connector)
@@ -92,6 +97,7 @@ def test_insert(mock_mongo_connector: Mock, mock_collection: Mock) -> None:
def test_transform_ingoing() -> None:
"""Tests the transform_ingoing function from the mongo connector."""
news = News("42", None, None, None, None) # type: ignore
result = MongoEntryTransformer.transform_ingoing(news)
assert result["_id"] == "42"
@@ -99,6 +105,7 @@ def test_transform_ingoing() -> None:
def test_transform_outgoing() -> None:
"""Tests the transform_outgoing function from the mongo connector."""
data = {
"_id": "4711",
"title": "Hello",

View File

@@ -1,21 +1,27 @@
"""Tests the sql connector."""
import os.path
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,
)
def test_get_engine_pg() -> None:
"""Tests the creation of a postgre engine."""
conn_args = PostgreConnectionString("", "", "", "", 42)
with patch(
"aki_prj23_transparenzregister.utils.sql.connector.sa.create_engine"
@@ -25,6 +31,36 @@ 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

@@ -1,4 +1,8 @@
def test_import() -> None:
from aki_prj23_transparenzregister.utils.sql import entities
"""Tests for the sql entities."""
from aki_prj23_transparenzregister.utils.sql import entities
def test_import() -> None: #
"""Tests if the entities can be imported."""
assert entities

View File

@@ -0,0 +1,35 @@
"""Tests for the string tool module."""
from typing import Any
import pytest
from aki_prj23_transparenzregister.utils import string_tools
def test_import() -> None:
"""Tests if the import is possible."""
assert string_tools
@pytest.mark.parametrize(
("value", "expected"),
[
("None ", "None"),
(" ", None),
("", None),
("\t", None),
("\n", None),
(" Some String ", "Some String"),
("Some String", "Some String"),
],
)
def test_simplify_string(value: str | None, expected: str | None) -> None:
"""Tests the sting simplification."""
assert string_tools.simplify_string(value) == expected
@pytest.mark.parametrize("value", [0, 0.1, True, ("1",), {}, set()])
def test_simplify_string_type_error(value: Any) -> None:
"""Tests if the type error is thrown when the value is the wrong type."""
with pytest.raises(TypeError):
assert string_tools.simplify_string(value)