Added Tests

Co-authored-by: Tristan Nolde <TrisNol@users.noreply.github.com>
This commit is contained in:
Tim
2023-11-10 18:04:19 +01:00
parent 410b690873
commit e5769b3c25
5 changed files with 404 additions and 467 deletions

View File

@ -1,7 +1,195 @@
"""Test for the Home Page."""
import datetime
from collections.abc import Generator
from unittest.mock import patch
import pandas as pd
import pytest
from aki_prj23_transparenzregister.ui.pages import home
def test_import() -> None:
"""Checks if an import co company_stats_dash can be made."""
assert home is not None
@pytest.mark.tim()
def test_person_relation_type_filter() -> None:
with patch(
"aki_prj23_transparenzregister.ui.pages.home.get_all_person_relations"
) as mock_filter:
data = [
{"relation_type": "Eigentümer"},
{"relation_type": "Inhaber"},
{"relation_type": "Eigentümer"},
]
mock_filter.return_value = pd.DataFrame(data)
assert list(home.person_relation_type_filter()) == ["Eigentümer", "Inhaber"]
@pytest.mark.tim()
def test_company_relation_type_filter() -> None:
with patch(
"aki_prj23_transparenzregister.ui.pages.home.get_all_company_relations"
) as mock_filter:
data = [
{"relation_type": "Eigentümer"},
{"relation_type": "Inhaber"},
{"relation_type": "Eigentümer"},
]
mock_filter.return_value = pd.DataFrame(data)
assert list(home.company_relation_type_filter()) == ["Eigentümer", "Inhaber"]
@pytest.mark.tim()
def test_update_table() -> None:
metrics = pd.DataFrame(
[
{
"designation": "Mustermann, Max",
"category": "Person",
"centrality": 3.14,
"betweenness": 42,
},
{
"designation": "Musterfrau, Martina",
"category": "Person",
"centrality": 42,
"betweenness": 3.14,
},
]
)
selected_metric = "centrality"
expected_result_df = [
{
"designation": "Musterfrau, Martina",
"category": "Person",
"centrality": 42.0,
},
{
"designation": "Mustermann, Max",
"category": "Person",
"centrality": 3.14,
},
]
expected_result_columns = [
{"name": "designation", "id": "designation"},
{"name": "category", "id": "category"},
{"name": "centrality", "id": "centrality"},
]
result_df, result_columns = home.update_table(selected_metric, metrics)
assert result_df == expected_result_df
assert result_columns == expected_result_columns
@pytest.fixture(scope="session", autouse=True)
def _get_person_relations() -> Generator:
data = [
{
"id_company": 1,
"name_company": "0 10 24 Telefondienste GmbH",
"relation_type": "GESCHAEFTSFUEHRER",
"id_person": 1,
"lastname": "Tetau",
"firstname": "Nicolas",
"date_of_birth": datetime.date(1971, 1, 2),
},
{
"id_company": 1,
"name_company": "0 10 24 Telefondienste GmbH",
"relation_type": "PROKURIST",
"id_person": 2,
"lastname": "Dammast",
"firstname": "Lutz",
"date_of_birth": datetime.date(1966, 12, 6),
},
{
"id_company": 2,
"name_company": "1. Staiger Grundstücksverwaltung GmbH & Co. KG",
"relation_type": "KOMMANDITIST",
"id_person": 3,
"lastname": "Tutsch",
"firstname": "Rosemarie",
"date_of_birth": datetime.date(1941, 10, 9),
},
{
"id_company": 2,
"name_company": "1. Staiger Grundstücksverwaltung GmbH & Co. KG",
"relation_type": "HAFTENDER_GESELLSCHAFTER",
"id_person": 4,
"lastname": "Staiger",
"firstname": "Marc",
"date_of_birth": datetime.date(1969, 10, 22),
},
{
"id_company": 2,
"name_company": "1. Staiger Grundstücksverwaltung GmbH & Co. KG",
"relation_type": "HAFTENDER_GESELLSCHAFTER",
"id_person": 5,
"lastname": "Staiger",
"firstname": "Michaela",
"date_of_birth": datetime.date(1971, 3, 3),
},
]
with patch(
"aki_prj23_transparenzregister.ui.pages.home.get_all_person_relations"
) as mock_get_person_relations:
mock_get_person_relations.return_value = pd.DataFrame(data)
yield
@pytest.fixture(scope="session", autouse=True)
def _get_company_relations() -> Generator:
data = [
{
"id_company_to": 2,
"name_company_to": "1. Staiger Grundstücksverwaltung GmbH & Co. KG",
"relation_type": "GESCHAEFTSFUEHRER",
"name_company_from": "Staiger I. Verwaltung-GmbH",
"id_company_from": 3226,
},
{
"id_company_to": 3,
"name_company_to": "1 A Autenrieth Kunststofftechnik GmbH & Co. KG",
"relation_type": "GESCHAEFTSFUEHRER",
"name_company_from": "Autenrieth Verwaltungs-GmbH",
"id_company_from": 3324,
},
{
"id_company_to": 5,
"name_company_to": "2. Schaper Objekt GmbH & Co. Kiel KG",
"relation_type": "KOMMANDITIST",
"name_company_from": "Multi-Center Warenvertriebs GmbH",
"id_company_from": 2213,
},
{
"id_company_to": 6,
"name_company_to": "AASP Filmproduktionsgesellschaft mbH & Co. Leonie KG",
"relation_type": "INHABER",
"name_company_from": "ABN AMRO Structured Products Gesellschaft für Fondsbeteiligungen mbH",
"id_company_from": 3332,
},
{
"id_company_to": 6,
"name_company_to": "AASP Filmproduktionsgesellschaft mbH & Co. Leonie KG",
"relation_type": "KOMMANDITIST",
"name_company_from": "Kallang GmbH",
"id_company_from": 3316,
},
]
with patch(
"aki_prj23_transparenzregister.ui.pages.home.get_all_company_relations"
) as mock_get_person_relations:
mock_get_person_relations.return_value = pd.DataFrame(data)
yield
@pytest.mark.tim()
def test_update_graph_data() -> None:
graph_result, metrics_result, nodes_result, edges_result = home.update_graph_data(
"HAFTENDER_GESELLSCHAFTER", "GESCHAEFTSFUEHRER"
)
assert graph_result is not None

