test(ingestion): validate accepted workbook profiles
davoodmehraban89 committed
Aug 24, 2026 at 06:07 UTC
66f0cb6f7f20710316b8a5fbaa00180cee34a0d9
1 file changed
+86
backend/tests/test_workbook_inspection.py
new
+86
@@ -0,0 +1,86 @@
1
+from io import BytesIO
2
+
3
+import pytest
4
+from openpyxl import Workbook
5
+
6
+from aram_backend.workbook_inspection import WorkbookInspectionError, inspect_xlsx
7
+
8
+
9
+def workbook_bytes(sheet_rows):
10
+ workbook = Workbook()
11
+ default = workbook.active
12
+ workbook.remove(default)
13
+ for sheet_name, rows in sheet_rows:
14
+ sheet = workbook.create_sheet(sheet_name)
15
+ for row in rows:
16
+ sheet.append(row)
17
+ output = BytesIO()
18
+ workbook.save(output)
19
+ workbook.close()
20
+ return output.getvalue()
21
+
22
+
23
+def test_inspects_accepted_general_ledger_structure():
24
+ headers = [
25
+ "حساب",
26
+ "ACCOUNT_NAME",
27
+ "year",
28
+ "OPENING",
29
+ "Debit",
30
+ "Credit",
31
+ "CLOSING",
32
+ ]
33
+ content = workbook_bytes([("Source", [["source ledger"], headers])])
34
+
35
+ result = inspect_xlsx(content, document_type="general_ledger")
36
+
37
+ assert result.data_sheet == "Source"
38
+ assert result.header_row == 2
39
+ assert result.headers == tuple(headers)
40
+
41
+
42
+def test_inspects_accepted_mapping_structure():
43
+ headers = [
44
+ "کد حساب",
45
+ "نوع",
46
+ "کد یادداشت",
47
+ "شرح یادداشت",
48
+ "فرمول یادداشت اصلی",
49
+ "فرمول یادداشت فرعی",
50
+ ]
51
+ content = workbook_bytes([("Data", [headers])])
52
+
53
+ result = inspect_xlsx(content, document_type="mapping")
54
+
55
+ assert result.data_sheet == "Data"
56
+ assert result.header_row == 1
57
+
58
+
59
+def test_inspects_primary_financial_statement_sheet_set():
60
+ names = ["سودوزيان", "وضعيت مالي", "حقوق مالكانه", "جريان هاي نقدي"]
61
+ content = workbook_bytes([(name, []) for name in names])
62
+
63
+ result = inspect_xlsx(content, document_type="financial_statement")
64
+
65
+ assert result.sheet_names == tuple(names)
66
+ assert result.headers == ()
67
+
68
+
69
+@pytest.mark.parametrize(
70
+ "content",
71
+ [
72
+ workbook_bytes([("Wrong", [["not the expected sheet"]])]),
73
+ workbook_bytes([("Source", [["source ledger"], ["حساب"]])]),
74
+ b"not-an-xlsx-file",
75
+ ],
76
+)
77
+def test_rejects_invalid_or_mismatched_workbooks(content):
78
+ with pytest.raises(WorkbookInspectionError):
79
+ inspect_xlsx(content, document_type="general_ledger")
80
+
81
+
82
+def test_rejects_document_type_without_an_xlsx_profile():
83
+ content = workbook_bytes([("Sheet", [])])
84
+
85
+ with pytest.raises(WorkbookInspectionError, match="no XLSX inspection profile"):
86
+ inspect_xlsx(content, document_type="trial_balance")