Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 15:45 UTC
test(mapping): cover ambiguous, orphan, version and year-isolation queues
davoodmehraban89 committed
Oct 23, 2026 at 02:46 UTC
758f6a8ba30bb23bfdc4c8bd87b054b81eca72ad
1 file changed
+103
backend/tests/test_mapping_coverage.py
new
+103
@@ -0,0 +1,103 @@
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