1
+"""Phase 3 acceptance: note engine evaluates formulas against real balances."""
2
+
3
+from __future__ import annotations
4
+
5
+import pytest
6
+
7
+from aram_backend.models import NoteRow
8
+from aram_backend.services.note_engine import NoteEngineError, evaluate_note, evaluate_year_notes
9
+from test_statement_build import _balance, _note, _setup
10
+
11
+
12
+def _row(session, note, order, label, formula):
13
+ r = NoteRow(note_id=note.id, row_order=order, label=label, formula_logic=formula)
14
+ session.add(r)
15
+ session.flush()
16
+ return r
17
+
18
+
19
+def _seed(session):
20
+ year, source = _setup(session)
21
+ _balance(session, year, source, "148348", 100, 3)
22
+ _balance(session, year, source, "241001", -60, 4)
23
+ session.flush()
24
+ return year
25
+
26
+
27
+class TestNoteEngine:
28
+ def test_rows_with_account_and_subtotal(self, session):
29
+ year = _seed(session)
30
+ note = _note(session, year, "6", "نقد", "BS", "Assets")
31
+ _row(session, note, 0, "حساب نقد", 'acct("148348")')
32
+ _row(session, note, 1, "پرداختنی", 'acct("241001")')
33
+ _row(session, note, 2, "جمع", "sum(row(0), row(1))")
34
+ session.flush()
35
+ result = evaluate_note(session, note.id)
36
+ values = [r["value"] for r in result["rows"]]
37
+ assert values == ["100", "-60", "40"]
38
+ assert result["rows"][0]["accounts_referenced"] == ["148348"]
39
+
40
+ def test_cross_note_reference(self, session):
41
+ year = _seed(session)
42
+ n6 = _note(session, year, "6", "نقد", "BS", "Assets")
43
+ _row(session, n6, 0, "نقد", 'acct("148348")')
44
+ n7 = _note(session, year, "7", "مشتق", "BS", "Assets")
45
+ _row(session, n7, 0, "مشتق", 'note("6", 0) + 5')
46
+ session.flush()
47
+ result = evaluate_note(session, n7.id)
48
+ assert result["rows"][0]["value"] == "105"
49
+
50
+ def test_circular_reference_detected(self, session):
51
+ year = _seed(session)
52
+ n6 = _note(session, year, "6", "الف", "BS", "Assets")
53
+ _row(session, n6, 0, "a", 'note("7", 0)')
54
+ n7 = _note(session, year, "7", "ب", "BS", "Assets")
55
+ _row(session, n7, 0, "b", 'note("6", 0)')
56
+ session.flush()
57
+ with pytest.raises(NoteEngineError, match="circular"):
58
+ evaluate_note(session, n6.id)
59
+
60
+ def test_forward_row_reference_rejected(self, session):
61
+ year = _seed(session)
62
+ note = _note(session, year, "6", "نقد", "BS", "Assets")
63
+ _row(session, note, 0, "bad", "row(1)")
64
+ _row(session, note, 1, "x", 'acct("148348")')
65
+ session.flush()
66
+ with pytest.raises(NoteEngineError):
67
+ evaluate_note(session, note.id)
68
+
69
+ def test_unknown_account_rejected(self, session):
70
+ year = _seed(session)
71
+ note = _note(session, year, "6", "نقد", "BS", "Assets")
72
+ _row(session, note, 0, "bad", 'acct("999999")')
73
+ session.flush()
74
+ with pytest.raises(NoteEngineError):
75
+ evaluate_note(session, note.id)
76
+
77
+ def test_evaluate_year_notes(self, session):
78
+ year = _seed(session)
79
+ note = _note(session, year, "6", "نقد", "BS", "Assets")
80
+ _row(session, note, 0, "نقد", 'acct("148348")')
81
+ session.flush()
82
+ results = evaluate_year_notes(session, year.id)
83
+ assert len(results) == 1
84
+ assert results[0]["rows"][0]["value"] == "100"
85
+
86
+
87
+class TestNoteEngineApi:
88
+ def test_create_rows_and_evaluate(self, api_client):
89
+ c = api_client
90
+ year_id = c.post(
91
+ "/api/v1/companies", json={"code": "N3", "name": "x", "year": 1404}
92
+ ).json()["year_id"]
93
+ csv = (
94
+ "حساب,kol,ACCOUNT_NAME,year,نوع,T1_CODE,T2_CODE,T3_CODE,T4_CODE,OPENING,Debit,Credit,CLOSING\n"
95
+ "8348,14,نقد,1404,O,,,,,0,100,0,100\n"
96
+ ).encode()
97
+ c.post(f"/api/v1/years/{year_id}/ledger", files={"file": ("l.csv", csv, "text/csv")})
98
+ note_id = c.post(
99
+ f"/api/v1/years/{year_id}/notes",
100
+ json={"note_number": "6", "title": "نقد", "statement": "BS", "section": "Assets"},
101
+ ).json()["note_id"]
102
+ r = c.post(
103
+ f"/api/v1/years/{year_id}/notes/{note_id}/rows",
104
+ json={"row_order": 0, "label": "نقد", "formula_logic": 'acct("148348")'},
105
+ )
106
+ assert r.status_code == 200, r.text
107
+ r = c.get(f"/api/v1/notes/{note_id}/evaluate")
108
+ assert r.status_code == 200, r.text
109
+ assert r.json()["rows"][0]["value"] == "100"
110
+
111
+ def test_invalid_formula_rejected_on_create(self, api_client):
112
+ c = api_client
113
+ year_id = c.post(
114
+ "/api/v1/companies", json={"code": "N4", "name": "x", "year": 1404}
115
+ ).json()["year_id"]
116
+ note_id = c.post(
117
+ f"/api/v1/years/{year_id}/notes",
118
+ json={"note_number": "6", "title": "نقد", "statement": "BS", "section": "Assets"},
119
+ ).json()["note_id"]
120
+ r = c.post(
121
+ f"/api/v1/years/{year_id}/notes/{note_id}/rows",
122
+ json={"row_order": 0, "label": "bad", "formula_logic": "__import__('os')"},
123
+ )
124
+ assert r.status_code == 422