@davoodmehraban89 / Aram / commits / efcb8d8
Fork 0

test(mapping): cross-year preview, versioning, isolation and no-write checks

davoodmehraban89 committed Oct 23, 2026 at 14:42 UTC efcb8d8b33cb493c0af1d36db13f9c9614e7ab94
1 file changed +114
backend/tests/test_mapping_carry_forward.py new
+114
@@ -0,0 +1,114 @@
1 +import pytest
2 +from sqlalchemy import select
3 +
4 +from aram_backend.mapping_carry_forward import preview_mapping_carry_forward
5 +from aram_backend.mapping_service import MappingValidationError
6 +from aram_backend.models import (
7 + AccountVersion, Company, FiscalYear, FiscalYearStatus, MappingRule, NoteDefinition,
8 +)
9 +
10 +
11 +def make_year(session, company, year, accounts=("001",), note_numbers=("1",)):
12 + fiscal = FiscalYear(company=company, year=year)
13 + session.add(fiscal)
14 + session.flush()
15 + for code in accounts:
16 + session.add(AccountVersion(year_id=fiscal.id, account_code=code, account_name=code, version=1))
17 + notes = []
18 + for number in note_numbers:
19 + note = NoteDefinition(year_id=fiscal.id, note_number=number, title=number, version=1)
20 + session.add(note)
21 + notes.append(note)
22 + session.flush()
23 + return fiscal, notes
24 +
25 +
26 +def fixture_years(session, target_accounts=("001",), target_notes=("1",)):
27 + company = Company(code="CF-A", name="Carry forward")
28 + source, old_notes = make_year(session, company, 1403)
29 + target, new_notes = make_year(session, company, 1404, target_accounts, target_notes)
30 + session.add(MappingRule(year_id=source.id, rule_key="cash", account_code="001",
31 + target_note_id=old_notes[0].id, mapping_type="direct", version=1))
32 + session.commit()
33 + return source, target, old_notes, new_notes
34 +
35 +
36 +def test_valid_preview_is_read_only(session):
37 + source, target, _, notes = fixture_years(session)
38 + before = session.scalars(select(MappingRule)).all()
39 + result = preview_mapping_carry_forward(session, source_year_id=source.id, target_year_id=target.id)
40 + assert len(result.candidates) == 1
41 + assert result.candidates[0].target_note_id == notes[0].id
42 + assert result.candidates[0].source_note_number == "1"
43 + assert result.skipped == ()
44 + assert session.scalars(select(MappingRule)).all() == before
45 +
46 +
47 +@pytest.mark.parametrize("accounts,notes,reason", [
48 + ((), ("1",), "target_account_missing"),
49 + (("001",), ("2",), "target_note_missing"),
50 +])
51 +def test_missing_reference_skipped(session, accounts, notes, reason):
52 + source, target, _, _ = fixture_years(session, accounts, notes)
53 + result = preview_mapping_carry_forward(session, source_year_id=source.id, target_year_id=target.id)
54 + assert result.candidates == ()
55 + assert result.skipped == (("cash", reason),)
56 +
57 +
58 +def test_existing_target_rule_is_not_overwritten(session):
59 + source, target, _, notes = fixture_years(session)
60 + session.add(MappingRule(year_id=target.id, rule_key="cash", account_code="001",
61 + target_note_id=notes[0].id, mapping_type="manual", version=1))
62 + session.commit()
63 + result = preview_mapping_carry_forward(session, source_year_id=source.id, target_year_id=target.id)
64 + assert result.skipped == (("cash", "target_rule_exists"),)
65 +
66 +
67 +def test_latest_source_rule_and_target_note_versions(session):
68 + source, target, old_notes, new_notes = fixture_years(session)
69 + session.add(NoteDefinition(year_id=target.id, note_number="1", title="Updated", version=2))
70 + session.add(MappingRule(year_id=source.id, rule_key="cash", account_code="001",
71 + target_note_id=old_notes[0].id, mapping_type="manual", version=2))
72 + session.commit()
73 + result = preview_mapping_carry_forward(session, source_year_id=source.id, target_year_id=target.id)
74 + assert result.candidates[0].source_version == 2
75 + assert result.candidates[0].mapping_type == "manual"
76 + assert result.candidates[0].target_note_id != new_notes[0].id
77 +
78 +
79 +def test_protected_target_rejected(session):
80 + source, target, _, _ = fixture_years(session)
81 + target.status = FiscalYearStatus.APPROVED
82 + session.commit()
83 + with pytest.raises(MappingValidationError, match="Draft"):
84 + preview_mapping_carry_forward(session, source_year_id=source.id, target_year_id=target.id)
85 +
86 +
87 +def test_different_company_rejected(session):
88 + source, target, _, _ = fixture_years(session)
89 + other = Company(code="CF-B", name="Other")
90 + other_year, _ = make_year(session, other, 1405)
91 + session.commit()
92 + with pytest.raises(MappingValidationError, match="same company"):
93 + preview_mapping_carry_forward(session, source_year_id=source.id, target_year_id=other_year.id)
94 +
95 +
96 +@pytest.mark.parametrize("source_id,target_id", [(0, 1), (True, 1), (1, False), ("1", 2), (1, 1), (99999, 100000)])
97 +def test_invalid_year_inputs_rejected(session, source_id, target_id):
98 + with pytest.raises(MappingValidationError):
99 + preview_mapping_carry_forward(session, source_year_id=source_id, target_year_id=target_id)
100 +
101 +
102 +def test_reverse_year_order_rejected(session):
103 + source, target, _, _ = fixture_years(session)
104 + with pytest.raises(MappingValidationError, match="later"):
105 + preview_mapping_carry_forward(session, source_year_id=target.id, target_year_id=source.id)
106 +
107 +
108 +def test_preview_does_not_autoflush_unrelated_changes(session):
109 + source, target, _, _ = fixture_years(session)
110 + pending = AccountVersion(year_id=target.id, account_code="NEW", account_name="Pending", version=1)
111 + session.add(pending)
112 + preview_mapping_carry_forward(session, source_year_id=source.id, target_year_id=target.id)
113 + assert pending in session.new
114 + assert pending.id is None