"""Test for the Home Page.""" import datetime from collections.abc import Generator from typing import Any from unittest.mock import patch import dash import pandas as pd import pytest from sqlalchemy.orm import Session from aki_prj23_transparenzregister.ui.pages import home from aki_prj23_transparenzregister.ui.session_handler import SessionHandler def test_import() -> None: """Checks if an import co company_stats_dash can be made.""" assert home is not None 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"] 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"] 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 def test_update_graph_data() -> None: graph_result, metrics_result, nodes_result, edges_result = home.update_graph_data( frozenset({"HAFTENDER_GESELLSCHAFTER"}), frozenset("GESCHAEFTSFUEHRER") ) assert graph_result is not None @pytest.fixture() def _set_session(full_db: Session) -> Generator[None, None, None]: """Sets a session for the dash application to be used.""" SessionHandler.session = full_db yield SessionHandler.session = None @pytest.mark.usefixtures("_set_session") def test_layout() -> None: """Checks if layout can be executed.""" home.layout() @pytest.mark.usefixtures("_set_session") @pytest.mark.parametrize("empty", ["", None, {}]) def test_redirect_empty(empty: Any) -> None: """Tests the redirection on clicking on the plot with an empty argument.""" assert home.redirect(empty) == dash.no_update # type: ignore def test_redirect_content_without_db() -> None: """Tests the error when no SQL session is defined.""" with pytest.raises(ValueError, match="No SQL session defined."): assert home.redirect({"empty": ""}) @pytest.mark.usefixtures("_set_session") @pytest.mark.parametrize( ("click_on", "redirect_to"), [ ( {"text": "Some Company GmbH"}, "/unternehmensdetails/1", ), ({"text": "Max Mustermann"}, "/personendetails/1"), ({"text": "I do not exist"}, dash.no_update), ], ) def test_redirect(click_on: dict, redirect_to: Any) -> None: """Tests the redirection when clicking on a plot.""" assert home.redirect({"points": [click_on]}) == redirect_to