Spaces:
Sleeping
Sleeping
File size: 10,903 Bytes
48cf766 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | """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()
|