@davoodmehraban89 / Aram-1 / commits / 5445f49
Fork 0

test(ingestion): cover source registration boundaries

davoodmehraban89 committed Aug 24, 2026 at 05:04 UTC 5445f49005d1c843219019ebb110a174935ae3d5
1 file changed +119
backend/tests/test_ingestion.py new
+119
@@ -0,0 +1,119 @@
1 +import hashlib
2 +
3 +import pytest
4 +
5 +from aram_backend.ingestion import SourceRegistrationError, register_source_document
6 +from aram_backend.models import Company, FiscalYear, FiscalYearStatus, SourceDocument
7 +
8 +
9 +def make_year(session, year=1404):
10 + company = Company(code=f"ING-{year}", name="Ingestion Test Company")
11 + fiscal_year = FiscalYear(company=company, year=year)
12 + session.add(company)
13 + session.commit()
14 + return fiscal_year
15 +
16 +
17 +def test_registers_hash_and_metadata_without_source_payload(session):
18 + year = make_year(session)
19 + payload = b"account_code,amount\n1000,125\n"
20 +
21 + source = register_source_document(
22 + session,
23 + year_id=year.id,
24 + filename="ledger.csv",
25 + document_type="general_ledger",
26 + content=payload,
27 + )
28 + session.commit()
29 +
30 + assert source.file_hash == hashlib.sha256(payload).hexdigest()
31 + assert source.filename == "ledger.csv"
32 + assert not hasattr(source, "content")
33 +
34 +
35 +def test_registration_is_idempotent_within_a_year(session):
36 + year = make_year(session)
37 + arguments = {
38 + "year_id": year.id,
39 + "filename": "trial-balance.xlsx",
40 + "document_type": "trial_balance",
41 + "content": b"safe-test-bytes",
42 + }
43 +
44 + first = register_source_document(session, **arguments)
45 + session.commit()
46 + second = register_source_document(session, **arguments)
47 + session.commit()
48 +
49 + assert second.id == first.id
50 + assert session.query(SourceDocument).count() == 1
51 +
52 +
53 +def test_same_source_is_isolated_between_fiscal_years(session):
54 + first_year = make_year(session, 1403)
55 + second_year = make_year(session, 1404)
56 + payload = b"same-source"
57 +
58 + first = register_source_document(
59 + session,
60 + year_id=first_year.id,
61 + filename="source.csv",
62 + document_type="general_ledger",
63 + content=payload,
64 + )
65 + second = register_source_document(
66 + session,
67 + year_id=second_year.id,
68 + filename="source.csv",
69 + document_type="general_ledger",
70 + content=payload,
71 + )
72 + session.commit()
73 +
74 + assert first.id != second.id
75 + assert first.file_hash == second.file_hash
76 +
77 +
78 +@pytest.mark.parametrize("status", [FiscalYearStatus.APPROVED, FiscalYearStatus.LOCKED])
79 +def test_protected_year_rejects_source_registration(session, status):
80 + year = make_year(session)
81 + year.status = FiscalYearStatus.APPROVED
82 + session.commit()
83 + if status is FiscalYearStatus.LOCKED:
84 + year.status = FiscalYearStatus.LOCKED
85 + session.commit()
86 +
87 + with pytest.raises(SourceRegistrationError, match="cannot accept sources"):
88 + register_source_document(
89 + session,
90 + year_id=year.id,
91 + filename="blocked.xlsx",
92 + document_type="financial_statement",
93 + content=b"blocked",
94 + )
95 +
96 +
97 +@pytest.mark.parametrize(
98 + ("filename", "document_type", "content"),
99 + [
100 + ("../ledger.csv", "general_ledger", b"data"),
101 + ("..\\ledger.csv", "general_ledger", b"data"),
102 + ("ledger.exe", "general_ledger", b"data"),
103 + ("ledger.csv", "unknown", b"data"),
104 + ("ledger.csv", "general_ledger", b""),
105 + ],
106 +)
107 +def test_rejects_unsafe_or_unsupported_source_metadata(
108 + session, filename, document_type, content
109 +):
110 + year = make_year(session)
111 +
112 + with pytest.raises(SourceRegistrationError):
113 + register_source_document(
114 + session,
115 + year_id=year.id,
116 + filename=filename,
117 + document_type=document_type,
118 + content=content,
119 + )