test: Unit test new KPI extraction

This commit is contained in:
TrisNol 2023-10-21 10:56:04 +02:00
parent f8a0d58314
commit fecf42d75a
2 changed files with 78 additions and 4 deletions

View File

@ -200,7 +200,9 @@ class Bundesanzeiger:
soup = BeautifulSoup(report, features="html.parser")
for table in soup.find_all("table", {"class": "std_table"}):
try:
results = pd.read_html(StringIO(str(table)), flavor="bs4")
results = pd.read_html(
StringIO(str(table)), flavor="bs4", thousands=".", decimal=","
)
if len(results) > 0:
data_frame = results[0]
result.append(data_frame)
@ -229,10 +231,10 @@ class Bundesanzeiger:
return None
def parse_string_to_float(value: str | float) -> float | None:
if value is None:
return None
try:
if value is None:
return None
return float(str(value).replace(".", "").replace(",", "."))
return float(value)
except Exception:
return None

View File

@ -136,3 +136,75 @@ def test_get_information_no_results(mock_bundesanzeiger: Mock) -> None:
ba = Bundesanzeiger()
result = ba.get_information("PRJ 23 Transparenzregister GmbH", "Iserlohn")
assert len(result) == 0
def test_extract_tables_from_reports() -> None:
report = """
<table>
</table>
<div>
Möge die Macht mir dir sein
<table class="std_table">
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
<tr>
<td>42</td>
<td>4711</td>
</tr>
</table>
</div>
"""
ba = Bundesanzeiger()
result = ba.__extract_tables_from_report__(report)
assert len(result) == 1
def test_parse_tables_to_kpis() -> None:
report = """
<table>
</table>
<div>
Möge die Macht mir dir sein
<table class="std_table">
<tr>
<th>Position</th>
<th>2023 in T</th>
<th>1997 in </th>
</tr>
<tr>
<td>a) Umlaufvermögen</td>
<td>12,13</td>
<td>4711</td>
</tr>
<tr>
<td>+EBIT</td>
<td>1123</td>
<td>4711</td>
</tr>
<tr>
<td>To be ignored</td>
<td>I've tried so hard and got so far, but in the end it doesn't even matter</td>
<td>4711</td>
</tr>
<tr>
<td>Gewinn</td>
<td></td>
<td>4711</td>
</tr>
<tr>
<td>Jahresüberschuss</td>
<td>4.130,12</td>
<td>4711</td>
</tr>
</table>
</div>
"""
ba = Bundesanzeiger()
result = ba.parse_tables_to_kpis(report)
assert result == {
"Umlaufvermögen": 12130.0,
"EBIT": 1123000.0,
"Jahresüberschuss": 4130120.0,
}