@davoodmehraban89 / Aram / commits / 092c7e9
Fork 0

test(integration): combine Mapping coverage integrity service and regressions

davoodmehraban89 committed Oct 30, 2026 at 17:34 UTC 092c7e950939311d5fa5c8e0269c743d52021d31
1 file changed +189
backend/tests/test_mapping_coverage.py new
+189
@@ -0,0 +1,189 @@
1 +import pytest
2 +
3 +from aram_backend.mapping_coverage import inspect_mapping_coverage
4 +from aram_backend.mapping_service import MappingValidationError
5 +from aram_backend.models import (
6 + AccountVersion, Company, FiscalYear, MappingRule, NoteDefinition,
7 +)
8 +
9 +
10 +def year_with_accounts(session, code, codes=("001", "002", "003")):
11 + company = Company(code=code, name="Coverage test")
12 + year = FiscalYear(company=company, year=1404)
13 + session.add(year)
14 + session.flush()
15 + for account_code in codes:
16 + session.add(AccountVersion(year_id=year.id, account_code=account_code,
17 + account_name=account_code, version=1))
18 + notes = [NoteDefinition(year_id=year.id, note_number=str(i), title=str(i), version=1)
19 + for i in (1, 2)]
20 + session.add_all(notes)
21 + session.commit()
22 + return year, notes
23 +
24 +
25 +def rule(session, year, key, code, note_id, version=1):
26 + session.add(MappingRule(year_id=year.id, rule_key=key, account_code=code,
27 + target_note_id=note_id, mapping_type="direct", version=version))
28 +
29 +
30 +def test_empty_queue_without_mappings(session):
31 + year, _ = year_with_accounts(session, "COVER-A")
32 + coverage = inspect_mapping_coverage(session, year_id=year.id)
33 + assert coverage.total_accounts == 3
34 + assert coverage.mapped_accounts == ()
35 + assert coverage.unmapped_accounts == ("001", "002", "003")
36 + assert coverage.ambiguous_accounts == ()
37 +
38 +
39 +def test_mapped_and_ambiguous_queues(session):
40 + year, notes = year_with_accounts(session, "COVER-B")
41 + rule(session, year, "a", "001", notes[0].id)
42 + rule(session, year, "b", "002", notes[0].id)
43 + rule(session, year, "c", "002", notes[1].id)
44 + session.commit()
45 + coverage = inspect_mapping_coverage(session, year_id=year.id)
46 + assert coverage.mapped_accounts == ("001",)
47 + assert coverage.ambiguous_accounts == ("002",)
48 + assert coverage.unmapped_accounts == ("003",)
49 + assert coverage.total_accounts == 3
50 +
51 +
52 +def test_multiple_rules_same_note_are_not_ambiguous(session):
53 + year, notes = year_with_accounts(session, "COVER-C", ("001",))
54 + rule(session, year, "a", "001", notes[0].id)
55 + rule(session, year, "b", "001", notes[0].id)
56 + session.commit()
57 + assert inspect_mapping_coverage(session, year_id=year.id).mapped_accounts == ("001",)
58 +
59 +
60 +def test_latest_rule_version_supersedes_prior_version(session):
61 + year, notes = year_with_accounts(session, "COVER-D", ("001",))
62 + rule(session, year, "a", "001", notes[0].id, version=1)
63 + rule(session, year, "a", "001", notes[1].id, version=2)
64 + session.commit()
65 + coverage = inspect_mapping_coverage(session, year_id=year.id)
66 + assert coverage.mapped_accounts == ("001",)
67 + assert coverage.ambiguous_accounts == ()
68 +
69 +
70 +def test_latest_account_version_counted_once(session):
71 + year, _ = year_with_accounts(session, "COVER-E", ("001",))
72 + session.add(AccountVersion(year_id=year.id, account_code="001", account_name="Updated", version=2))
73 + session.commit()
74 + coverage = inspect_mapping_coverage(session, year_id=year.id)
75 + assert coverage.total_accounts == 1
76 + assert coverage.unmapped_accounts == ("001",)
77 +
78 +
79 +def test_orphan_rule_and_cross_year_isolation(session):
80 + year, notes = year_with_accounts(session, "COVER-F", ("001",))
81 + other, other_notes = year_with_accounts(session, "COVER-G", ("001",))
82 + rule(session, year, "orphan", "999", notes[0].id)
83 + rule(session, other, "other", "001", other_notes[0].id)
84 + session.commit()
85 + coverage = inspect_mapping_coverage(session, year_id=year.id)
86 + assert coverage.orphan_rule_keys == ("orphan",)
87 + assert coverage.unmapped_accounts == ("001",)
88 + assert coverage.mapped_accounts == ()
89 +
90 +
91 +@pytest.mark.parametrize("year_id", [0, -1, True, "1", None, 99999])
92 +def test_invalid_or_unknown_year_rejected(session, year_id):
93 + with pytest.raises(MappingValidationError):
94 + inspect_mapping_coverage(session, year_id=year_id)
95 +
96 +
97 +def test_coverage_does_not_change_rows(session):
98 + year, notes = year_with_accounts(session, "COVER-H")
99 + rule(session, year, "a", "001", notes[0].id)
100 + session.commit()
101 + before = session.query(MappingRule).count()
102 + inspect_mapping_coverage(session, year_id=year.id)
103 + assert session.query(MappingRule).count() == before
104 +
105 +
106 +def test_diagnostic_does_not_flush_pending_unrelated_writes(session):
107 + year, _ = year_with_accounts(session, "COVER-I", ("001",))
108 + pending = AccountVersion(year_id=year.id, account_code="PENDING", account_name="Pending", version=1)
109 + session.add(pending)
110 + coverage = inspect_mapping_coverage(session, year_id=year.id)
111 + assert pending in session.new
112 + assert pending.id is None
113 + assert coverage.total_accounts == 1
114 + assert coverage.unmapped_accounts == ("001",)
115 +
116 +
117 +def test_stale_same_year_note_is_not_counted_as_mapped(session):
118 + year, notes = year_with_accounts(session, "STALE-A", ("001",))
119 + rule(session, year, "old_note", "001", notes[0].id)
120 + session.add(NoteDefinition(
121 + year_id=year.id, note_number=notes[0].note_number,
122 + title="Current version", version=2,
123 + ))
124 + session.commit()
125 +
126 + result = inspect_mapping_coverage(session, year_id=year.id)
127 +
128 + assert result.mapped_accounts == ()
129 + assert result.unmapped_accounts == ("001",)
130 + assert result.invalid_target_rules == (("old_note", "001", "target_note_stale"),)
131 +
132 +
133 +def test_cross_year_note_target_is_not_counted_as_mapped(session):
134 + from sqlalchemy import update
135 +
136 + year, notes = year_with_accounts(session, "FOREIGN-A", ("001",))
137 + foreign, foreign_notes = year_with_accounts(session, "FOREIGN-B", ("001",))
138 + rule(session, year, "foreign", "001", notes[0].id)
139 + session.commit()
140 + # This is intentionally an ORM event bypass via SQLAlchemy Core SQL.
141 + session.execute(
142 + update(MappingRule)
143 + .where(MappingRule.year_id == year.id, MappingRule.rule_key == "foreign")
144 + .values(target_note_id=foreign_notes[0].id)
145 + )
146 + session.commit()
147 + session.expire_all()
148 +
149 + result = inspect_mapping_coverage(session, year_id=year.id)
150 +
151 + assert result.mapped_accounts == ()
152 + assert result.unmapped_accounts == ("001",)
153 + assert result.invalid_target_rules == (("foreign", "001", "target_note_wrong_year"),)
154 +
155 +
156 +def test_one_valid_and_one_stale_rule_does_not_count_as_clean_mapping(session):
157 + year, notes = year_with_accounts(session, "MIXED-A", ("001",))
158 + rule(session, year, "valid", "001", notes[1].id)
159 + rule(session, year, "stale", "001", notes[0].id)
160 + session.add(NoteDefinition(
161 + year_id=year.id, note_number=notes[0].note_number,
162 + title="Current version", version=2,
163 + ))
164 + session.commit()
165 +
166 + result = inspect_mapping_coverage(session, year_id=year.id)
167 +
168 + assert result.mapped_accounts == ()
169 + assert result.unmapped_accounts == ("001",)
170 + assert result.ambiguous_accounts == ()
171 + assert result.invalid_target_rules == (("stale", "001", "target_note_stale"),)
172 +
173 +
174 +def test_latest_note_version_target_is_clean_and_read_only(session):
175 + year, notes = year_with_accounts(session, "LATEST-A", ("001",))
176 + current = NoteDefinition(year_id=year.id, note_number=notes[0].note_number, title="Current", version=2)
177 + session.add(current)
178 + session.flush()
179 + rule(session, year, "ok", "001", current.id)
180 + session.commit()
181 +
182 + pending = AccountVersion(year_id=year.id, account_code="X", account_name="Pending", version=1)
183 + session.add(pending)
184 + result = inspect_mapping_coverage(session, year_id=year.id)
185 +
186 + assert result.mapped_accounts == ("001",)
187 + assert result.invalid_target_rules == ()
188 + assert pending in session.new
189 + assert pending.id is None