test(statements): compute tiny reference using high-precision Decimal context
davoodmehraban89 committed
Oct 31, 2026 at 06:47 UTC
d138090ca8f2428bcaa8996589f6d813ad046587
1 file changed
+8
-4
backend/tests/test_income_statement_candidate.py
+8
-4
@@ -3,6 +3,7 @@ from __future__ import annotations
3
4
from dataclasses import replace
5
from io import BytesIO
6
+from decimal import Decimal, localcontext
7
import json
8
9
import pytest
@@ -47,7 +48,7 @@ def workbook(*, costs="110", missing_mapping=False, include_bs=False,
48
if include_bs:
49
items += (("AST", "0", "Assets", "A01", "BS", 1),)
50
for index, (code, amount, section, note, category, sign) in enumerate(items, 2):
50
- closing = str(-__import__("decimal").Decimal(amount)) if sign == -1 else amount
51
+ closing = str(-Decimal(amount)) if sign == -1 else amount
52
add("Accounts", company_code="001", fiscal_year=1404,
53
general_code=code, general_name=section, account_full_code=code,
54
account_name="Synthetic " + section, opening=closing, debit="0",
@@ -75,7 +76,7 @@ def references(*, costs="110", include_bs=False, revenue="200"):
76
result = []
77
for code, amount, section, note, category, sign in ITEMS:
78
replacement = revenue if code == "REV" else costs if code == "COS" else amount
78
- closing = str(-__import__("decimal").Decimal(replacement)) if sign == -1 else replacement
79
+ closing = str(-Decimal(replacement)) if sign == -1 else replacement
80
result.append(NoteReference("IS", section, note, "", closing, "SYNTH-" + code))
81
if include_bs:
82
result.append(NoteReference("BS", "Assets", "A01", "", "0", "SYNTH-BS"))
@@ -131,7 +132,7 @@ def test_real_loss_is_not_false_positive_if_expense_exceeds_income():
132
assert not result.findings
133
134
134
-def test_no_tax_is_a_valid_zero_amount_with_explicit_reference():
135
+def test_zero_cost_of_revenue_is_valid_with_explicit_reference():
136
# Source and reference keep the same input amount; no unseen tax estimate.
137
result = build(data=workbook(costs="0"), refs=references(costs="0"))
138
assert result.cost_of_revenue_total == "0"
@@ -283,4 +284,7 @@ def test_tiny_exact_decimal_is_not_rounded_away():
284
)
285
assert result.eligible_for_analyst_review
286
assert result.cost_of_revenue_total == small
286
- assert result.net_result == str(__import__("decimal").Decimal("155") - __import__("decimal").Decimal(small))
287
+ with localcontext() as context:
288
+ context.prec = 400
289
+ expected = str(Decimal("155") - Decimal(small))
290
+ assert result.net_result == expected