View File

@ -1,5 +1,7 @@
"""Test the initialize Network function."""
import datetime
from collections.abc import Generator
from unittest.mock import patch
import pandas as pd
import pytest
@ -7,16 +9,108 @@ from sqlalchemy.orm import Session
from aki_prj23_transparenzregister.ui.session_handler import SessionHandler
from aki_prj23_transparenzregister.utils.networkx import networkx_data
from aki_prj23_transparenzregister.utils.networkx.networkx_data import (
create_edge_and_node_list,
create_edge_and_node_list_for_company,
filter_relation_type,
filter_relation_with_more_than_one_connection,
find_all_company_relations,
find_company_relations,
get_all_company_relations,
get_all_person_relations,
)
@pytest.fixture(scope="session", autouse=True)
def _get_person_relations() -> Generator:
data = [
{
"id_company": 1,
"name_company": "0 10 24 Telefondienste GmbH",
"relation_type": "GESCHAEFTSFUEHRER",
"id_person": 1,
"lastname": "Tetau",
"firstname": "Nicolas",
"date_of_birth": datetime.date(1971, 1, 2),
},
{
"id_company": 1,
"name_company": "0 10 24 Telefondienste GmbH",
"relation_type": "PROKURIST",
"id_person": 2,
"lastname": "Dammast",
"firstname": "Lutz",
"date_of_birth": datetime.date(1966, 12, 6),
},
{
"id_company": 2,
"name_company": "1. Staiger Grundstücksverwaltung GmbH & Co. KG",
"relation_type": "KOMMANDITIST",
"id_person": 3,
"lastname": "Tutsch",
"firstname": "Rosemarie",
"date_of_birth": datetime.date(1941, 10, 9),
},
{
"id_company": 2,
"name_company": "1. Staiger Grundstücksverwaltung GmbH & Co. KG",
"relation_type": "KOMMANDITIST",
"id_person": 4,
"lastname": "Staiger",
"firstname": "Marc",
"date_of_birth": datetime.date(1969, 10, 22),
},
{
"id_company": 2,
"name_company": "1. Staiger Grundstücksverwaltung GmbH & Co. KG",
"relation_type": "KOMMANDITIST",
"id_person": 5,
"lastname": "Staiger",
"firstname": "Michaela",
"date_of_birth": datetime.date(1971, 3, 3),
},
]
with patch(
"aki_prj23_transparenzregister.utils.networkx.networkx_data.get_all_person_relations"
) as mock_get_person_relations:
mock_get_person_relations.return_value = pd.DataFrame(data)
yield
@pytest.fixture(scope="session", autouse=True)
def _get_company_relations() -> Generator:
data = [
{
"id_company_to": 2,
"name_company_to": "1. Staiger Grundstücksverwaltung GmbH & Co. KG",
"relation_type": "HAFTENDER_GESELLSCHAFTER",
"name_company_from": "Staiger I. Verwaltung-GmbH",
"id_company_from": 3226,
},
{
"id_company_to": 3,
"name_company_to": "1 A Autenrieth Kunststofftechnik GmbH & Co. KG",
"relation_type": "HAFTENDER_GESELLSCHAFTER",
"name_company_from": "Autenrieth Verwaltungs-GmbH",
"id_company_from": 3324,
},
{
"id_company_to": 5,
"name_company_to": "2. Schaper Objekt GmbH & Co. Kiel KG",
"relation_type": "KOMMANDITIST",
"name_company_from": "Multi-Center Warenvertriebs GmbH",
"id_company_from": 2213,
},
{
"id_company_to": 6,
"name_company_to": "AASP Filmproduktionsgesellschaft mbH & Co. Leonie KG",
"relation_type": "INHABER",
"name_company_from": "ABN AMRO Structured Products Gesellschaft für Fondsbeteiligungen mbH",
"id_company_from": 3332,
},
{
"id_company_to": 6,
"name_company_to": "AASP Filmproduktionsgesellschaft mbH & Co. Leonie KG",
"relation_type": "KOMMANDITIST",
"name_company_from": "Kallang GmbH",
"id_company_from": 3316,
},
]
with patch(
"aki_prj23_transparenzregister.utils.networkx.networkx_data.get_all_company_relations"
) as mock_get_person_relations:
mock_get_person_relations.return_value = pd.DataFrame(data)
yield
@pytest.fixture(autouse=True)
@ -70,48 +164,37 @@ def test_import() -> None:
def test_find_all_company_relations() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
company_relations_df = find_all_company_relations()
company_relations_df = networkx_data.find_all_company_relations()
assert type(company_relations_df) is pd.DataFrame
def test_get_all_company_relations() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
company_relations_df = get_all_company_relations()
company_relations_df = networkx_data.get_all_company_relations()
assert type(company_relations_df) is pd.DataFrame
def test_get_all_person_relations() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
company_relations_df = get_all_person_relations()
company_relations_df = networkx_data.get_all_person_relations()
assert type(company_relations_df) is pd.DataFrame
def test_filter_relation_type() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
relation_dataframe = get_all_company_relations()
relation_dataframe = networkx_data.get_all_company_relations()
selected_relation_type = "HAFTENDER_GESELLSCHAFTER"
company_relations_df = filter_relation_type(
company_relations_df = networkx_data.filter_relation_type(
relation_dataframe, selected_relation_type
)
assert type(company_relations_df) is pd.DataFrame
def test_filter_relation_with_more_than_one_connection() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
relation_dataframe = get_all_company_relations()
id_column_name_to = "c_1"
id_column_name_from = "c_2"
relations_df = filter_relation_with_more_than_one_connection(
relation_dataframe, id_column_name_to, id_column_name_from
)
assert type(relations_df) is pd.DataFrame
def test_create_edge_and_node_list() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
person_df = get_all_person_relations()
company_df = get_all_company_relations()
nodes, edges = create_edge_and_node_list(person_df, company_df)
person_df = networkx_data.get_all_person_relations()
company_df = networkx_data.get_all_company_relations()
nodes, edges = networkx_data.create_edge_and_node_list(person_df, company_df)
assert isinstance(nodes, dict)
assert isinstance(edges, list)
@ -119,32 +202,39 @@ def test_create_edge_and_node_list() -> None:
def test_find_company_relations() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
selected_company_id = 1
company_relations_df, person_df = find_company_relations(selected_company_id)
company_relations_df, person_df = networkx_data.find_company_relations(
selected_company_id
)
assert type(company_relations_df) is pd.DataFrame
assert type(person_df) is pd.DataFrame
def test_create_edge_and_node_list_for_company() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
company_relations = get_all_company_relations()
nodes, edges = create_edge_and_node_list_for_company(company_relations)
company_relations = networkx_data.get_all_company_relations()
nodes, edges = networkx_data.create_edge_and_node_list_for_company(
company_relations
)
assert isinstance(nodes, dict)
assert isinstance(edges, list)
# @pytest.mark.tim()
# def test_get_all_metrics_from_id() -> None:
# """This Test methods tests if the correct type is returned for the corresponding Function."""
# company_id = 2549
# metrics = get_all_metrics_from_id(company_id)
# assert type(metrics) is pd.Series
def test_get_all_metrics_from_id() -> None:
"""This Test methods tests if the correct type is returned for the corresponding Function."""
company_id = 2
metrics = networkx_data.get_all_metrics_from_id(company_id)
assert type(metrics) is pd.Series
# @pytest.mark.tim()
# def test_get_relations_number_from_id() -> None:
# """This Test methods tests if the correct type and number of relations is received."""
# # id = "c_2549"
# id = "c_2667"
# relations_lvl_1, relations_lvl_2, relations_lvl_3 = get_relations_number_from_id(id)
# assert type(relations_lvl_1) is int
# assert type(relations_lvl_2) is int
# assert type(relations_lvl_3) is int
def test_get_relations_number_from_id() -> None:
"""This Test methods tests if the correct type and number of relations is received."""
# id = "c_2549"
id = 2
(
relations_lvl_1,
relations_lvl_2,
relations_lvl_3,
) = networkx_data.get_relations_number_from_id(id)
assert isinstance(relations_lvl_1, int)
assert isinstance(relations_lvl_2, int)
assert isinstance(relations_lvl_3, int)