update data based on selected company (#122)

Added UI elements to select a company and update shown data depending on chosen company



---------

Co-authored-by: Philipp Horstenkamp <philipp@horstenkamp.de>
This commit is contained in:
KM-R
2023-09-19 23:45:10 +02:00
committed by GitHub
parent 80f077ee7a
commit 487b2f42d1
15 changed files with 900 additions and 732 deletions

View File

@ -3,6 +3,7 @@ import datetime
import os
from collections.abc import Generator
from inspect import getmembers, isfunction
from typing import Any
import pytest
from sqlalchemy.engine import Engine
@ -49,7 +50,60 @@ def empty_db() -> Generator[Session, None, None]:
@pytest.fixture()
def full_db(empty_db: Session) -> Session:
def finance_statements() -> list[dict[str, Any]]:
"""Creates a list of finance statements."""
return [
{
"id": 1,
"company_id": 1,
"date": datetime.date.fromisoformat("2023-01-01"),
"total_volume": 1000.0,
"ebit": 1000.0,
"ebitda": 1000.0,
"ebit_margin": 1000.0,
"total_balance": 1000.0,
"equity": 1000.0,
"debt": 1000.0,
"return_on_equity": 1000.0,
"capital_turnover_rate": 1000.0,
"current_liabilities": 1000.0,
"dividends": float("NaN"),
"net_income": float("NaN"),
"assets": 1000.0,
"long_term_debt": 1000.0,
"short_term_debt": 1000.0,
"revenue": 1000.0,
"cash_flow": 1000.0,
"current_assets": 1000.0,
},
{
"id": 2,
"company_id": 1,
"date": datetime.date.fromisoformat("2022-01-01"),
"total_volume": 1100.0,
"ebit": 1100.0,
"ebitda": 1100.0,
"ebit_margin": 1100.0,
"total_balance": 1100.0,
"equity": 1100.0,
"debt": 1100.0,
"return_on_equity": 1100.0,
"capital_turnover_rate": 1100.0,
"current_liabilities": 1100.0,
"dividends": float("NaN"),
"net_income": float("NaN"),
"assets": 1100.0,
"long_term_debt": 1100.0,
"short_term_debt": 1100.0,
"revenue": 1100.0,
"cash_flow": 1100.0,
"current_assets": 1100.0,
},
]
@pytest.fixture()
def full_db(empty_db: Session, finance_statements: list[dict[str, Any]]) -> Session:
"""Fills a db with some test data."""
empty_db.add_all(
[
@ -112,5 +166,13 @@ def full_db(empty_db: Session) -> Session:
]
)
empty_db.commit()
empty_db.add_all(
[
entities.AnnualFinanceStatement(**finance_statement)
for finance_statement in finance_statements
]
)
empty_db.commit()
# print(pd.read_sql_table("company", empty_db.bind).to_string())
return empty_db

View File

@ -0,0 +1,118 @@
"""Tests for ui elements."""
import pandas as pd
from sqlalchemy.orm import Session
from aki_prj23_transparenzregister.ui import ui_elements
def test_import() -> None:
"""Checks if an import co ui_elements can be made."""
assert ui_elements is not None
def test_get_company_data(full_db: Session) -> None:
"""Checks if data from the company and district court tables can be accessed."""
company_df = ui_elements.get_company_data(full_db)
test_data = pd.DataFrame(
{
"company_id": {0: 1, 1: 2, 2: 3},
"company_hr": {0: "HRB 123", 1: "HRB 123", 2: "HRB 12"},
"company_court_id": {0: 2, 1: 1, 2: 2},
"company_name": {
0: "Some Company GmbH",
1: "Other Company GmbH",
2: "Third Company GmbH",
},
"company_street": {0: "Sesamstr.", 1: "Sesamstr.", 2: None},
"company_zip_code": {0: "12345", 1: "12345", 2: None},
"company_city": {0: "TV City", 1: "TV City", 2: None},
"company_last_update": {
0: "2023-01-01",
1: "2023-01-01",
2: "2023-01-01",
},
"company_sector": {0: None, 1: None, 2: None},
"district_court_name": {
0: "Amtsgericht Dortmund",
1: "Amtsgericht Bochum",
2: "Amtsgericht Dortmund",
},
}
)
test_data = test_data.set_index("company_id")
pd.testing.assert_frame_equal(company_df, test_data)
def test_get_finance_data(full_db: Session) -> None:
"""Checks if data from the company and finance tables can be accessed."""
finance_df = ui_elements.get_finance_data(full_db)
test_data = pd.DataFrame(
{
"annual_finance_statement_id": {0: 1, 1: 2},
"annual_finance_statement_company_id": {0: 1, 1: 1},
"annual_finance_statement_date": {0: "2023-01-01", 1: "2022-01-01"},
"annual_finance_statement_total_volume": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_ebit": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_ebitda": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_ebit_margin": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_total_balance": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_equity": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_debt": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_return_on_equity": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_capital_turnover_rate": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_current_liabilities": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_dividends": {0: None, 1: None},
"annual_finance_statement_net_income": {0: None, 1: None},
"annual_finance_statement_assets": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_long_term_debt": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_short_term_debt": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_revenue": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_cash_flow": {0: 1000.0, 1: 1100.0},
"annual_finance_statement_current_assets": {0: 1000.0, 1: 1100.0},
"company_name": {0: "Some Company GmbH", 1: "Some Company GmbH"},
"company_id": {0: 1, 1: 1},
}
)
pd.testing.assert_frame_equal(finance_df, test_data)
def test_create_header() -> None:
"""Checks if the header can be created."""
options = {1: "a", 2: "b"}
ui_elements.create_header(options)
def test_create_company_header() -> None:
"""Checks if the company header can be created."""
selected_company = "Test GmbH"
ui_elements.create_company_header(selected_company)
def test_create_company_stats(full_db: Session) -> None:
"""Checks if the company widgets can be created."""
company_df = ui_elements.get_company_data(full_db)
value_chosen = 1
selected_company_stats = company_df.loc[value_chosen]
ui_elements.create_company_stats(selected_company_stats)
def test_create_tabs(full_db: Session) -> None:
"""Checks if the tabs of the company page can be created."""
selected_company_id = 1
finance_df = ui_elements.get_finance_data(full_db)
selected_finance_df = finance_df.loc[
finance_df["company_id"] == selected_company_id
]
ui_elements.create_tabs(selected_finance_df)
def test_kennzahlen_layout(full_db: Session) -> None:
"""Checks if the financial metric layout of the company page can be created."""
selected_company_id = 1
finance_df = ui_elements.get_finance_data(full_db)
selected_finance_df = finance_df.loc[
finance_df["company_id"] == selected_company_id
]
ui_elements.kennzahlen_layout(selected_finance_df)

View File

@ -168,7 +168,7 @@ def test_get_person_id_value_check(
data_transfer.get_person_id(
firstname,
surname,
date.fromisoformat(date_str) if date_str else None, # type: ignore
date.fromisoformat(date_str) if date_str else None,
full_db,
)
@ -941,7 +941,11 @@ def test_add_annual_report_to_unknown_company(
@pytest.mark.parametrize("year", [2023, 2025, 2020])
@pytest.mark.parametrize("short_term_debt", [2023.2, 2025.5, 2020.5, float("NaN")])
def test_add_annual_report(
short_term_debt: float, company_id: int, year: int, full_db: Session
short_term_debt: float,
company_id: int,
year: int,
finance_statements: list[dict[str, Any]],
full_db: Session,
) -> None:
"""Tests the addition of annual financial records."""
data_transfer.add_annual_report(
@ -961,34 +965,38 @@ def test_add_annual_report(
df_prior = pd.read_sql_table(
entities.AnnualFinanceStatement.__tablename__, full_db.bind # type: ignore
)
expected_results = pd.DataFrame(
finance_statements
+ [
{
"id": 3,
"company_id": company_id,
"date": pd.to_datetime(date(year, 1, 1)),
"total_volume": float("NaN"),
"ebit": 123.0,
"ebitda": 235.0,
"ebit_margin": float("NaN"),
"total_balance": float("NaN"),
"equity": float("NaN"),
"debt": float("NaN"),
"return_on_equity": float("NaN"),
"capital_turnover_rate": float("NaN"),
"current_liabilities": float("NaN"),
"dividends": float("NaN"),
"net_income": float("NaN"),
"assets": float("NaN"),
"long_term_debt": float("NaN"),
"short_term_debt": short_term_debt,
"revenue": float("NaN"),
"cash_flow": float("NaN"),
"current_assets": float("NaN"),
}
]
)
expected_results["date"] = pd.to_datetime(expected_results["date"])
pd.testing.assert_frame_equal(
pd.DataFrame(
[
{
"id": 1,
"company_id": company_id,
"date": pd.to_datetime(date(year, 1, 1)),
"total_volume": float("NaN"),
"ebit": 123.0,
"ebitda": 235.0,
"ebit_margin": float("NaN"),
"total_balance": float("NaN"),
"equity": float("NaN"),
"debt": float("NaN"),
"return_on_equity": float("NaN"),
"capital_turnover_rate": float("NaN"),
"current_liabilities": float("NaN"),
"dividends": float("NaN"),
"net_income": float("NaN"),
"assets": float("NaN"),
"long_term_debt": float("NaN"),
"short_term_debt": short_term_debt,
"revenue": float("NaN"),
"cash_flow": float("NaN"),
"current_assets": float("NaN"),
}
]
),
expected_results,
df_prior,
)