Spaces:
Sleeping
Sleeping
Download tests/test_model_diff.py from bdschi1/financial-model-builder-v2: direct link, hf CLI and curl.
- Browser
- Download file 10.9 kB
-
https://huggingface.co/spaces/bdschi1/financial-model-builder-v2/resolve/main/tests/test_model_diff.py
- Command line
-
hf download hf://spaces/bdschi1/financial-model-builder-v2/tests/test_model_diff.py
-
curl -L -o test_model_diff.py https://huggingface.co/spaces/bdschi1/financial-model-builder-v2/resolve/main/tests/test_model_diff.py
10.9 kB
| """Tests for model_diff module.""" | |
| import os | |
| import sys | |
| import tempfile | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| import openpyxl | |
| from model_diff import ( | |
| diff_workbooks, diff_key_metrics, | |
| _is_numeric, _to_float, _build_label_map, _get_col_header, | |
| ) | |
| # ββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _make_workbook(sheets_data: dict, path: str): | |
| """ | |
| Create a workbook from sheets_data dict. | |
| sheets_data: {"SheetName": [[(row1col1, ...), (row2col1, ...)], ...]} | |
| Each sheet is a list of row tuples. | |
| """ | |
| wb = openpyxl.Workbook() | |
| wb.remove(wb.active) | |
| for name, rows in sheets_data.items(): | |
| ws = wb.create_sheet(title=name) | |
| for r_idx, row in enumerate(rows, 1): | |
| for c_idx, val in enumerate(row, 1): | |
| ws.cell(row=r_idx, column=c_idx, value=val) | |
| wb.save(path) | |
| wb.close() | |
| # ββ _is_numeric tests βββββββββββββββββββββββββββββββββββββββββββ | |
| def test_is_numeric_int(): | |
| assert _is_numeric(42) is True | |
| def test_is_numeric_float(): | |
| assert _is_numeric(3.14) is True | |
| def test_is_numeric_string_number(): | |
| assert _is_numeric("123.45") is True | |
| def test_is_numeric_string_dollar(): | |
| assert _is_numeric("$1,234.56") is True | |
| def test_is_numeric_none(): | |
| assert _is_numeric(None) is False | |
| def test_is_numeric_text(): | |
| assert _is_numeric("Revenue") is False | |
| def test_is_numeric_empty(): | |
| assert _is_numeric("") is False | |
| # ββ _to_float tests βββββββββββββββββββββββββββββββββββββββββββββ | |
| def test_to_float_int(): | |
| assert _to_float(42) == 42.0 | |
| def test_to_float_float(): | |
| assert _to_float(3.14) == 3.14 | |
| def test_to_float_string(): | |
| assert _to_float("100.5") == 100.5 | |
| def test_to_float_dollar_string(): | |
| assert _to_float("$1,234.56") == 1234.56 | |
| def test_to_float_none(): | |
| assert _to_float(None) is None | |
| def test_to_float_text(): | |
| assert _to_float("hello") is None | |
| # ββ diff_workbooks tests ββββββββββββββββββββββββββββββββββββββββ | |
| def test_diff_identical_workbooks(): | |
| """Identical workbooks should show no changes.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| data = {"Sheet1": [("Label", "FY2023"), ("Revenue", 100.0)]} | |
| _make_workbook(data, old_path) | |
| _make_workbook(data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert result["changes"] == [] | |
| assert result["sheets_added"] == [] | |
| assert result["sheets_removed"] == [] | |
| def test_diff_changed_value(): | |
| """Changed numeric value should appear in changes.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = {"Sheet1": [("Label", "FY2023"), ("Revenue", 100.0)]} | |
| new_data = {"Sheet1": [("Label", "FY2023"), ("Revenue", 110.0)]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert len(result["changes"]) == 1 | |
| change = result["changes"][0] | |
| assert change["sheet"] == "Sheet1" | |
| assert change["old"] == 100.0 | |
| assert change["new"] == 110.0 | |
| assert change["delta"] == 10.0 | |
| def test_diff_pct_calculation(): | |
| """Percentage change should be calculated correctly.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = {"Sheet1": [("Metric", 200.0)]} | |
| new_data = {"Sheet1": [("Metric", 220.0)]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert len(result["changes"]) == 1 | |
| assert result["changes"][0]["pct"] == 0.1 # 10% | |
| def test_diff_sheet_added(): | |
| """New sheet should appear in sheets_added.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = {"Sheet1": [("A", 1)]} | |
| new_data = {"Sheet1": [("A", 1)], "NewSheet": [("B", 2)]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert "NewSheet" in result["sheets_added"] | |
| def test_diff_sheet_removed(): | |
| """Removed sheet should appear in sheets_removed.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = {"Sheet1": [("A", 1)], "OldSheet": [("B", 2)]} | |
| new_data = {"Sheet1": [("A", 1)]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert "OldSheet" in result["sheets_removed"] | |
| def test_diff_multiple_changes(): | |
| """Multiple changes across sheets should all be captured.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = { | |
| "IS": [("Label",), ("Revenue", 100.0), ("COGS", -60.0)], | |
| "BS": [("Label",), ("Assets", 500.0)], | |
| } | |
| new_data = { | |
| "IS": [("Label",), ("Revenue", 110.0), ("COGS", -65.0)], | |
| "BS": [("Label",), ("Assets", 520.0)], | |
| } | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert len(result["changes"]) == 3 | |
| def test_diff_ignores_text_changes(): | |
| """Changes to text cells should be ignored.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = {"Sheet1": [("Old Label",)]} | |
| new_data = {"Sheet1": [("New Label",)]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert len(result["changes"]) == 0 | |
| def test_diff_output_report(): | |
| """Output report should be written when output_path is provided.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| report_path = os.path.join(tmpdir, "diff_report.txt") | |
| old_data = {"Sheet1": [("Revenue", 100.0)]} | |
| new_data = {"Sheet1": [("Revenue", 110.0)]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path, output_path=report_path) | |
| assert os.path.exists(report_path) | |
| assert result["report_path"] == report_path | |
| content = open(report_path).read() | |
| assert "Diff Report" in content | |
| def test_diff_summary_text(): | |
| """Summary should describe the changes.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = {"Sheet1": [("Revenue", 100.0), ("COGS", -60.0)]} | |
| new_data = {"Sheet1": [("Revenue", 110.0), ("COGS", -65.0)]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert "2 cell(s) changed" in result["summary"] | |
| assert "1 sheet(s)" in result["summary"] | |
| def test_diff_new_value_from_none(): | |
| """A cell that was None and now has a value should show as a change.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = {"Sheet1": [("Revenue", None)]} | |
| new_data = {"Sheet1": [("Revenue", 100.0)]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| result = diff_workbooks(old_path, new_path) | |
| assert len(result["changes"]) == 1 | |
| assert result["changes"][0]["old"] is None | |
| assert result["changes"][0]["new"] == 100.0 | |
| # ββ diff_key_metrics tests ββββββββββββββββββββββββββββββββββββββ | |
| def test_diff_key_metrics_filters(): | |
| """diff_key_metrics should only return changes matching KEY_LABELS.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| old_path = os.path.join(tmpdir, "old.xlsx") | |
| new_path = os.path.join(tmpdir, "new.xlsx") | |
| old_data = {"Sheet1": [ | |
| ("FY2023",), | |
| ("Revenue", 100.0), | |
| ("Custom Metric", 50.0), | |
| ]} | |
| new_data = {"Sheet1": [ | |
| ("FY2023",), | |
| ("Revenue", 110.0), | |
| ("Custom Metric", 55.0), | |
| ]} | |
| _make_workbook(old_data, old_path) | |
| _make_workbook(new_data, new_path) | |
| key = diff_key_metrics(old_path, new_path) | |
| # Revenue should match, Custom Metric should not | |
| labels = [c["label"].lower() for c in key] | |
| assert any("revenue" in l for l in labels) | |
| assert not any("custom" in l for l in labels) | |
| # ββ _build_label_map tests ββββββββββββββββββββββββββββββββββββββ | |
| def test_build_label_map(): | |
| wb = openpyxl.Workbook() | |
| ws = wb.active | |
| ws.cell(row=1, column=1, value="Header") | |
| ws.cell(row=2, column=1, value="Revenue") | |
| ws.cell(row=3, column=2, value="EBITDA") # col B | |
| labels = _build_label_map(ws) | |
| assert labels[1] == "Header" | |
| assert labels[2] == "Revenue" | |
| assert labels[3] == "EBITDA" | |
| wb.close() | |
| # ββ _get_col_header tests βββββββββββββββββββββββββββββββββββββββ | |
| def test_get_col_header(): | |
| wb = openpyxl.Workbook() | |
| ws = wb.active | |
| ws.cell(row=1, column=3, value="FY2023") | |
| ws.cell(row=5, column=3, value=100.0) | |
| header = _get_col_header(ws, 3) | |
| assert header == "FY2023" | |
| wb.close() | |
| def test_get_col_header_empty(): | |
| wb = openpyxl.Workbook() | |
| ws = wb.active | |
| # No values in first 5 rows of col 5 | |
| header = _get_col_header(ws, 5) | |
| assert header == "" | |
| wb.close() | |