@davoodmehraban89 / Aram / commits / f832c6d
Fork 0

test(mapping): expose stale and cross-year note coverage false positives

davoodmehraban89 committed Oct 30, 2026 at 17:26 UTC f832c6d085b31190eacfaebbe92c0c806f3d3f94
1 file changed +75
backend/tests/test_mapping_coverage.py
+75
@@ -112,3 +112,78 @@ def test_diagnostic_does_not_flush_pending_unrelated_writes(session):
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