From e5769b3c25444268d01adabc8b372eddec0a2ac9 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 10 Nov 2023 18:04:19 +0100 Subject: [PATCH] Added Tests Co-authored-by: Tristan Nolde --- .../archive/sql_alchemy_to_networkx_v2.ipynb | 417 +++--------------- .../ui/pages/home.py | 36 -- .../utils/networkx/networkx_data.py | 46 +- tests/ui/home_page_test.py | 188 ++++++++ tests/utils/networkx/networkx_data_test.py | 184 ++++++-- 5 files changed, 404 insertions(+), 467 deletions(-) diff --git a/Jupyter/NetworkX/archive/sql_alchemy_to_networkx_v2.ipynb b/Jupyter/NetworkX/archive/sql_alchemy_to_networkx_v2.ipynb index 35d858d..6f957ab 100644 --- a/Jupyter/NetworkX/archive/sql_alchemy_to_networkx_v2.ipynb +++ b/Jupyter/NetworkX/archive/sql_alchemy_to_networkx_v2.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 5, "id": "b6eea59adeae27d4", "metadata": { "ExecuteTime": { @@ -36,7 +36,7 @@ "'c:\\\\Users\\\\trimr\\\\Projekte\\\\aki_prj23_transparenzregister'" ] }, - "execution_count": 37, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 6, "id": "eb9498d3", "metadata": {}, "outputs": [], @@ -71,7 +71,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 7, "id": "6a317af6", "metadata": {}, "outputs": [], @@ -92,7 +92,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 8, "id": "2d17651a", "metadata": {}, "outputs": [ @@ -102,7 +102,7 @@ "'SELECT to_company.id AS id_company_to, to_company.name AS name_company_to, relation.relation AS relation_type, from_company.name AS name_company_from, from_company.id AS id_company_from \\nFROM company AS to_company JOIN (relation JOIN company_relation ON relation.id = company_relation.id) ON relation.company_id = to_company.id JOIN company AS from_company ON company_relation.company2_id = from_company.id'" ] }, - "execution_count": 40, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -150,183 +150,48 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 12, "id": "444cd402", "metadata": {}, "outputs": [ { "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
id_company_toname_company_torelation_typename_company_fromid_company_from
052. Schaper Objekt GmbH & Co. Kiel KGKOMMANDITISTMulti-Center Warenvertriebs GmbH2213
132Alb-Windkraft GmbH & Co. KGKOMMANDITISTEnBW Windkraftprojekte GmbH845
234Anneliese Köster GmbH & Co. KGKOMMANDITISTINDUS Holding Aktiengesellschaft1903
374AURELIUS Equity Opportunities SE & Co. KGaAHAFTENDER_GESELLSCHAFTERAURELIUS Management SE163
477Aurelius KGHAFTENDER_GESELLSCHAFTERAurelius Verwaltungs GmbH80
..................
5733137Zalando BTD 011 SE & Co. KGHAFTENDER_GESELLSCHAFTERZalando SE3112
5743137Zalando BTD 011 SE & Co. KGKOMMANDITISTZalando Operations GmbH3103
5753138zLabels Creation & Sales GmbH & Co. KGHAFTENDER_GESELLSCHAFTERzLabels GmbH3113
5763145Zalando Customer Care International SE & Co. KGHAFTENDER_GESELLSCHAFTERZalando SE3112
5773145Zalando Customer Care International SE & Co. KGKOMMANDITISTZalando Operations GmbH3103
\n", - "

578 rows × 5 columns

\n", - "
" - ], "text/plain": [ - " id_company_to name_company_to \\\n", - "0 5 2. Schaper Objekt GmbH & Co. Kiel KG \n", - "1 32 Alb-Windkraft GmbH & Co. KG \n", - "2 34 Anneliese Köster GmbH & Co. KG \n", - "3 74 AURELIUS Equity Opportunities SE & Co. KGaA \n", - "4 77 Aurelius KG \n", - ".. ... ... \n", - "573 3137 Zalando BTD 011 SE & Co. KG \n", - "574 3137 Zalando BTD 011 SE & Co. KG \n", - "575 3138 zLabels Creation & Sales GmbH & Co. KG \n", - "576 3145 Zalando Customer Care International SE & Co. KG \n", - "577 3145 Zalando Customer Care International SE & Co. KG \n", - "\n", - " relation_type name_company_from \\\n", - "0 KOMMANDITIST Multi-Center Warenvertriebs GmbH \n", - "1 KOMMANDITIST EnBW Windkraftprojekte GmbH \n", - "2 KOMMANDITIST INDUS Holding Aktiengesellschaft \n", - "3 HAFTENDER_GESELLSCHAFTER AURELIUS Management SE \n", - "4 HAFTENDER_GESELLSCHAFTER Aurelius Verwaltungs GmbH \n", - ".. ... ... \n", - "573 HAFTENDER_GESELLSCHAFTER Zalando SE \n", - "574 KOMMANDITIST Zalando Operations GmbH \n", - "575 HAFTENDER_GESELLSCHAFTER zLabels GmbH \n", - "576 HAFTENDER_GESELLSCHAFTER Zalando SE \n", - "577 KOMMANDITIST Zalando Operations GmbH \n", - "\n", - " id_company_from \n", - "0 2213 \n", - "1 845 \n", - "2 1903 \n", - "3 163 \n", - "4 80 \n", - ".. ... \n", - "573 3112 \n", - "574 3103 \n", - "575 3113 \n", - "576 3112 \n", - "577 3103 \n", - "\n", - "[578 rows x 5 columns]" + "[{'id_company_to': 2,\n", + " 'name_company_to': '1. Staiger Grundstücksverwaltung GmbH & Co. KG',\n", + " 'relation_type': 'HAFTENDER_GESELLSCHAFTER',\n", + " 'name_company_from': 'Staiger I. Verwaltung-GmbH',\n", + " 'id_company_from': 3226},\n", + " {'id_company_to': 3,\n", + " 'name_company_to': '1 A Autenrieth Kunststofftechnik GmbH & Co. KG',\n", + " 'relation_type': 'HAFTENDER_GESELLSCHAFTER',\n", + " 'name_company_from': 'Autenrieth Verwaltungs-GmbH',\n", + " 'id_company_from': 3324},\n", + " {'id_company_to': 5,\n", + " 'name_company_to': '2. Schaper Objekt GmbH & Co. Kiel KG',\n", + " 'relation_type': 'KOMMANDITIST',\n", + " 'name_company_from': 'Multi-Center Warenvertriebs GmbH',\n", + " 'id_company_from': 2213},\n", + " {'id_company_to': 6,\n", + " 'name_company_to': 'AASP Filmproduktionsgesellschaft mbH & Co. Leonie KG',\n", + " 'relation_type': 'INHABER',\n", + " 'name_company_from': 'ABN AMRO Structured Products Gesellschaft für Fondsbeteiligungen mbH',\n", + " 'id_company_from': 3332},\n", + " {'id_company_to': 6,\n", + " 'name_company_to': 'AASP Filmproduktionsgesellschaft mbH & Co. Leonie KG',\n", + " 'relation_type': 'KOMMANDITIST',\n", + " 'name_company_from': 'Kallang GmbH',\n", + " 'id_company_from': 3316}]" ] }, - "execution_count": 42, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "company_relations = pd.read_sql_query(str(relations_company_query), session.bind)\n", - "company_relations" + "company_relations.head().to_dict(orient=\"records\")" ] }, { @@ -339,7 +204,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 14, "id": "52af1d30", "metadata": {}, "outputs": [], @@ -385,194 +250,58 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 15, "id": "c78b3e65", "metadata": {}, "outputs": [ { "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
id_companyname_companyrelation_typeid_personlastnamefirstnamedate_of_birth
010 10 24 Telefondienste GmbHGESCHAEFTSFUEHRER1TetauNicolas1971-01-02
110 10 24 Telefondienste GmbHPROKURIST2DammastLutz1966-12-06
221. Staiger Grundstücksverwaltung GmbH & Co. KGKOMMANDITIST3TutschRosemarie1941-10-09
321. Staiger Grundstücksverwaltung GmbH & Co. KGKOMMANDITIST4StaigerMarc1969-10-22
421. Staiger Grundstücksverwaltung GmbH & Co. KGKOMMANDITIST5StaigerMichaela1971-03-03
........................
148913144Wohnungsbaugesellschaft mit beschränkter Haftu...GESCHAEFTSFUEHRER878WeirichTorsten1975-07-21
148923144Wohnungsbaugesellschaft mit beschränkter Haftu...GESCHAEFTSFUEHRER1840BrusinskiBastian1980-10-29
148933145Zalando Customer Care International SE & Co. KGPROKURIST9359PapeUte1978-12-13
148943146zebotec GmbHGESCHAEFTSFUEHRER9628NeffWerner1981-11-24
148953146zebotec GmbHGESCHAEFTSFUEHRER9629MorrisRichard1971-01-02
\n", - "

14896 rows × 7 columns

\n", - "
" - ], "text/plain": [ - " id_company name_company \\\n", - "0 1 0 10 24 Telefondienste GmbH \n", - "1 1 0 10 24 Telefondienste GmbH \n", - "2 2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n", - "3 2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n", - "4 2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n", - "... ... ... \n", - "14891 3144 Wohnungsbaugesellschaft mit beschränkter Haftu... \n", - "14892 3144 Wohnungsbaugesellschaft mit beschränkter Haftu... \n", - "14893 3145 Zalando Customer Care International SE & Co. KG \n", - "14894 3146 zebotec GmbH \n", - "14895 3146 zebotec GmbH \n", - "\n", - " relation_type id_person lastname firstname date_of_birth \n", - "0 GESCHAEFTSFUEHRER 1 Tetau Nicolas 1971-01-02 \n", - "1 PROKURIST 2 Dammast Lutz 1966-12-06 \n", - "2 KOMMANDITIST 3 Tutsch Rosemarie 1941-10-09 \n", - "3 KOMMANDITIST 4 Staiger Marc 1969-10-22 \n", - "4 KOMMANDITIST 5 Staiger Michaela 1971-03-03 \n", - "... ... ... ... ... ... \n", - "14891 GESCHAEFTSFUEHRER 878 Weirich Torsten 1975-07-21 \n", - "14892 GESCHAEFTSFUEHRER 1840 Brusinski Bastian 1980-10-29 \n", - "14893 PROKURIST 9359 Pape Ute 1978-12-13 \n", - "14894 GESCHAEFTSFUEHRER 9628 Neff Werner 1981-11-24 \n", - "14895 GESCHAEFTSFUEHRER 9629 Morris Richard 1971-01-02 \n", - "\n", - "[14896 rows x 7 columns]" + "[{'id_company': 1,\n", + " 'name_company': '0 10 24 Telefondienste GmbH',\n", + " 'relation_type': 'GESCHAEFTSFUEHRER',\n", + " 'id_person': 1,\n", + " 'lastname': 'Tetau',\n", + " 'firstname': 'Nicolas',\n", + " 'date_of_birth': datetime.date(1971, 1, 2)},\n", + " {'id_company': 1,\n", + " 'name_company': '0 10 24 Telefondienste GmbH',\n", + " 'relation_type': 'PROKURIST',\n", + " 'id_person': 2,\n", + " 'lastname': 'Dammast',\n", + " 'firstname': 'Lutz',\n", + " 'date_of_birth': datetime.date(1966, 12, 6)},\n", + " {'id_company': 2,\n", + " 'name_company': '1. Staiger Grundstücksverwaltung GmbH & Co. KG',\n", + " 'relation_type': 'KOMMANDITIST',\n", + " 'id_person': 3,\n", + " 'lastname': 'Tutsch',\n", + " 'firstname': 'Rosemarie',\n", + " 'date_of_birth': datetime.date(1941, 10, 9)},\n", + " {'id_company': 2,\n", + " 'name_company': '1. Staiger Grundstücksverwaltung GmbH & Co. KG',\n", + " 'relation_type': 'KOMMANDITIST',\n", + " 'id_person': 4,\n", + " 'lastname': 'Staiger',\n", + " 'firstname': 'Marc',\n", + " 'date_of_birth': datetime.date(1969, 10, 22)},\n", + " {'id_company': 2,\n", + " 'name_company': '1. Staiger Grundstücksverwaltung GmbH & Co. KG',\n", + " 'relation_type': 'KOMMANDITIST',\n", + " 'id_person': 5,\n", + " 'lastname': 'Staiger',\n", + " 'firstname': 'Michaela',\n", + " 'date_of_birth': datetime.date(1971, 3, 3)}]" ] }, - "execution_count": 45, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person_relations = pd.read_sql_query(str(relations_person_query), session.bind)\n", - "person_relations" + "person_relations.head().to_dict(orient=\"records\")" ] }, { diff --git a/src/aki_prj23_transparenzregister/ui/pages/home.py b/src/aki_prj23_transparenzregister/ui/pages/home.py index 65f3aa1..2acfaea 100644 --- a/src/aki_prj23_transparenzregister/ui/pages/home.py +++ b/src/aki_prj23_transparenzregister/ui/pages/home.py @@ -9,7 +9,6 @@ import pandas as pd import plotly.graph_objects as go from cachetools import TTLCache, cached from dash import Input, Output, callback, dash_table, dcc, html -from loguru import logger from aki_prj23_transparenzregister.utils.networkx.network_2d import ( create_2d_graph, @@ -66,13 +65,11 @@ network = None def person_relation_type_filter() -> np.ndarray: """Returns an Numpy Array of String with Person telation types.""" - logger.debug("Updating Person Dropdown") return get_all_person_relations()["relation_type"].unique() def company_relation_type_filter() -> np.ndarray: """Returns an Numpy Array of String with Company relation types.""" - logger.debug("Updating Person Dropdown") return get_all_company_relations()["relation_type"].unique() @@ -88,17 +85,14 @@ def update_table( Returns: tuple[dict, list]: _description_ """ - logger.debug("Updateing Table") table_df = metrics.sort_values(metric_dropdown_value, ascending=False).head(10) table_df = table_df[["designation", "category", metric_dropdown_value]] - table_df.to_dict("records") columns = [{"name": i, "id": i} for i in table_df.columns] return table_df.to_dict("records"), columns # type: ignore def layout() -> list[html.Div]: """Generates the Layout of the Homepage.""" - logger.debug("Layouting Homepage") person_relation_types = person_relation_type_filter() company_relation_types = company_relation_type_filter() top_companies_dict, top_companies_columns, figure = update_figure( @@ -348,7 +342,6 @@ def update_graph_data( Returns: tuple[nx.Graph, pd.DataFrame, dict, list]: _description_ """ - logger.debug("Updating Graph data") # Get Data person_df = get_all_person_relations() company_df = get_all_company_relations() @@ -356,8 +349,6 @@ def update_graph_data( person_relation = filter_relation_type(person_df, person_relation_type) company_relation = filter_relation_type(company_df, company_relation_type) - # company_relation = filter_relation_with_more_than_one_connection(company_relation, "id_company_to", "id_company_from") - # Create Edge and Node List from data nodes_tmp, edges_tmp = create_edge_and_node_list(person_relation, company_relation) @@ -414,7 +405,6 @@ def update_figure( # noqa: PLR0913 Returns: Network Graph(Plotly Figure): Plotly Figure in 3 or 2D """ - logger.debug("Update Figure") _ = c_relation_filter_value, p_relation_filter_value graph, metrics, nodes, edges = update_graph_data( @@ -454,29 +444,3 @@ def update_figure( # noqa: PLR0913 slider_value, # type: ignore ), ) - - -@callback( - Output("company_dropdown", "style"), - [ - Input("dropdown_data_soruce_filter", "value"), - ], -) -def update_dropdown(datasource_value: str) -> str: - """_summary_. - - Args: - datasource_value (str): _description_ - - Returns: - str: _description_ - """ - style = "" - match datasource_value: - case "Company Data only": - style = "visibility: visible;" - case "Person Data only": - style = "visibility: hidden;" - case "Company & Person Data": - style = "visibility: visible;" - return style diff --git a/src/aki_prj23_transparenzregister/utils/networkx/networkx_data.py b/src/aki_prj23_transparenzregister/utils/networkx/networkx_data.py index 1071d1b..ab5621f 100644 --- a/src/aki_prj23_transparenzregister/utils/networkx/networkx_data.py +++ b/src/aki_prj23_transparenzregister/utils/networkx/networkx_data.py @@ -158,42 +158,6 @@ def filter_relation_type( ] -def filter_relation_with_more_than_one_connection( - relation_dataframe: pd.DataFrame, id_column_name_to: str, id_column_name_from: str -) -> pd.DataFrame: - """Method to filter all Entries in an DataFrame which has more than one connection. - - Args: - relation_dataframe (pd.DataFrame): _description_ - id_column_name_to (_type_): _description_ - id_column_name_from (_type_): _description_ - - Returns: - relation_dataframe (pd.DataFrame): The DataFrame which now only contains entries with more than one connection. - """ - tmp_df = pd.DataFrame(columns=relation_dataframe.columns.values) - for _index, row in relation_dataframe.iterrows(): - count = 0 - id = row[id_column_name_to] - for _index_sub, row_sub in relation_dataframe.iterrows(): - if id == row_sub[id_column_name_to]: - count = count + 1 - if id == row_sub[id_column_name_from]: - count = count + 1 - if count > 1: - break - - if count > 1: - # tmp_df = pd.concat([tmp_df, pd.DataFrame(row)])+ - tmp_df.loc[len(tmp_df)] = row # type: ignore - count = 0 - else: - count = 0 - continue - count = 0 - return tmp_df - - def create_edge_and_node_list( person_relations: pd.DataFrame, company_relations: pd.DataFrame ) -> tuple[dict, list]: @@ -362,7 +326,7 @@ def create_edge_and_node_list_for_company( return nodes, edges -def get_all_metrics_from_id(company_id: str) -> pd.Series: +def get_all_metrics_from_id(company_id: int) -> pd.Series: """Returns all Metric for the given ID. Args: @@ -380,11 +344,14 @@ def get_all_metrics_from_id(company_id: str) -> pd.Series: graph, metrics = initialize_network_with_reduced_metrics( nodes=nodes_tmp, edges=edges_tmp ) - return metrics.loc[metrics["id"] == company_id].iloc[0] + filtered_metrics = metrics.loc[metrics["id"] == company_id] + if len(filtered_metrics) == 0: + return pd.Series([]) + return filtered_metrics.iloc[0] @lru_cache -def get_relations_number_from_id(id: str) -> tuple[int, int, int]: +def get_relations_number_from_id(id: int) -> tuple[int, int, int]: """Returns all Relation in 1, 2 and 3 lvl of one Node. Args: @@ -399,7 +366,6 @@ def get_relations_number_from_id(id: str) -> tuple[int, int, int]: # Create Edge and Node List from data nodes_tmp, edges_tmp = create_edge_and_node_list(person_df, company_df) - graph = initialize_network_without_metrics(nodes=nodes_tmp, edges=edges_tmp) neighbors = nx.all_neighbors(graph, id) diff --git a/tests/ui/home_page_test.py b/tests/ui/home_page_test.py index 1bb3bb3..0aacb21 100644 --- a/tests/ui/home_page_test.py +++ b/tests/ui/home_page_test.py @@ -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 diff --git a/tests/utils/networkx/networkx_data_test.py b/tests/utils/networkx/networkx_data_test.py index ff928c4..8c52c6c 100644 --- a/tests/utils/networkx/networkx_data_test.py +++ b/tests/utils/networkx/networkx_data_test.py @@ -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)