grantforge-api / backend /tests /test_excel_parser.py
GrantForge Bot
Deploy sha-9a5957fcdef15b7e2623f8b147cda6026475aee0 — source build (no GHCR)
3a3734f
Raw
History Blame Contribute Delete
1.87 kB
import os
import pandas as pd
import pytest
from rag_pipeline.excel_parser import parse_excel_financials
@pytest.fixture
def sample_csv(tmp_path):
df = pd.DataFrame({
"Miesiąc": ["Styczeń", "Luty", "Marzec"],
"Przychody": [10000, 15000, 20000],
"EBITDA": [2000, 3000, 4000]
})
file_path = tmp_path / "finanse.csv"
df.to_csv(file_path, index=False)
return str(file_path)
@pytest.fixture
def sample_excel(tmp_path):
df1 = pd.DataFrame({
"Koszty": ["Marketing", "R&D", "IT"],
"Wartość": [5000, 20000, 10000]
})
df2 = pd.DataFrame({
"Przychody ze sprzedaży": [100000],
"Zysk Netto": [25000]
})
file_path = tmp_path / "finanse.xlsx"
with pd.ExcelWriter(file_path) as writer:
df1.to_excel(writer, sheet_name="Koszty", index=False)
df2.to_excel(writer, sheet_name="Wyniki", index=False)
return str(file_path)
def test_parse_csv(sample_csv):
result = parse_excel_financials(sample_csv)
assert result["parser"] == "pandas_excel"
assert "Przychody" in result["text"]
assert "EBITDA" in result["text"]
assert result["metadata"].get("has_ebitda") is True
assert result["metadata"].get("has_revenue") is True
def test_parse_excel(sample_excel):
result = parse_excel_financials(sample_excel)
assert result["parser"] == "pandas_excel"
assert "Koszty" in result["text"]
assert "Wyniki" in result["text"]
assert "Przychody" in result["text"]
assert result["metadata"].get("has_revenue") is True
assert result["metadata"].get("has_ebitda") is None
def test_parse_invalid_file(tmp_path):
invalid_file = tmp_path / "broken.xlsx"
invalid_file.write_text("not an excel file")
result = parse_excel_financials(str(invalid_file))
assert result["parser"] == "error"
assert result["text"] == ""