Added test

This commit is contained in:
Tim
2023-11-07 21:18:58 +01:00
parent 41af7e2d18
commit 410b690873
18 changed files with 619 additions and 384 deletions

View File

@ -45,11 +45,11 @@ def create_2d_graph( # noqa PLR0913
# pos = nx.planar_layout(graph) # pos = nx.planar_layout(graph)
case "Random": case "Random":
pos = nx.random_layout(graph) pos = nx.random_layout(graph)
case "Shell only 2D)": case "(Shell only 2D)":
pos = nx.shell_layout(graph) pos = nx.shell_layout(graph)
# case "Spectral": # case "Spectral":
# pos = nx.spectral_layout(graph) # pos = nx.spectral_layout(graph)
case "Spiral only 2D)": case "(Spiral only 2D)":
pos = nx.spiral_layout(graph) pos = nx.spiral_layout(graph)
# case "Multipartite": # case "Multipartite":
# pos = nx.multipartite_layout(graph) # pos = nx.multipartite_layout(graph)

View File

@ -22,20 +22,28 @@ def initialize_network(edges: list, nodes: dict) -> tuple[nx.Graph, pd.DataFrame
# update node attributes from dataframe # update node attributes from dataframe
nx.set_node_attributes(graph, nodes) nx.set_node_attributes(graph, nodes)
# Create a DataFrame with all Metrics
# Create a DataFrame with all Metrics # Create a DataFrame with all Metrics
metrics = pd.DataFrame( metrics = pd.DataFrame(
{ columns=[
"eigenvector": nx.eigenvector_centrality(graph).values(), "eigenvector",
"degree": nx.degree_centrality(graph).values(), "degree",
"betweenness": nx.betweenness_centrality(graph).values(), "betweenness",
"closeness": nx.closeness_centrality(graph).values(), "closeness",
"pagerank": nx.pagerank(graph).values(), "pagerank",
"category": nx.get_node_attributes(graph, "type").values(), "category",
"designation": nx.get_node_attributes(graph, "name").values(), "designation",
"id": nx.get_node_attributes(graph, "id").values(), "id",
} ]
).T )
metrics["eigenvector"] = nx.eigenvector_centrality(graph).values()
metrics["degree"] = nx.degree_centrality(graph).values()
metrics["betweenness"] = nx.betweenness_centrality(graph).values()
metrics["closeness"] = nx.closeness_centrality(graph).values()
metrics["pagerank"] = nx.pagerank(graph).values()
metrics["category"] = nx.get_node_attributes(graph, "type").values()
metrics["designation"] = nx.get_node_attributes(graph, "name").values()
metrics["id"] = nx.get_node_attributes(graph, "id").values()
return graph, metrics return graph, metrics
@ -64,7 +72,7 @@ def initialize_network_with_reduced_metrics(
# Create a DataFrame with all Metrics # Create a DataFrame with all Metrics
metrics = pd.DataFrame( metrics = pd.DataFrame(
columns=["degree", "eigenvector", "betweenness", "closeness", "pagerank"] columns=["degree", "betweenness", "closeness", "category", "designation", "id"]
) )
# metrics["eigenvector"] = nx.eigenvector_centrality(graph).values() # metrics["eigenvector"] = nx.eigenvector_centrality(graph).values()
metrics["degree"] = nx.degree_centrality(graph).values() metrics["degree"] = nx.degree_centrality(graph).values()

View File

@ -63,24 +63,6 @@ def find_all_company_relations() -> pd.DataFrame:
return companies_relations_df return companies_relations_df
def find_top_companies() -> pd.DataFrame:
"""_summary_.
Returns:
pd.DataFrame: _description_
"""
session = SessionHandler.session
assert session # noqa: S101
query_companies = session.query(entities.Company) # .all()
companies_df: pd.DataFrame = pd.read_sql(str(query_companies), session.bind) # type: ignore
companies_df = companies_df.head()
companies_df = companies_df[["company_name"]]
companies_df["Platzierung"] = [1, 2, 3, 4, 5]
companies_df["Umsatz M€"] = [1, 2, 3, 4, 5]
return companies_df[["Platzierung", "company_name", "Umsatz M€"]]
def get_all_company_relations() -> pd.DataFrame: def get_all_company_relations() -> pd.DataFrame:
"""This Methods makes a Database Request for all Companies and their relations, modifies the ID Column and returns the Result as an DataFrame. """This Methods makes a Database Request for all Companies and their relations, modifies the ID Column and returns the Result as an DataFrame.
@ -380,7 +362,7 @@ def create_edge_and_node_list_for_company(
return nodes, edges return nodes, edges
def get_all_metrics_from_id(company_id: int) -> pd.Series: def get_all_metrics_from_id(company_id: str) -> pd.Series:
"""Returns all Metric for the given ID. """Returns all Metric for the given ID.
Args: Args:

View File

@ -1,7 +0,0 @@
"""Test for the NetworkX Component."""
from aki_prj23_transparenzregister.ui.archive import networkx_dash
def network_graph(edges: None) -> None:
"""Checks if an import co company_stats_dash can be made."""
assert networkx_dash is not None

View File

@ -2,15 +2,21 @@
import datetime import datetime
from collections.abc import Generator from collections.abc import Generator
import networkx as nx import plotly.graph_objects as go
import pandas as pd
import pytest import pytest
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from aki_prj23_transparenzregister.ui.session_handler import SessionHandler from aki_prj23_transparenzregister.ui.session_handler import SessionHandler
from aki_prj23_transparenzregister.utils.networkx import network_2d
from aki_prj23_transparenzregister.utils.networkx.network_2d import create_2d_graph
from aki_prj23_transparenzregister.utils.networkx.network_base import initialize_network from aki_prj23_transparenzregister.utils.networkx.network_base import initialize_network
def test_import() -> None:
"""Checks if an import co company_stats_dash can be made."""
assert network_2d is not None
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def _set_session(full_db: Session) -> Generator[None, None, None]: def _set_session(full_db: Session) -> Generator[None, None, None]:
"""Sets a session for the dash application to be used.""" """Sets a session for the dash application to be used."""
@ -19,32 +25,79 @@ def _set_session(full_db: Session) -> Generator[None, None, None]:
SessionHandler.session = None SessionHandler.session = None
def test_initialize_network() -> None: def test_create_2d_graph() -> None:
"""Tests the creation of a 2D Graph."""
edges: list = [ edges: list = [
{"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"}, {"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_53", "to": "p_545", "type": "HAFTENDER_GESELLSCHAFTER"}, {"from": "c_53", "to": "p_545", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_1", "to": "c_2", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_53", "to": "c_1", "type": "HAFTENDER_GESELLSCHAFTER"},
] ]
nodes: dict = { nodes: dict = {
"c_53": { "c_53": {
"id": "c_53", "id": "c_53",
"name": "1. Freiburger Solarfonds Beteiligungs-KG", "name": "1. Freiburger Solarfonds Beteiligungs-KG",
"type": "company",
"color": "blue", "color": "blue",
}, },
"p_545": { "p_545": {
"id": "p_545", "id": "p_545",
"firstname": "Jürgen", "name": "Wetzel, Jürgen",
"lastname": "Wetzel", "type": "person",
"date_of_birth": datetime.date(1962, 11, 15), "date_of_birth": datetime.date(1962, 11, 15),
"color": "red", "color": "red",
}, },
"c_1": {
"id": "c_1",
"name": "Musterfirma",
"type": "company",
"color": "blue",
},
"c_2": {
"id": "c_2",
"name": "Firma 1",
"type": "company",
"color": "blue",
},
} }
graph, metrics = initialize_network(edges=edges, nodes=nodes) graph, metrics = initialize_network(edges=edges, nodes=nodes)
assert type(graph) is nx.Graph metric = "None"
assert type(metrics) is pd.DataFrame layout = "Spring"
# assert list(metrics.columns) == [ edge_annotation = False
# "degree", edge_thickness = 1
# "eigenvector", figure = create_2d_graph(
# "betweeness", graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
# "closeness", )
# "pagerank", assert type(figure) is go.Figure
# ]
metric = "degree"
layout = "Circular"
figure = create_2d_graph(
graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
)
assert type(figure) is go.Figure
edge_annotation = True
layout = "Kamada Kawai"
figure = create_2d_graph(
graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
)
assert type(figure) is go.Figure
layout = "Random"
figure = create_2d_graph(
graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
)
assert type(figure) is go.Figure
layout = "Shell (only 2D)"
figure = create_2d_graph(
graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
)
assert type(figure) is go.Figure
layout = "Spiral (only 2D)"
figure = create_2d_graph(
graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
)
assert type(figure) is go.Figure

View File

@ -1,38 +1,79 @@
"""Test the initialize Network function.""" """Test the initialize Network function."""
import datetime import datetime
import networkx as nx import plotly.graph_objects as go
import pandas as pd
from aki_prj23_transparenzregister.utils.networkx import network_3d
from aki_prj23_transparenzregister.utils.networkx.network_3d import create_3d_graph
from aki_prj23_transparenzregister.utils.networkx.network_base import initialize_network from aki_prj23_transparenzregister.utils.networkx.network_base import initialize_network
def test_initialize_network() -> None: def test_import() -> None:
"""Checks if an import co company_stats_dash can be made."""
assert network_3d is not None
def test_create_3d_graph() -> None:
"""Tests the creation of a 3D Graph."""
edges: list = [ edges: list = [
{"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"}, {"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_53", "to": "p_545", "type": "HAFTENDER_GESELLSCHAFTER"}, {"from": "c_53", "to": "p_545", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_1", "to": "c_2", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_53", "to": "c_1", "type": "HAFTENDER_GESELLSCHAFTER"},
] ]
nodes: dict = { nodes: dict = {
"c_53": { "c_53": {
"id": "c_53", "id": "c_53",
"name": "1. Freiburger Solarfonds Beteiligungs-KG", "name": "1. Freiburger Solarfonds Beteiligungs-KG",
"type": "company",
"color": "blue", "color": "blue",
}, },
"p_545": { "p_545": {
"id": "p_545", "id": "p_545",
"firstname": "Jürgen", "name": "Wetzel, Jürgen",
"lastname": "Wetzel", "type": "person",
"date_of_birth": datetime.date(1962, 11, 15), "date_of_birth": datetime.date(1962, 11, 15),
"color": "red", "color": "red",
}, },
"c_1": {
"id": "c_1",
"name": "Musterfirma",
"type": "company",
"color": "blue",
},
"c_2": {
"id": "c_2",
"name": "Firma 1",
"type": "company",
"color": "blue",
},
} }
graph, metrics = initialize_network(edges=edges, nodes=nodes) graph, metrics = initialize_network(edges=edges, nodes=nodes)
assert type(graph) is nx.Graph metric = "None"
assert type(metrics) is pd.DataFrame layout = "Spring"
# assert list(metrics.columns) == [ edge_annotation = False
# "degree", edge_thickness = 1
# "eigenvector", figure = create_3d_graph(
# "betweeness", graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
# "closeness", )
# "pagerank", assert type(figure) is go.Figure
# ]
metric = "degree"
layout = "Circular"
figure = create_3d_graph(
graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
)
assert type(figure) is go.Figure
edge_annotation = True
layout = "Kamada Kawai"
figure = create_3d_graph(
graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
)
assert type(figure) is go.Figure
layout = "Random"
figure = create_3d_graph(
graph, nodes, edges, metrics, metric, layout, edge_annotation, edge_thickness
)
assert type(figure) is go.Figure

View File

@ -3,38 +3,83 @@ import datetime
import networkx as nx import networkx as nx
import pandas as pd import pandas as pd
import pytest
from aki_prj23_transparenzregister.utils.networkx.network_base import initialize_network from aki_prj23_transparenzregister.utils.networkx import network_base
from aki_prj23_transparenzregister.utils.networkx.network_base import (
initialize_network,
initialize_network_with_reduced_metrics,
initialize_network_without_metrics,
)
def test_import() -> None:
"""Checks if an import co company_stats_dash can be made."""
assert network_base is not None
@pytest.mark.tim()
def test_initialize_network() -> None: def test_initialize_network() -> None:
edges: list = [ edges: list = [
{"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"}, {"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_53", "to": "p_545", "type": "HAFTENDER_GESELLSCHAFTER"}, {"from": "c_53", "to": "p_545", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_1", "to": "c_2", "type": "HAFTENDER_GESELLSCHAFTER"},
{"from": "c_53", "to": "c_1", "type": "HAFTENDER_GESELLSCHAFTER"},
] ]
nodes: dict = { nodes: dict = {
"c_53": { "c_53": {
"id": "c_53", "id": "c_53",
"name": "1. Freiburger Solarfonds Beteiligungs-KG", "name": "1. Freiburger Solarfonds Beteiligungs-KG",
"type": "company",
"color": "blue", "color": "blue",
}, },
"p_545": { "p_545": {
"id": "p_545", "id": "p_545",
"firstname": "Jürgen", "name": "Wetzel, Jürgen",
"lastname": "Wetzel", "type": "person",
"date_of_birth": datetime.date(1962, 11, 15), "date_of_birth": datetime.date(1962, 11, 15),
"color": "red", "color": "red",
}, },
"c_1": {
"id": "c_1",
"name": "Musterfirma",
"type": "company",
"color": "blue",
},
"c_2": {
"id": "c_2",
"name": "Firma 1",
"type": "company",
"color": "blur",
},
} }
# print(len(edges))
# print(len(nodes))
graph, metrics = initialize_network(edges=edges, nodes=nodes) graph, metrics = initialize_network(edges=edges, nodes=nodes)
assert isinstance(graph, nx.Graph) assert isinstance(graph, nx.Graph)
assert isinstance(metrics, pd.DataFrame) assert isinstance(metrics, pd.DataFrame)
# assert list(metrics.columns) == [ assert list(metrics.columns) == [
# "degree", "eigenvector",
# "eigenvector", "degree",
# "betweeness", "betweenness",
# "closeness", "closeness",
# "pagerank", "pagerank",
# ] "category",
"designation",
"id",
]
graph_reduced, metrics_reduced = initialize_network_with_reduced_metrics(
edges=edges, nodes=nodes
)
assert isinstance(graph_reduced, nx.Graph)
assert isinstance(metrics_reduced, pd.DataFrame)
assert list(metrics_reduced.columns) == [
"degree",
"betweenness",
"closeness",
"category",
"designation",
"id",
]
graph = initialize_network_without_metrics(edges=edges, nodes=nodes)
assert isinstance(graph_reduced, nx.Graph)

View File

@ -1,37 +1,150 @@
"""Test the initialize Network function.""" """Test the initialize Network function."""
import datetime from collections.abc import Generator
import networkx as nx
import pandas as pd import pandas as pd
import pytest
from sqlalchemy.orm import Session
from aki_prj23_transparenzregister.utils.networkx.network_base import initialize_network 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,
)
def test_initialize_network() -> None: @pytest.fixture(autouse=True)
edges: list = [ def _set_session(full_db: Session) -> Generator[None, None, None]:
{"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"}, """Sets a session for the dash application to be used."""
{"from": "c_53", "to": "p_545", "type": "HAFTENDER_GESELLSCHAFTER"}, SessionHandler.session = full_db
] yield
nodes: dict = { SessionHandler.session = None
"c_53": {
"id": "c_53",
"name": "1. Freiburger Solarfonds Beteiligungs-KG", def test_import() -> None:
"color": "blue", """Checks if an import co company_stats_dash can be made."""
}, assert networkx_data is not None
"p_545": {
"id": "p_545",
"name": "Jürgen Wenzel", # def test_initialize_network() -> None:
"date_of_birth": datetime.date(1962, 11, 15), # edges: list = [
"color": "red", # {"from": "p_545", "to": "c_53", "type": "HAFTENDER_GESELLSCHAFTER"},
}, # {"from": "c_53", "to": "p_545", "type": "HAFTENDER_GESELLSCHAFTER"},
} # {"from": "c_1", "to": "c_2", "type": "HAFTENDER_GESELLSCHAFTER"},
graph, metrics = initialize_network(edges=edges, nodes=nodes) # {"from": "c_53", "to": "c_1", "type": "HAFTENDER_GESELLSCHAFTER"},
assert type(graph) is nx.Graph # ]
assert type(metrics) is pd.DataFrame # nodes: dict = {
# assert list(metrics.columns) == [ # "c_53": {
# "degree", # "id": "c_53",
# "eigenvector", # "name": "1. Freiburger Solarfonds Beteiligungs-KG",
# "betweeness", # "type": "company",
# "closeness", # "color": "blue",
# "pagerank", # },
# ] # "p_545": {
# "id": "p_545",
# "name": "Wetzel, Jürgen",
# "type": "person",
# "date_of_birth": datetime.date(1962, 11, 15),
# "color": "red",
# },
# "c_1": {
# "id": "c_1",
# "name": "Musterfirma",
# "type": "company",
# "color": "blue",
# },
# "c_2": {
# "id": "c_2",
# "name": "Firma 1",
# "type": "company",
# "color": "blur",
# },
# }
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()
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()
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()
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()
selected_relation_type = "HAFTENDER_GESELLSCHAFTER"
company_relations_df = 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)
assert isinstance(nodes, dict)
assert isinstance(edges, list)
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)
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)
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
# @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