This commit is contained in:
gsinghpal
2026-05-16 13:18:52 -04:00
parent 191a9c82be
commit 9ebf89bde2
1080 changed files with 0 additions and 1197 deletions

View File

@@ -0,0 +1,25 @@
from . import test_memo_tokenizer
from . import test_exchange_diff
from . import test_matching_strategies
from . import test_ai_suggestion_lifecycle
from . import test_precedent_lookup
from . import test_pattern_extraction
from . import test_confidence_scoring
from . import test_reconcile_engine_unit
from . import test_reconcile_engine_property
from . import test_factories
from . import test_reconcile_engine_integration
from . import test_bank_rec_prompt
from . import test_bank_rec_adapter
from . import test_bank_rec_tools
from . import test_legacy_tools_refactor
from . import test_mv_unreconciled
from . import test_cron_methods
from . import test_controller
from . import test_auto_reconcile_wizard
from . import test_bulk_reconcile_wizard
from . import test_migration_round_trip
from . import test_coexistence
from . import test_bank_rec_tours
from . import test_performance_benchmarks
from . import test_local_llm_compat

View File

@@ -0,0 +1,205 @@
"""Test data factories for fusion_accounting_bank_rec.
Provides recordset builders for use across all test files. Sane defaults
let tests be readable: `make_bank_line(env, amount=100, partner=p)` instead
of 30 lines of recordset setup.
These factories work against the real Odoo registry — they exercise the
same code paths as production. Each factory is idempotent in the sense
that calling it multiple times returns separate records.
"""
from datetime import date, timedelta
from odoo import fields
# ============================================================
# Bank journal + statements
# ============================================================
def make_bank_journal(env, *, name='Test Bank', code=None):
"""Create a bank journal. `code` defaults to first 5 chars of `name`."""
code = code or name[:5].upper().replace(' ', '')
return env['account.journal'].create({
'name': name,
'type': 'bank',
'code': code,
})
def make_bank_statement(env, *, journal=None, name='Test Statement', date_=None):
"""Create a bank statement.
NOTE: in V19 Community, ``account.bank.statement.journal_id`` is a
read-only computed field derived from ``line_ids.journal_id`` — direct
writes are silently dropped. Enterprise's ``account_accountant`` used to
override this to make it writable; without Enterprise we have to derive
the journal from a line. We attach a single token line at create time
(later removed/replaced by the test) to bootstrap the journal.
"""
journal = journal or make_bank_journal(env)
return env['account.bank.statement'].create({
'name': name,
'date': date_ or date.today(),
'line_ids': [(0, 0, {
'journal_id': journal.id,
'date': date_ or date.today(),
'payment_ref': 'Statement bootstrap line',
'amount': 0.0,
})],
})
def make_bank_line(env, *, journal=None, statement=None, amount=100.00,
partner=None, memo='Test line', date_=None):
"""Create a bank statement line. Creates a journal (and optionally a
statement) if not provided.
In V19 Community, lines can exist standalone — a statement is not
required. We create one only if the test explicitly passes ``statement=``.
"""
if statement and not journal:
journal = statement.journal_id
if not journal:
journal = make_bank_journal(env)
vals = {
'journal_id': journal.id,
'date': date_ or date.today(),
'payment_ref': memo,
'amount': amount,
'partner_id': partner.id if partner else False,
}
if statement:
vals['statement_id'] = statement.id
return env['account.bank.statement.line'].create(vals)
# ============================================================
# Invoices + journal items
# ============================================================
def _ensure_test_product(env):
"""Get or create a service product suitable for invoice lines."""
product = env['product.product'].search([('type', '=', 'service')], limit=1)
if not product:
product = env['product.product'].create({
'name': 'Fusion Test Service',
'type': 'service',
})
return product
def make_invoice(env, *, partner, amount=100.00, date_=None, currency=None,
product=None, posted=True):
"""Create a customer invoice (out_invoice). Posted by default."""
product = product or _ensure_test_product(env)
vals = {
'move_type': 'out_invoice',
'partner_id': partner.id,
'invoice_date': date_ or date.today(),
'invoice_line_ids': [(0, 0, {
'product_id': product.id,
'name': 'Test invoice line',
'quantity': 1,
'price_unit': amount,
})],
}
if currency:
vals['currency_id'] = currency.id
move = env['account.move'].create(vals)
if posted:
move.action_post()
return move
def make_vendor_bill(env, *, partner, amount=100.00, date_=None, currency=None,
product=None, posted=True):
"""Create a vendor bill (in_invoice). Posted by default."""
product = product or _ensure_test_product(env)
vals = {
'move_type': 'in_invoice',
'partner_id': partner.id,
'invoice_date': date_ or date.today(),
'invoice_line_ids': [(0, 0, {
'product_id': product.id,
'name': 'Test bill line',
'quantity': 1,
'price_unit': amount,
})],
}
if currency:
vals['currency_id'] = currency.id
move = env['account.move'].create(vals)
if posted:
move.action_post()
return move
# ============================================================
# Suggestions + patterns + precedents (fusion-specific)
# ============================================================
def make_suggestion(env, *, statement_line, candidate_move_lines=None,
confidence=0.92, rank=1, reasoning='Test suggestion',
state='pending'):
"""Create a fusion.reconcile.suggestion against a bank line."""
candidate_ids = candidate_move_lines.ids if candidate_move_lines else []
return env['fusion.reconcile.suggestion'].create({
'company_id': env.company.id,
'statement_line_id': statement_line.id,
'proposed_move_line_ids': [(6, 0, candidate_ids)],
'confidence': confidence,
'rank': rank,
'reasoning': reasoning,
'state': state,
})
def make_pattern(env, *, partner, reconcile_count=10, pref_strategy='exact_amount',
typical_cadence_days=14.0, common_memo_tokens='RBC,ETF'):
"""Create a fusion.reconcile.pattern for a partner."""
return env['fusion.reconcile.pattern'].create({
'company_id': env.company.id,
'partner_id': partner.id,
'reconcile_count': reconcile_count,
'pref_strategy': pref_strategy,
'typical_cadence_days': typical_cadence_days,
'common_memo_tokens': common_memo_tokens,
})
def make_precedent(env, *, partner, amount=1847.50, days_ago=14,
memo_tokens='RBC,ETF,REF', count=1, source='manual'):
"""Create a fusion.reconcile.precedent."""
return env['fusion.reconcile.precedent'].create({
'company_id': env.company.id,
'partner_id': partner.id,
'amount': amount,
'currency_id': env.company.currency_id.id,
'date': date.today() - timedelta(days=days_ago),
'memo_tokens': memo_tokens,
'matched_move_line_count': count,
'reconciled_at': fields.Datetime.now(),
'source': source,
})
# ============================================================
# Convenience composite — bank line + matching invoice ready to reconcile
# ============================================================
def make_reconcileable_pair(env, *, amount=100.00, partner=None, date_=None):
"""Create a bank line + a customer invoice with the same partner+amount.
Returns (bank_line, invoice_recv_lines) ready to pass to engine.reconcile_one().
Returns:
(bank_line, invoice_receivable_lines) tuple
"""
if not partner:
partner = env['res.partner'].create({'name': 'Reconcile Test Partner'})
invoice = make_invoice(env, partner=partner, amount=amount, date_=date_)
bank_line = make_bank_line(env, amount=amount, partner=partner, date_=date_)
recv_lines = invoice.line_ids.filtered(
lambda l: l.account_id.account_type == 'asset_receivable')
return (bank_line, recv_lines)

View File

@@ -0,0 +1,86 @@
from odoo.tests.common import TransactionCase, tagged
@tagged('post_install', '-at_install')
class TestSuggestionLifecycle(TransactionCase):
"""The fusion.reconcile.suggestion state machine + computed band."""
def setUp(self):
super().setUp()
journal = self.env['account.journal'].create({
'name': 'Test Bank Suggestion',
'type': 'bank',
'code': 'TBSG',
})
statement = self.env['account.bank.statement'].create({
'name': 'Test Statement',
'journal_id': journal.id,
})
self.line = self.env['account.bank.statement.line'].create({
'statement_id': statement.id,
'journal_id': journal.id,
'date': '2026-04-19',
'payment_ref': 'Test for suggestion',
'amount': 100.00,
})
def _make_suggestion(self, confidence=0.92, **vals):
defaults = {
'company_id': self.env.company.id,
'statement_line_id': self.line.id,
'confidence': confidence,
'rank': 1,
'reasoning': 'Test',
}
defaults.update(vals)
return self.env['fusion.reconcile.suggestion'].create(defaults)
def test_compute_band_high(self):
sug = self._make_suggestion(confidence=0.96)
self.assertEqual(sug.confidence_band, 'high')
def test_compute_band_medium(self):
sug = self._make_suggestion(confidence=0.75)
self.assertEqual(sug.confidence_band, 'medium')
def test_compute_band_low(self):
sug = self._make_suggestion(confidence=0.55)
self.assertEqual(sug.confidence_band, 'low')
def test_compute_band_none(self):
sug = self._make_suggestion(confidence=0.30)
self.assertEqual(sug.confidence_band, 'none')
def test_default_state_is_pending(self):
sug = self._make_suggestion()
self.assertEqual(sug.state, 'pending')
def test_state_transition_to_accepted(self):
sug = self._make_suggestion()
sug.write({
'state': 'accepted',
'accepted_at': '2026-04-19 12:00:00',
'accepted_by': self.env.user.id,
})
self.assertEqual(sug.state, 'accepted')
self.assertTrue(sug.accepted_at)
self.assertEqual(sug.accepted_by, self.env.user)
def test_state_transition_to_rejected_with_reason(self):
sug = self._make_suggestion()
sug.write({
'state': 'rejected',
'rejected_at': '2026-04-19 12:05:00',
'rejected_reason': 'wrong_invoice',
})
self.assertEqual(sug.state, 'rejected')
self.assertEqual(sug.rejected_reason, 'wrong_invoice')
def test_state_transition_to_superseded(self):
sug = self._make_suggestion()
sug.write({'state': 'superseded'})
self.assertEqual(sug.state, 'superseded')
def test_currency_id_relates_to_line(self):
sug = self._make_suggestion()
self.assertEqual(sug.currency_id, self.line.currency_id)

View File

@@ -0,0 +1,50 @@
"""Tests for fusion.auto.reconcile.wizard."""
from odoo.tests.common import TransactionCase, tagged
from . import _factories as f
@tagged('post_install', '-at_install')
class TestAutoReconcileWizard(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Auto Wizard Partner'})
self.journal = f.make_bank_journal(self.env, name='Auto Bank', code='AUBK')
def test_wizard_runs_and_reconciles_matchable_lines(self):
statement = f.make_bank_statement(self.env, journal=self.journal)
for amount in [100.00, 200.00]:
f.make_invoice(self.env, partner=self.partner, amount=amount)
f.make_bank_line(
self.env, statement=statement, amount=amount, partner=self.partner)
wizard = self.env['fusion.auto.reconcile.wizard'].create({
'journal_id': self.journal.id,
'strategy': 'auto',
'only_with_partner': True,
})
wizard.action_run()
self.assertEqual(wizard.state, 'done')
self.assertGreaterEqual(wizard.reconciled_count, 2)
def test_wizard_filters_by_date_range(self):
wizard = self.env['fusion.auto.reconcile.wizard'].create({
'journal_id': self.journal.id,
'date_from': '2099-01-01',
'date_to': '2099-12-31',
'strategy': 'auto',
})
wizard.action_run()
self.assertEqual(wizard.reconciled_count, 0)
def test_wizard_skips_when_only_with_partner_excludes_orphans(self):
statement = f.make_bank_statement(self.env, journal=self.journal)
f.make_bank_line(self.env, statement=statement, amount=999, partner=None)
wizard = self.env['fusion.auto.reconcile.wizard'].create({
'journal_id': self.journal.id,
'strategy': 'auto',
'only_with_partner': True,
})
wizard.action_run()
self.assertEqual(wizard.reconciled_count, 0)

View File

@@ -0,0 +1,81 @@
"""Tests for BankRecAdapter's fusion paths."""
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_ai.services.data_adapters.bank_rec import BankRecAdapter
from . import _factories as f
@tagged('post_install', '-at_install')
class TestBankRecAdapter(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Adapter Test Partner'})
self.adapter = BankRecAdapter(self.env)
def test_list_unreconciled_via_fusion_returns_base_fields(self):
bank_line = f.make_bank_line(
self.env, amount=100.00, partner=self.partner, memo='Adapter base test')
result = self.adapter.list_unreconciled_via_fusion(
company_id=self.env.company.id, limit=50)
ours = [r for r in result if r['id'] == bank_line.id]
self.assertEqual(len(ours), 1)
row = ours[0]
for f_name in ['id', 'date', 'payment_ref', 'amount', 'partner_id', 'journal_id']:
self.assertIn(f_name, row)
self.assertIn('fusion_top_suggestion_id', row)
self.assertIn('fusion_confidence_band', row)
self.assertIn('attachment_count', row)
def test_list_unreconciled_via_community_omits_fusion_fields(self):
bank_line = f.make_bank_line(self.env, amount=200.00, partner=self.partner)
result = self.adapter.list_unreconciled_via_community(
company_id=self.env.company.id, limit=50)
ours = [r for r in result if r['id'] == bank_line.id]
self.assertEqual(len(ours), 1)
self.assertNotIn('fusion_top_suggestion_id', ours[0])
def test_suggest_matches_via_fusion_returns_dict(self):
partner = self.env['res.partner'].create({'name': 'Suggest Adapter'})
invoice = f.make_invoice(self.env, partner=partner, amount=350.00)
bank_line = f.make_bank_line(self.env, amount=350.00, partner=partner)
result = self.adapter.suggest_matches_via_fusion(
statement_line_ids=[bank_line.id], limit_per_line=3)
self.assertIsInstance(result, dict)
self.assertIn(bank_line.id, result)
self.assertGreater(len(result[bank_line.id]), 0)
def test_suggest_matches_via_community_returns_empty(self):
bank_line = f.make_bank_line(self.env, amount=100.00, partner=self.partner)
result = self.adapter.suggest_matches_via_community(
statement_line_ids=[bank_line.id])
self.assertEqual(result, {})
def test_accept_suggestion_via_fusion(self):
partner = self.env['res.partner'].create({'name': 'Accept Adapter'})
invoice = f.make_invoice(self.env, partner=partner, amount=425.00)
recv_lines = invoice.line_ids.filtered(
lambda l: l.account_id.account_type == 'asset_receivable')
bank_line = f.make_bank_line(self.env, amount=425.00, partner=partner)
sug = f.make_suggestion(
self.env, statement_line=bank_line,
candidate_move_lines=recv_lines, confidence=0.95)
result = self.adapter.accept_suggestion_via_fusion(suggestion_id=sug.id)
self.assertIn('partial_ids', result)
self.assertGreater(len(result['partial_ids']), 0)
def test_accept_suggestion_via_community_raises(self):
with self.assertRaises(NotImplementedError):
self.adapter.accept_suggestion_via_community(suggestion_id=1)
def test_unreconcile_via_fusion(self):
partner = self.env['res.partner'].create({'name': 'Unrec Adapter'})
bank_line, recv_lines = f.make_reconcileable_pair(
self.env, amount=275.00, partner=partner)
rec_result = self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
partial_ids = rec_result['partial_ids']
result = self.adapter.unreconcile_via_fusion(
partial_reconcile_ids=partial_ids)
self.assertIn('unreconciled_line_ids', result)
self.assertGreater(len(result['unreconciled_line_ids']), 0)

View File

@@ -0,0 +1,92 @@
"""Smoke tests for bank_rec_prompt module."""
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_ai.services.prompts.bank_rec_prompt import (
SYSTEM_PROMPT,
build_prompt,
)
from odoo.addons.fusion_accounting_bank_rec.services.confidence_scoring import (
ScoredCandidate,
)
from . import _factories as f
@tagged('post_install', '-at_install')
class TestBankRecPrompt(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Prompt Test Partner'})
self.bank_line = f.make_bank_line(
self.env,
amount=1847.50,
partner=self.partner,
memo='RBC ETF DEP REF 4831',
)
self.scored = [
ScoredCandidate(
candidate_id=101,
confidence=0.92,
reasoning='Exact amount match',
score_amount_match=1.0,
score_partner_pattern=0.5,
score_precedent_similarity=0.85,
),
ScoredCandidate(
candidate_id=102,
confidence=0.71,
reasoning='Close amount',
score_amount_match=0.95,
score_partner_pattern=0.5,
score_precedent_similarity=0.6,
),
]
def test_system_prompt_requires_json_output(self):
self.assertIn('JSON', SYSTEM_PROMPT)
self.assertIn('"ranked"', SYSTEM_PROMPT)
def test_build_prompt_returns_tuple(self):
result = build_prompt(self.bank_line, self.scored)
self.assertEqual(len(result), 2)
system, user = result
self.assertIsInstance(system, str)
self.assertIsInstance(user, str)
def test_user_prompt_includes_bank_line_details(self):
_, user = build_prompt(self.bank_line, self.scored)
self.assertIn('1847.5', user)
self.assertIn('RBC ETF DEP REF 4831', user)
self.assertIn('Prompt Test Partner', user)
def test_user_prompt_includes_all_candidates(self):
_, user = build_prompt(self.bank_line, self.scored)
self.assertIn('candidate_id=101', user)
self.assertIn('candidate_id=102', user)
def test_user_prompt_omits_pattern_section_when_none(self):
_, user = build_prompt(self.bank_line, self.scored, pattern=None)
self.assertNotIn('PARTNER PATTERN', user)
def test_user_prompt_includes_pattern_section_when_provided(self):
pattern = f.make_pattern(self.env, partner=self.partner, reconcile_count=15)
_, user = build_prompt(self.bank_line, self.scored, pattern=pattern)
self.assertIn('PARTNER PATTERN', user)
self.assertIn('15', user)
def test_user_prompt_includes_precedents_when_provided(self):
from odoo.addons.fusion_accounting_bank_rec.services.precedent_lookup import (
PrecedentMatch,
)
precedents = [
PrecedentMatch(
precedent_id=1,
amount=1847.50,
memo_tokens='RBC,ETF',
matched_move_line_count=1,
similarity_score=0.95,
),
]
_, user = build_prompt(self.bank_line, self.scored, precedents=precedents)
self.assertIn('RECENT PRECEDENTS', user)
self.assertIn('0.95', user)

View File

@@ -0,0 +1,84 @@
"""Smoke tests for the 5 new fusion bank-rec AI tools."""
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_ai.services.tools import bank_reconciliation as tools
from . import _factories as f
@tagged('post_install', '-at_install')
class TestFusionBankRecTools(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Tools Test Partner'})
def test_fusion_suggest_matches_returns_suggestions(self):
invoice = f.make_invoice(self.env, partner=self.partner, amount=550.00)
bank_line = f.make_bank_line(
self.env, amount=550.00, partner=self.partner, memo='Tool test')
result = tools.fusion_suggest_matches(self.env, {
'statement_line_ids': [bank_line.id],
'limit_per_line': 3,
})
self.assertIn('suggestions', result)
self.assertIn('count', result)
self.assertGreater(result['count'], 0)
def test_fusion_accept_suggestion_reconciles(self):
invoice = f.make_invoice(self.env, partner=self.partner, amount=625.00)
recv_lines = invoice.line_ids.filtered(
lambda l: l.account_id.account_type == 'asset_receivable')
bank_line = f.make_bank_line(self.env, amount=625.00, partner=self.partner)
sug = f.make_suggestion(
self.env, statement_line=bank_line,
candidate_move_lines=recv_lines, confidence=0.94)
result = tools.fusion_accept_suggestion(self.env, {'suggestion_id': sug.id})
self.assertEqual(result['status'], 'accepted')
self.assertGreater(len(result['partial_ids']), 0)
def test_fusion_reconcile_bank_line(self):
bank_line, recv_lines = f.make_reconcileable_pair(
self.env, amount=375.00, partner=self.partner)
result = tools.fusion_reconcile_bank_line(self.env, {
'statement_line_id': bank_line.id,
'against_move_line_ids': recv_lines.ids,
})
self.assertEqual(result['status'], 'reconciled')
self.assertTrue(result['is_reconciled'])
def test_fusion_unreconcile(self):
bank_line, recv_lines = f.make_reconcileable_pair(
self.env, amount=275.00, partner=self.partner)
rec = self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
partial_ids = rec['partial_ids']
result = tools.fusion_unreconcile(self.env, {
'partial_reconcile_ids': partial_ids,
})
self.assertEqual(result['status'], 'unreconciled')
self.assertGreater(result['count'], 0)
def test_fusion_get_pending_suggestions(self):
bank_line = f.make_bank_line(self.env, amount=100.00, partner=self.partner)
sug = f.make_suggestion(
self.env, statement_line=bank_line,
candidate_move_lines=self.env['account.move.line'],
confidence=0.88, state='pending')
result = tools.fusion_get_pending_suggestions(self.env, {})
self.assertIn('count', result)
self.assertGreater(result['count'], 0)
ids = [s['id'] for s in result['suggestions']]
self.assertIn(sug.id, ids)
def test_fusion_get_pending_suggestions_filters_by_min_confidence(self):
bank_line = f.make_bank_line(self.env, amount=100.00, partner=self.partner)
# One low-confidence suggestion
f.make_suggestion(self.env, statement_line=bank_line,
confidence=0.30, state='pending')
# One high-confidence
high = f.make_suggestion(self.env, statement_line=bank_line,
confidence=0.95, state='pending')
result = tools.fusion_get_pending_suggestions(
self.env, {'min_confidence': 0.80})
ids = [s['id'] for s in result['suggestions']]
self.assertIn(high.id, ids)

View File

@@ -0,0 +1,42 @@
"""Python wrappers that run the OWL tours via HttpCase.start_tour.
Tours require an HTTP server + headless browser. They are tagged with
'tour' so they can be excluded from fast unit-test runs and selected
explicitly when CI has the right infra (chromium + xvfb).
"""
from odoo.tests.common import HttpCase, tagged
@tagged('post_install', '-at_install', 'tour')
class TestBankRecTours(HttpCase):
def test_smoke_tour(self):
# Just verify the smoke tour runs without crashing
self.start_tour("/odoo", "fusion_bank_rec_smoke", login="admin")
def test_select_line_tour(self):
# Need a bank line to select — create one
partner = self.env['res.partner'].create({'name': 'Tour Partner'})
journal = self.env['account.journal'].create({
'name': 'Tour Bank', 'type': 'bank', 'code': 'TOURB',
})
statement = self.env['account.bank.statement'].create({
'name': 'Tour Stmt', 'journal_id': journal.id,
})
self.env['account.bank.statement.line'].create({
'statement_id': statement.id, 'journal_id': journal.id,
'date': '2026-04-19', 'payment_ref': 'Tour line',
'amount': 100, 'partner_id': partner.id,
})
self.start_tour("/odoo", "fusion_bank_rec_select_line", login="admin")
def test_accept_suggestion_tour(self):
# Skip if too slow / dataset issues — tour itself is the smoke
self.skipTest("Tour 3 requires AI provider config; skipping in CI smoke")
def test_auto_reconcile_wizard_tour(self):
self.start_tour("/odoo", "fusion_bank_rec_auto_reconcile_wizard", login="admin")
def test_load_more_tour(self):
self.start_tour("/odoo", "fusion_bank_rec_load_more", login="admin")

View File

@@ -0,0 +1,42 @@
"""Tests for fusion.bulk.reconcile.wizard."""
from odoo.tests.common import TransactionCase, tagged
from . import _factories as f
@tagged('post_install', '-at_install')
class TestBulkReconcileWizard(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Bulk Wizard Partner'})
self.journal = f.make_bank_journal(self.env, name='Bulk Bank', code='BLKBK')
self.statement = f.make_bank_statement(self.env, journal=self.journal)
def test_wizard_default_picks_active_ids(self):
line1 = f.make_bank_line(
self.env, statement=self.statement, amount=100, partner=self.partner)
line2 = f.make_bank_line(
self.env, statement=self.statement, amount=200, partner=self.partner)
wizard = self.env['fusion.bulk.reconcile.wizard'].with_context(
active_model='account.bank.statement.line',
active_ids=[line1.id, line2.id],
).create({})
self.assertEqual(set(wizard.statement_line_ids.ids), {line1.id, line2.id})
self.assertEqual(wizard.selected_count, 2)
def test_wizard_auto_mode_runs_engine_batch(self):
line_ids = []
for amount in [110.00, 220.00]:
f.make_invoice(self.env, partner=self.partner, amount=amount)
line = f.make_bank_line(
self.env, statement=self.statement, amount=amount, partner=self.partner)
line_ids.append(line.id)
wizard = self.env['fusion.bulk.reconcile.wizard'].create({
'statement_line_ids': [(6, 0, line_ids)],
'mode': 'auto',
'strategy': 'auto',
})
wizard.action_run()
self.assertEqual(wizard.state, 'done')
self.assertGreaterEqual(wizard.reconciled_count, 2)

View File

@@ -0,0 +1,86 @@
"""Coexistence tests: fusion_accounting_bank_rec menus only visible
when Enterprise's account_accountant is absent.
Strategy: mock the install state by toggling the group's user list directly,
then verify the recompute method aligns it with module presence."""
from unittest.mock import patch
from odoo.tests.common import TransactionCase, tagged
@tagged('post_install', '-at_install')
class TestCoexistence(TransactionCase):
def setUp(self):
super().setUp()
self.group = self.env.ref(
'fusion_accounting_core.group_fusion_show_when_enterprise_absent')
def _account_accountant_installed(self):
return bool(self.env['ir.module.module'].sudo().search([
('name', '=', 'account_accountant'),
('state', '=', 'installed'),
]))
def test_group_exists(self):
self.assertTrue(self.group, "Coexistence group must exist")
def test_recompute_when_enterprise_present(self):
"""When account_accountant is installed, group should be empty."""
if not self._account_accountant_installed():
self.skipTest(
"Local DB doesn't have account_accountant installed; "
"this test only meaningful in Enterprise-present scenario"
)
self.env['res.users']._fusion_recompute_coexistence_group()
self.assertEqual(
len(self.group.user_ids), 0,
"Coexistence group should be empty when Enterprise is installed",
)
def test_recompute_when_enterprise_absent(self):
"""When account_accountant is uninstalled, all internal users get the group."""
if self._account_accountant_installed():
# Simulate by mocking the enterprise-installed check.
with patch.object(
type(self.env['ir.module.module']),
'_fusion_is_enterprise_accounting_installed',
return_value=False,
):
self.env['res.users']._fusion_recompute_coexistence_group()
internal_users = self.env['res.users'].search([
('share', '=', False),
])
self.assertGreater(
len(self.group.user_ids & internal_users), 0,
"Coexistence group should contain internal users when "
"Enterprise is absent",
)
else:
self.env['res.users']._fusion_recompute_coexistence_group()
internal = self.env['res.users'].search([('share', '=', False)])
self.assertGreater(len(self.group.user_ids & internal), 0)
def test_menu_has_coexistence_group(self):
"""The fusion bank-rec root menu must have the coexistence group attached."""
menu = self.env.ref(
'fusion_accounting_bank_rec.menu_fusion_bank_rec_root',
raise_if_not_found=False,
)
if not menu:
self.skipTest("Menu not yet loaded — Task 42 must run first")
# Odoo 19 renamed ir.ui.menu.groups_id -> group_ids; tolerate either.
groups_field = getattr(menu, 'group_ids', None) or menu.groups_id
self.assertIn(
self.group, groups_field,
"Menu must require the coexistence group",
)
def test_engine_works_regardless_of_coexistence(self):
"""The reconcile engine must work even when Enterprise is installed
(it's the AI tools/menu that gate; the engine is always available)."""
self.assertIn(
'fusion.reconcile.engine', self.env.registry,
"Engine must always be available when fusion_accounting_bank_rec "
"is installed",
)

View File

@@ -0,0 +1,102 @@
from datetime import date, timedelta, datetime
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_bank_rec.services.confidence_scoring import (
score_candidates, ScoredCandidate,
)
from odoo.addons.fusion_accounting_bank_rec.services.matching_strategies import Candidate
@tagged('post_install', '-at_install')
class TestConfidenceScoring(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Scoring Test Partner'})
self.company = self.env.company
self.currency = self.env.ref('base.CAD')
self.journal = self.env['account.journal'].create({
'name': 'Test Bank Scoring',
'type': 'bank',
'code': 'TBSC',
})
statement = self.env['account.bank.statement'].create({
'name': 'Test Statement',
'journal_id': self.journal.id,
})
self.line = self.env['account.bank.statement.line'].create({
'statement_id': statement.id,
'journal_id': self.journal.id,
'date': date.today(),
'payment_ref': 'RBC ETF DEP REF 4831',
'amount': 1847.50,
'partner_id': self.partner.id,
})
def _candidate(self, id_, amount, age_days=10):
return Candidate(id=id_, amount=amount, partner_id=self.partner.id, age_days=age_days)
def test_returns_empty_when_no_candidates(self):
result = score_candidates(self.env, statement_line=self.line, candidates=[], k=5)
self.assertEqual(result, [])
def test_returns_empty_when_no_statement_line(self):
result = score_candidates(self.env, statement_line=None,
candidates=[self._candidate(1, 100)], k=5)
self.assertEqual(result, [])
def test_amount_exact_dominates(self):
candidates = [
self._candidate(1, 1847.50),
self._candidate(2, 1800.00),
]
result = score_candidates(self.env, statement_line=self.line, candidates=candidates, k=5,
use_ai=False)
self.assertEqual(len(result), 2)
self.assertEqual(result[0].candidate_id, 1)
self.assertGreater(result[0].confidence, result[1].confidence)
self.assertGreater(result[0].score_amount_match, 0.99)
def test_returns_top_k(self):
candidates = [self._candidate(i, 1847.50 - i) for i in range(10)]
result = score_candidates(self.env, statement_line=self.line, candidates=candidates, k=3,
use_ai=False)
self.assertEqual(len(result), 3)
def test_no_ai_provider_returns_statistical_only(self):
"""When no AI provider config, score_ai_rerank stays at 0.0."""
self.env['ir.config_parameter'].sudo().search([
('key', 'in', ['fusion_accounting.provider.bank_rec_suggest',
'fusion_accounting.provider.default'])
]).unlink()
candidates = [self._candidate(1, 1847.50)]
result = score_candidates(self.env, statement_line=self.line, candidates=candidates, k=5,
use_ai=True)
self.assertEqual(result[0].score_ai_rerank, 0.0)
def test_use_ai_false_skips_ai_rerank(self):
candidates = [self._candidate(1, 1847.50)]
result = score_candidates(self.env, statement_line=self.line, candidates=candidates, k=5,
use_ai=False)
self.assertEqual(result[0].score_ai_rerank, 0.0)
def test_pattern_match_boosts_confidence(self):
"""When the partner has a matching pattern, confidence is higher than no-pattern case."""
self.env['fusion.reconcile.pattern'].create({
'company_id': self.company.id,
'partner_id': self.partner.id,
'reconcile_count': 10,
'pref_strategy': 'exact_amount',
})
candidates = [self._candidate(1, 1847.50)]
with_pattern = score_candidates(self.env, statement_line=self.line,
candidates=candidates, k=5, use_ai=False)
other_partner = self.env['res.partner'].create({'name': 'No Pattern Partner'})
self.line.write({'partner_id': other_partner.id})
other_candidates = [Candidate(id=1, amount=1847.50, partner_id=other_partner.id, age_days=10)]
without_pattern = score_candidates(self.env, statement_line=self.line,
candidates=other_candidates, k=5, use_ai=False)
self.assertGreater(with_pattern[0].score_partner_pattern,
without_pattern[0].score_partner_pattern - 0.001)

View File

@@ -0,0 +1,333 @@
"""Tests for the fusion bank-rec HTTP controller (Task 26).
Uses ``HttpCase`` so we exercise the full Werkzeug stack -- the JSON-RPC
dispatcher, auth check, and route resolution all run for real. Tests
authenticate as a Fusion Accounting administrator (the realistic role
for a user driving the bank-rec widget); a separate test confirms the
``auth='user'`` decorator rejects anonymous traffic.
"""
import json
from odoo.tests.common import HttpCase, new_test_user, tagged
from . import _factories as f
@tagged('post_install', '-at_install')
class TestBankRecController(HttpCase):
"""End-to-end coverage of the 10 JSON-RPC endpoints."""
USER_LOGIN = 'ctrl_test_user'
USER_PASSWORD = 'ctrl_test_user'
def setUp(self):
super().setUp()
# group_account_user grants accounting write perms AND auto-implies
# fusion_accounting_user via the security XML's implied_ids hook;
# group_fusion_accounting_admin grants full CRUD on the fusion
# suggestion / precedent / pattern models the engine writes to.
self.user = new_test_user(
self.env,
login=self.USER_LOGIN,
password=self.USER_PASSWORD,
groups=(
'base.group_user,'
'account.group_account_user,'
'fusion_accounting_core.group_fusion_accounting_admin'
),
)
self.partner = self.env['res.partner'].create(
{'name': 'Controller Test Partner'})
self.journal = f.make_bank_journal(
self.env, name='Ctrl Bank', code='CBNK')
# ------------------------------------------------------------------
# helpers
# ------------------------------------------------------------------
def _jsonrpc(self, endpoint, params, *, authenticate=True):
"""POST a JSON-RPC envelope to ``/fusion/bank_rec/<endpoint>``.
Returns the ``result`` dict on success and fails the test if
the body has an ``error`` key (so endpoint test failures show
the actual server-side exception, not just the HTTP status).
"""
if authenticate:
self.authenticate(self.USER_LOGIN, self.USER_PASSWORD)
url = f'/fusion/bank_rec/{endpoint}'
body = {
'jsonrpc': '2.0',
'method': 'call',
'params': params,
'id': 1,
}
response = self.url_open(
url,
data=json.dumps(body),
headers={'Content-Type': 'application/json'},
)
self.assertEqual(
response.status_code, 200,
f"Endpoint {endpoint} returned {response.status_code}: "
f"{response.text[:300]}")
payload = response.json()
if 'error' in payload:
self.fail(
f"Endpoint {endpoint} errored: "
f"{json.dumps(payload['error'])[:600]}")
return payload.get('result', {})
# ------------------------------------------------------------------
# 1. get_state
# ------------------------------------------------------------------
def test_get_state(self):
result = self._jsonrpc('get_state', {
'journal_id': self.journal.id,
'company_id': self.env.company.id,
})
self.assertIn('journal', result)
self.assertEqual(result['journal']['id'], self.journal.id)
self.assertIn('unreconciled_count', result)
self.assertIn('total_pending_amount', result)
self.assertIn('last_statement_date', result)
# ------------------------------------------------------------------
# 2. list_unreconciled
# ------------------------------------------------------------------
def test_list_unreconciled(self):
# Reuse a single statement so we don't trip the
# (journal_id, name) uniqueness or hit the parent-move autocreate
# path twice in the same flush window.
statement = f.make_bank_statement(
self.env, journal=self.journal, name='List Stmt')
f.make_bank_line(
self.env, journal=self.journal, statement=statement,
amount=100, partner=self.partner, memo='List 1')
f.make_bank_line(
self.env, journal=self.journal, statement=statement,
amount=200, partner=self.partner, memo='List 2')
result = self._jsonrpc('list_unreconciled', {
'journal_id': self.journal.id,
'limit': 50,
'offset': 0,
'company_id': self.env.company.id,
})
self.assertIn('lines', result)
self.assertGreaterEqual(len(result['lines']), 2)
self.assertGreaterEqual(result['total'], 2)
first = result['lines'][0]
for key in ('id', 'amount', 'fusion_top_suggestion_id',
'fusion_confidence_band', 'attachment_count'):
self.assertIn(key, first)
# ------------------------------------------------------------------
# 3. get_line_detail
# ------------------------------------------------------------------
def test_get_line_detail(self):
line = f.make_bank_line(
self.env, journal=self.journal, amount=100, partner=self.partner)
f.make_suggestion(
self.env, statement_line=line, confidence=0.85)
result = self._jsonrpc(
'get_line_detail', {'statement_line_id': line.id})
self.assertEqual(result['line']['id'], line.id)
self.assertEqual(result['line']['amount'], 100.0)
self.assertGreaterEqual(len(result['suggestions']), 1)
sug = result['suggestions'][0]
for key in ('id', 'candidate_ids', 'confidence', 'rank',
'reasoning', 'scores'):
self.assertIn(key, sug)
# ------------------------------------------------------------------
# 4. suggest_matches
# ------------------------------------------------------------------
def test_suggest_matches(self):
f.make_invoice(self.env, partner=self.partner, amount=300)
line = f.make_bank_line(
self.env, journal=self.journal, amount=300, partner=self.partner)
result = self._jsonrpc('suggest_matches', {
'statement_line_ids': [line.id],
'limit_per_line': 3,
})
self.assertIn('suggestions', result)
self.assertIsInstance(result['suggestions'], dict)
# ------------------------------------------------------------------
# 5. accept_suggestion
# ------------------------------------------------------------------
def test_accept_suggestion(self):
invoice = f.make_invoice(
self.env, partner=self.partner, amount=400)
recv = invoice.line_ids.filtered(
lambda l: l.account_id.account_type == 'asset_receivable')
line = f.make_bank_line(
self.env, journal=self.journal, amount=400, partner=self.partner)
sug = f.make_suggestion(
self.env, statement_line=line,
candidate_move_lines=recv, confidence=0.92)
result = self._jsonrpc(
'accept_suggestion', {'suggestion_id': sug.id})
self.assertEqual(result['status'], 'accepted')
self.assertGreater(len(result['partial_ids']), 0)
self.assertIn('unreconciled_count_after', result)
# ------------------------------------------------------------------
# 6. reconcile_manual
# ------------------------------------------------------------------
def _make_pair(self, *, amount, statement=None):
"""Inline reconcile-able pair against ``self.journal``.
The shared ``make_reconcileable_pair`` factory creates a fresh bank
journal per call (default code 'TEST'), which collides with the
unique (code, company) constraint when used multiple times in one
test. Reusing ``self.journal`` (and optionally a shared statement)
keeps every pair on the same journal.
"""
invoice = f.make_invoice(
self.env, partner=self.partner, amount=amount)
recv = invoice.line_ids.filtered(
lambda l: l.account_id.account_type == 'asset_receivable')
line = f.make_bank_line(
self.env, journal=self.journal, statement=statement,
amount=amount, partner=self.partner)
return line, recv
def test_reconcile_manual(self):
line, recv = self._make_pair(amount=550)
result = self._jsonrpc('reconcile_manual', {
'statement_line_id': line.id,
'against_move_line_ids': recv.ids,
})
self.assertEqual(result['status'], 'reconciled')
self.assertGreater(len(result['partial_ids']), 0)
# ------------------------------------------------------------------
# 7. unreconcile
# ------------------------------------------------------------------
def test_unreconcile(self):
line, recv = self._make_pair(amount=625)
rec = self.env['fusion.reconcile.engine'].reconcile_one(
line, against_lines=recv)
result = self._jsonrpc('unreconcile', {
'partial_reconcile_ids': rec['partial_ids'],
})
self.assertEqual(result['status'], 'unreconciled')
self.assertGreater(len(result['unreconciled_line_ids']), 0)
# ------------------------------------------------------------------
# 8. write_off -- smoke only (Task 12 deferred full coverage)
# ------------------------------------------------------------------
def test_write_off_smoke(self):
line = f.make_bank_line(
self.env, journal=self.journal, amount=12.34,
partner=self.partner)
# Pick any expense-type account that exists in the chart.
wo_account = self.env['account.account'].search([
('account_type', '=', 'expense'),
('company_ids', 'in', self.env.company.id),
], limit=1)
if not wo_account:
self.skipTest("No expense account available for write-off smoke")
# Endpoint must respond without 500-erroring; engine may legitimately
# raise a ValidationError for an over-allocation, in which case the
# JSON-RPC response will include an 'error' key. We accept either
# success or a structured error -- what we are guarding against is a
# routing-layer regression (NameError, missing import, etc.).
url = '/fusion/bank_rec/write_off'
self.authenticate(self.USER_LOGIN, self.USER_PASSWORD)
body = {
'jsonrpc': '2.0', 'method': 'call', 'id': 1,
'params': {
'statement_line_id': line.id,
'account_id': wo_account.id,
'amount': line.amount,
'label': 'Smoke write-off',
},
}
response = self.url_open(
url, data=json.dumps(body),
headers={'Content-Type': 'application/json'})
self.assertEqual(
response.status_code, 200,
f"write_off returned {response.status_code}: "
f"{response.text[:300]}")
# ------------------------------------------------------------------
# 9. bulk_reconcile
# ------------------------------------------------------------------
def test_bulk_reconcile(self):
statement = f.make_bank_statement(
self.env, journal=self.journal, name='Bulk Stmt')
line_ids = []
for amt in (110, 220, 330):
line, _recv = self._make_pair(amount=amt, statement=statement)
line_ids.append(line.id)
result = self._jsonrpc('bulk_reconcile', {
'statement_line_ids': line_ids,
'strategy': 'auto',
})
self.assertIn('reconciled_count', result)
self.assertGreaterEqual(result['reconciled_count'], 3)
# ------------------------------------------------------------------
# 10. get_partner_history
# ------------------------------------------------------------------
def test_get_partner_history(self):
for d in (5, 12, 20):
f.make_precedent(
self.env, partner=self.partner, days_ago=d, amount=1000)
f.make_pattern(
self.env, partner=self.partner, reconcile_count=3)
result = self._jsonrpc('get_partner_history', {
'partner_id': self.partner.id,
'limit': 10,
})
self.assertEqual(result['partner']['id'], self.partner.id)
self.assertGreaterEqual(len(result['recent_reconciles']), 3)
self.assertIsNotNone(result['pattern'])
self.assertEqual(result['pattern']['reconcile_count'], 3)
# ------------------------------------------------------------------
# 11. unauthenticated traffic is blocked
# ------------------------------------------------------------------
def test_unauthenticated_request_blocked(self):
# Use a fresh session by creating a new opener -- self.url_open
# reuses the test session, which `authenticate()` would mutate.
url = '/fusion/bank_rec/get_state'
body = {
'jsonrpc': '2.0', 'method': 'call', 'id': 1,
'params': {
'journal_id': self.journal.id,
'company_id': self.env.company.id,
},
}
# No call to self.authenticate() -> session has no uid.
response = self.url_open(
url, data=json.dumps(body),
headers={'Content-Type': 'application/json'},
allow_redirects=False,
)
# Odoo's auth='user' on a JSON-RPC route returns a 200 with an
# error envelope (SessionExpiredException) when not authenticated;
# what must NOT happen is the handler running and returning our
# success payload.
if response.status_code == 200:
payload = response.json()
self.assertIn(
'error', payload,
"Unauthenticated request should not return a success result")
else:
# 3xx redirect or 4xx are also acceptable rejections.
self.assertGreaterEqual(response.status_code, 300)

View File

@@ -0,0 +1,85 @@
"""Smoke tests for the cron handler methods.
We don't test the Odoo cron scheduler itself (it works) — we test that
calling the cron methods directly does what they're supposed to do."""
from odoo.tests.common import TransactionCase, tagged
from . import _factories as f
@tagged('post_install', '-at_install')
class TestFusionBankRecCron(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Cron Test Partner'})
self.cron = self.env['fusion.bank.rec.cron']
def test_cron_suggest_pending_creates_suggestions_for_new_line(self):
f.make_invoice(self.env, partner=self.partner, amount=420.00)
bank_line = f.make_bank_line(
self.env, amount=420.00, partner=self.partner)
Sug = self.env['fusion.reconcile.suggestion']
self.assertEqual(
Sug.search_count([('statement_line_id', '=', bank_line.id)]), 0)
self.cron._cron_suggest_pending(batch_size=10)
self.assertGreater(
Sug.search_count([('statement_line_id', '=', bank_line.id)]), 0)
def test_cron_suggest_pending_skips_lines_with_recent_suggestions(self):
f.make_invoice(self.env, partner=self.partner, amount=510.00)
bank_line = f.make_bank_line(
self.env, amount=510.00, partner=self.partner)
f.make_suggestion(
self.env, statement_line=bank_line, confidence=0.5)
Sug = self.env['fusion.reconcile.suggestion']
before = Sug.search_count(
[('statement_line_id', '=', bank_line.id)])
self.cron._cron_suggest_pending(batch_size=10)
after = Sug.search_count(
[('statement_line_id', '=', bank_line.id)])
self.assertEqual(
before, after,
"Cron should skip lines with a recent pending suggestion")
def test_cron_refresh_patterns_creates_pattern_for_partner_with_precedents(self):
for d in [10, 24, 38]:
f.make_precedent(
self.env, partner=self.partner, days_ago=d, amount=1000)
Pattern = self.env['fusion.reconcile.pattern']
Pattern.search([('partner_id', '=', self.partner.id)]).unlink()
self.cron._cron_refresh_patterns()
pattern = Pattern.search(
[('partner_id', '=', self.partner.id)], limit=1)
self.assertTrue(
pattern, "Cron should create pattern for partner with precedents")
self.assertEqual(pattern.reconcile_count, 3)
def test_cron_refresh_patterns_updates_existing_pattern(self):
Pattern = self.env['fusion.reconcile.pattern']
Pattern.search([('partner_id', '=', self.partner.id)]).unlink()
f.make_pattern(
self.env, partner=self.partner, reconcile_count=99)
for d in [5, 15]:
f.make_precedent(
self.env, partner=self.partner, days_ago=d, amount=500)
self.cron._cron_refresh_patterns()
pattern = Pattern.search(
[('partner_id', '=', self.partner.id)], limit=1)
self.assertEqual(
pattern.reconcile_count, 2,
"Cron should update existing pattern with fresh precedent count")
def test_cron_refresh_mv_does_not_raise(self):
# Just verify it runs — full MV behaviour is tested in Task 24
self.cron._cron_refresh_mv()

View File

@@ -0,0 +1,56 @@
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_bank_rec.services.exchange_diff import (
compute_exchange_diff, ExchangeDiffResult,
)
@tagged('post_install', '-at_install')
class TestExchangeDiff(TransactionCase):
def test_no_diff_when_currencies_match_and_rates_match(self):
result = compute_exchange_diff(
line_amount=100.00, line_currency_code='CAD',
against_amount=100.00, against_currency_code='CAD',
line_rate=1.0, against_rate=1.0,
)
self.assertFalse(result.needs_diff_move)
self.assertEqual(result.diff_amount, 0.0)
def test_diff_when_rates_differ_same_currency(self):
"""USD invoice posted at 1.35, USD bank line settled at 1.40 -> diff exists.
100 USD at 1.40 = 140 CAD; same at 1.35 = 135 CAD; diff = 5 CAD gain."""
result = compute_exchange_diff(
line_amount=100.00, line_currency_code='USD',
against_amount=100.00, against_currency_code='USD',
line_rate=1.40, against_rate=1.35,
)
self.assertTrue(result.needs_diff_move)
self.assertAlmostEqual(result.diff_amount, 5.00, places=2)
def test_diff_negative_when_rate_dropped(self):
"""USD invoice at 1.40, settled at 1.35 -> loss"""
result = compute_exchange_diff(
line_amount=100.00, line_currency_code='USD',
against_amount=100.00, against_currency_code='USD',
line_rate=1.35, against_rate=1.40,
)
self.assertTrue(result.needs_diff_move)
self.assertAlmostEqual(result.diff_amount, -5.00, places=2)
def test_company_amounts_computed_correctly(self):
result = compute_exchange_diff(
line_amount=100.00, line_currency_code='USD',
against_amount=100.00, against_currency_code='USD',
line_rate=1.40, against_rate=1.35,
)
self.assertAlmostEqual(result.line_company_amount, 140.00, places=2)
self.assertAlmostEqual(result.against_company_amount, 135.00, places=2)
def test_tolerance_handles_rounding_noise(self):
"""Tiny FX rounding under 0.005 should NOT trigger a diff move."""
result = compute_exchange_diff(
line_amount=100.00, line_currency_code='USD',
against_amount=100.00, against_currency_code='USD',
line_rate=1.40000, against_rate=1.40003, # 0.003 cent diff
)
self.assertFalse(result.needs_diff_move)

View File

@@ -0,0 +1,74 @@
"""Smoke tests verifying the factories produce usable records.
Not testing factory correctness exhaustively — just that each helper
returns a record of the expected type with the expected basic state."""
from odoo.tests.common import TransactionCase, tagged
from . import _factories as f
@tagged('post_install', '-at_install')
class TestFactories(TransactionCase):
def test_make_bank_journal(self):
journal = f.make_bank_journal(self.env)
self.assertEqual(journal._name, 'account.journal')
self.assertEqual(journal.type, 'bank')
def test_make_bank_statement(self):
statement = f.make_bank_statement(self.env)
self.assertEqual(statement._name, 'account.bank.statement')
self.assertTrue(statement.journal_id)
def test_make_bank_line(self):
line = f.make_bank_line(self.env, amount=250.00, memo='Smoke memo')
self.assertEqual(line._name, 'account.bank.statement.line')
self.assertEqual(line.amount, 250.00)
self.assertEqual(line.payment_ref, 'Smoke memo')
self.assertFalse(line.is_reconciled)
def test_make_bank_line_with_partner(self):
partner = self.env['res.partner'].create({'name': 'Factory Partner'})
line = f.make_bank_line(self.env, partner=partner, amount=500)
self.assertEqual(line.partner_id, partner)
def test_make_invoice_posted(self):
partner = self.env['res.partner'].create({'name': 'Invoice Partner'})
invoice = f.make_invoice(self.env, partner=partner, amount=300)
self.assertEqual(invoice._name, 'account.move')
self.assertEqual(invoice.move_type, 'out_invoice')
self.assertEqual(invoice.state, 'posted')
self.assertAlmostEqual(invoice.amount_total, 300, places=2)
def test_make_vendor_bill_posted(self):
partner = self.env['res.partner'].create({'name': 'Vendor Partner'})
bill = f.make_vendor_bill(self.env, partner=partner, amount=400)
self.assertEqual(bill.move_type, 'in_invoice')
self.assertEqual(bill.state, 'posted')
def test_make_suggestion(self):
line = f.make_bank_line(self.env, amount=100)
sug = f.make_suggestion(self.env, statement_line=line, confidence=0.85)
self.assertEqual(sug._name, 'fusion.reconcile.suggestion')
self.assertEqual(sug.confidence, 0.85)
self.assertEqual(sug.state, 'pending')
def test_make_pattern(self):
partner = self.env['res.partner'].create({'name': 'Pattern Partner'})
pattern = f.make_pattern(self.env, partner=partner, reconcile_count=20)
self.assertEqual(pattern._name, 'fusion.reconcile.pattern')
self.assertEqual(pattern.reconcile_count, 20)
def test_make_precedent(self):
partner = self.env['res.partner'].create({'name': 'Precedent Partner'})
precedent = f.make_precedent(self.env, partner=partner, amount=999.99)
self.assertEqual(precedent._name, 'fusion.reconcile.precedent')
self.assertEqual(precedent.amount, 999.99)
self.assertEqual(precedent.source, 'manual')
def test_make_reconcileable_pair(self):
bank_line, recv_lines = f.make_reconcileable_pair(self.env, amount=750)
self.assertEqual(bank_line.amount, 750.00)
self.assertGreater(len(recv_lines), 0)
self.assertAlmostEqual(sum(recv_lines.mapped('amount_residual')), 750, places=2)

View File

@@ -0,0 +1,59 @@
"""Tests verifying legacy tools route through fusion.reconcile.engine when present.
These tests run in the fusion_accounting_bank_rec context where the engine IS
available, so they assert the engine path is taken and produces correct
results. The fallback path is exercised by the existing fusion_accounting_ai
tests when fusion_accounting_bank_rec is not installed."""
from unittest.mock import patch
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_ai.services.tools import bank_reconciliation as tools
from . import _factories as f
@tagged('post_install', '-at_install')
class TestLegacyToolsRefactor(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Refactor Test Partner'})
def test_match_bank_line_to_payments_uses_engine(self):
"""When engine is present, match_bank_line_to_payments must produce
a partial reconcile via the engine, not via set_line_bank_statement_line."""
bank_line, recv_lines = f.make_reconcileable_pair(
self.env, amount=180.00, partner=self.partner)
result = tools.match_bank_line_to_payments(self.env, {
'statement_line_id': bank_line.id,
'move_line_ids': recv_lines.ids,
})
self.assertEqual(result.get('status'), 'matched')
bank_line.invalidate_recordset(['is_reconciled'])
self.assertTrue(bank_line.is_reconciled)
# Verify a precedent was recorded - engine-only behaviour
Precedent = self.env['fusion.reconcile.precedent']
precedents = Precedent.search([('partner_id', '=', self.partner.id)])
self.assertGreater(len(precedents), 0,
"Engine path should record a precedent; legacy path would not")
def test_auto_reconcile_bank_lines_uses_engine(self):
"""When engine is present, auto_reconcile_bank_lines must call
fusion.reconcile.engine.reconcile_batch (not the Enterprise-only
_try_auto_reconcile_statement_lines fallback). We patch
reconcile_batch to verify routing without running the real engine
across every legacy unreconciled line in the test DB."""
Engine = type(self.env['fusion.reconcile.engine'])
with patch.object(
Engine, 'reconcile_batch', autospec=True,
return_value={'reconciled_count': 2, 'skipped': 0, 'errors': []},
) as engine_call:
result = tools.auto_reconcile_bank_lines(self.env, {
'company_id': self.env.company.id,
})
self.assertEqual(result['status'], 'completed')
self.assertTrue(engine_call.called,
"Engine path must invoke fusion.reconcile.engine.reconcile_batch")
# Verify the engine was passed the strategy='auto' kwarg per spec
_self, _lines = engine_call.call_args.args[0], engine_call.call_args.args[1]
self.assertEqual(engine_call.call_args.kwargs.get('strategy'), 'auto')

View File

@@ -0,0 +1,102 @@
"""Local LLM compatibility test (LM Studio, Ollama, etc.).
Skips if no local OpenAI-compatible LLM server is reachable. When one is
running (LM Studio at :1234, Ollama at :11434), runs an end-to-end:
1. Configure ``ir.config_parameter`` to point at the local server.
2. Trigger ``engine.suggest_matches`` with the 'openai' provider.
3. Assert the call did not crash and produced at least one suggestion.
The smoke is intentionally lenient: local models often emit malformed
JSON, in which case ``confidence_scoring`` falls back to statistical-only
ranking. We assert end-to-end happiness, not AI re-rank quality.
"""
import socket
from odoo.tests.common import TransactionCase, tagged
from . import _factories as f
def _server_reachable(host, port, timeout=1.0):
try:
with socket.create_connection((host, port), timeout=timeout):
return True
except (OSError, socket.timeout):
return False
def _detect_local_llm():
"""Return (base_url, model_name) tuple, or (None, None) if no server.
Tries LM Studio (:1234) and Ollama (:11434) on both
``host.docker.internal`` (so the container can reach the host) and
``localhost`` (so a non-containerised run finds the same servers).
"""
candidates = (
('host.docker.internal', 1234, 'local-model'), # LM Studio
('host.docker.internal', 11434, 'llama3.1:8b'), # Ollama
('localhost', 1234, 'local-model'),
('localhost', 11434, 'llama3.1:8b'),
)
for host, port, default_model in candidates:
if _server_reachable(host, port, timeout=0.5):
return (f'http://{host}:{port}/v1', default_model)
return (None, None)
@tagged('post_install', '-at_install', 'local_llm')
class TestLocalLLMCompat(TransactionCase):
def setUp(self):
super().setUp()
self.base_url, self.model = _detect_local_llm()
if not self.base_url:
self.skipTest(
"No local LLM server detected "
"(LM Studio :1234 / Ollama :11434)")
def test_suggest_matches_with_local_llm(self):
params = self.env['ir.config_parameter'].sudo()
prior = {
'fusion_accounting.openai_base_url': params.get_param(
'fusion_accounting.openai_base_url'),
'fusion_accounting.openai_model': params.get_param(
'fusion_accounting.openai_model'),
'fusion_accounting.openai_api_key': params.get_param(
'fusion_accounting.openai_api_key'),
'fusion_accounting.provider.bank_rec_suggest': params.get_param(
'fusion_accounting.provider.bank_rec_suggest'),
}
params.set_param('fusion_accounting.openai_base_url', self.base_url)
params.set_param('fusion_accounting.openai_model', self.model)
# Local servers ignore the key but the adapter requires *some* value.
params.set_param('fusion_accounting.openai_api_key', 'lm-studio')
params.set_param(
'fusion_accounting.provider.bank_rec_suggest', 'openai')
try:
partner = self.env['res.partner'].create(
{'name': 'Local LLM Partner'})
f.make_invoice(self.env, partner=partner, amount=750)
bank_line = f.make_bank_line(
self.env, amount=750, partner=partner,
memo='REF 12345 Local LLM test')
result = self.env['fusion.reconcile.engine'].suggest_matches(
bank_line, limit_per_line=3)
self.assertIn(bank_line.id, result)
suggestions = self.env['fusion.reconcile.suggestion'].search([
('statement_line_id', '=', bank_line.id),
])
self.assertGreater(
len(suggestions), 0,
"Local LLM run should still produce at least one suggestion "
"(statistical fallback if AI re-rank fails)")
finally:
for key, value in prior.items():
if value is not None:
params.set_param(key, value)

View File

@@ -0,0 +1,111 @@
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_bank_rec.services.matching_strategies import (
Candidate, AmountExactStrategy, FIFOStrategy, MultiInvoiceStrategy, MatchResult,
)
@tagged('post_install', '-at_install')
class TestAmountExactStrategy(TransactionCase):
def test_picks_exact_amount(self):
candidates = [
Candidate(id=1, amount=99.99, partner_id=42, age_days=10),
Candidate(id=2, amount=100.00, partner_id=42, age_days=20),
Candidate(id=3, amount=100.50, partner_id=42, age_days=5),
]
result = AmountExactStrategy().match(bank_amount=100.00, candidates=candidates)
self.assertEqual(result.picked_ids, [2])
self.assertEqual(result.confidence, 1.0)
def test_no_match_when_no_exact(self):
candidates = [
Candidate(id=1, amount=99.99, partner_id=42, age_days=10),
Candidate(id=2, amount=100.50, partner_id=42, age_days=20),
]
result = AmountExactStrategy().match(bank_amount=100.00, candidates=candidates)
self.assertEqual(result.picked_ids, [])
def test_picks_oldest_when_multiple_exact(self):
candidates = [
Candidate(id=1, amount=100.00, partner_id=42, age_days=10),
Candidate(id=2, amount=100.00, partner_id=42, age_days=30), # oldest
Candidate(id=3, amount=100.00, partner_id=42, age_days=20),
]
result = AmountExactStrategy().match(bank_amount=100.00, candidates=candidates)
self.assertEqual(result.picked_ids, [2])
def test_handles_empty_candidates(self):
result = AmountExactStrategy().match(bank_amount=100.00, candidates=[])
self.assertEqual(result.picked_ids, [])
@tagged('post_install', '-at_install')
class TestFIFOStrategy(TransactionCase):
def test_picks_oldest_first(self):
candidates = [
Candidate(id=1, amount=50.00, partner_id=42, age_days=10),
Candidate(id=2, amount=50.00, partner_id=42, age_days=30),
Candidate(id=3, amount=50.00, partner_id=42, age_days=20),
]
result = FIFOStrategy().match(bank_amount=100.00, candidates=candidates)
self.assertEqual(result.picked_ids, [2, 3]) # oldest two summing to 100
def test_handles_partial_payment(self):
candidates = [
Candidate(id=1, amount=200.00, partner_id=42, age_days=30),
]
result = FIFOStrategy().match(bank_amount=100.00, candidates=candidates)
self.assertEqual(result.picked_ids, [1]) # partial reconcile signaled by residual
self.assertEqual(result.residual, -100.00) # over-allocated; engine handles
def test_handles_empty_candidates(self):
result = FIFOStrategy().match(bank_amount=100.00, candidates=[])
self.assertEqual(result.picked_ids, [])
@tagged('post_install', '-at_install')
class TestMultiInvoiceStrategy(TransactionCase):
def test_finds_smallest_set_summing_to_amount(self):
candidates = [
Candidate(id=1, amount=30.00, partner_id=42, age_days=10),
Candidate(id=2, amount=40.00, partner_id=42, age_days=15),
Candidate(id=3, amount=30.00, partner_id=42, age_days=20),
Candidate(id=4, amount=70.00, partner_id=42, age_days=25),
]
result = MultiInvoiceStrategy(max_combinations=3).match(
bank_amount=100.00, candidates=candidates)
# Two-element solutions exist (e.g., {3,4}=100). Strategy should pick a 2-set.
self.assertEqual(len(result.picked_ids), 2)
# The picked set should sum to 100
picked_amounts = [c.amount for c in candidates if c.id in result.picked_ids]
self.assertAlmostEqual(sum(picked_amounts), 100.00, places=2)
def test_returns_empty_when_no_combination_sums(self):
candidates = [
Candidate(id=1, amount=15.00, partner_id=42, age_days=10),
Candidate(id=2, amount=25.00, partner_id=42, age_days=15),
]
result = MultiInvoiceStrategy(max_combinations=3).match(
bank_amount=100.00, candidates=candidates)
self.assertEqual(result.picked_ids, [])
def test_respects_max_combinations(self):
# Many small invoices — only combinations of ≤3 items considered
candidates = [Candidate(id=i, amount=10.00, partner_id=42, age_days=i)
for i in range(1, 11)]
result = MultiInvoiceStrategy(max_combinations=3).match(
bank_amount=100.00, candidates=candidates)
# Can't make 100 with ≤3 items of $10 each
self.assertEqual(result.picked_ids, [])
def test_strategy_name_includes_combination_size(self):
candidates = [
Candidate(id=1, amount=50.00, partner_id=42, age_days=10),
Candidate(id=2, amount=50.00, partner_id=42, age_days=20),
]
result = MultiInvoiceStrategy(max_combinations=3).match(
bank_amount=100.00, candidates=candidates)
self.assertEqual(set(result.picked_ids), {1, 2})
self.assertIn('multi_invoice', result.strategy_name)

View File

@@ -0,0 +1,42 @@
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_bank_rec.services.memo_tokenizer import tokenize_memo
@tagged('post_install', '-at_install')
class TestMemoTokenizer(TransactionCase):
def test_extracts_rbc_etf_reference(self):
tokens = tokenize_memo("RBC ETF DEP REF 4831")
self.assertIn('RBC', tokens)
self.assertIn('ETF', tokens)
self.assertIn('REF4831', tokens)
def test_extracts_cheque_number(self):
tokens = tokenize_memo("CHEQUE 4827 - WESTIN PLATING")
self.assertIn('CHEQUE4827', tokens)
self.assertIn('WESTIN', tokens)
self.assertIn('PLATING', tokens)
def test_strips_noise_tokens(self):
tokens = tokenize_memo("PAYMENT - INV - DEP - 12345")
self.assertNotIn('-', tokens)
self.assertEqual([t for t in tokens if len(t) <= 1], [])
def test_handles_empty_memo(self):
self.assertEqual(tokenize_memo(""), [])
self.assertEqual(tokenize_memo(None), [])
def test_canadian_french_memo(self):
tokens = tokenize_memo("PAIEMENT VIREMENT BANCAIRE")
self.assertIn('PAIEMENT', tokens)
self.assertIn('VIREMENT', tokens)
def test_normalises_case(self):
tokens = tokenize_memo("rbc etf dep ref 4831")
self.assertIn('RBC', tokens)
def test_handles_special_characters(self):
tokens = tokenize_memo("RBC*PAYMENT/REF#4831")
self.assertIn('RBC', tokens)
self.assertIn('PAYMENT', tokens)
self.assertIn('REF4831', tokens)

View File

@@ -0,0 +1,115 @@
"""Migration round-trip: bootstrap step backfills precedents from
existing account.partial.reconcile rows.
Exercises Task 39's _bank_rec_bootstrap_step end-to-end:
1. Set up a bank-line / invoice reconciliation via the engine. This
creates an account.partial.reconcile row.
2. Wipe the auto-recorded fusion.reconcile.precedent rows so the
backfill has work to do.
3. Run wizard._bank_rec_bootstrap_step().
4. Assert at least one precedent was created with source='backfill',
the wizard reports successful pattern + MV refresh, and that a
second run is a no-op (idempotent).
"""
from odoo.tests.common import TransactionCase, tagged
from . import _factories as f
@tagged('post_install', '-at_install')
class TestMigrationRoundTrip(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({
'name': 'Migration Round-Trip Partner',
})
self.journal = f.make_bank_journal(
self.env, name='Migration Bank', code='MIGBK')
self.statement = f.make_bank_statement(
self.env, journal=self.journal, name='Migration Statement')
def _seed_partial_reconciles(self, amounts):
"""Create one reconciled bank-line/invoice pair per amount, reusing
a single bank journal so we don't violate the
account_journal_code_company_uniq constraint.
Each call here produces one account.partial.reconcile row.
Returns the partial recordset.
"""
Engine = self.env['fusion.reconcile.engine']
partials = self.env['account.partial.reconcile']
for amount in amounts:
invoice = f.make_invoice(
self.env, partner=self.partner, amount=amount)
recv_lines = invoice.line_ids.filtered(
lambda l: l.account_id.account_type == 'asset_receivable')
bank_line = f.make_bank_line(
self.env, statement=self.statement, amount=amount,
partner=self.partner)
result = Engine.reconcile_one(
bank_line, against_lines=recv_lines)
partials |= self.env['account.partial.reconcile'].browse(
result['partial_ids'])
return partials
def _wipe_precedents(self):
self.env['fusion.reconcile.precedent'].search([
('partner_id', '=', self.partner.id),
]).unlink()
def test_bootstrap_creates_precedents_from_existing_reconciles(self):
partials = self._seed_partial_reconciles([125.00, 275.00])
self.assertTrue(partials,
"Test setup should produce account.partial.reconcile rows")
self._wipe_precedents()
before_backfill = self.env['fusion.reconcile.precedent'].search_count([
('partner_id', '=', self.partner.id),
('source', '=', 'backfill'),
])
self.assertEqual(before_backfill, 0,
"Precondition: no backfill precedents should exist before bootstrap")
wizard = self.env['fusion.migration.wizard'].create({})
result = wizard._bank_rec_bootstrap_step()
self.assertEqual(result['step'], 'bank_rec_bootstrap')
self.assertGreaterEqual(result['precedents_created'], 1,
"Bootstrap should backfill at least one precedent from the "
"partial.reconcile rows produced in setUp")
self.assertTrue(result['mv_refreshed'],
"Bootstrap should report successful MV refresh")
after_backfill = self.env['fusion.reconcile.precedent'].search_count([
('partner_id', '=', self.partner.id),
('source', '=', 'backfill'),
])
self.assertGreaterEqual(after_backfill, 1,
"At least one source='backfill' precedent should exist post-bootstrap")
def test_bootstrap_step_idempotent(self):
self._seed_partial_reconciles([411.00])
self._wipe_precedents()
wizard = self.env['fusion.migration.wizard'].create({})
result1 = wizard._bank_rec_bootstrap_step()
created_first_run = result1['precedents_created']
self.assertGreaterEqual(created_first_run, 1)
result2 = wizard._bank_rec_bootstrap_step()
self.assertEqual(result2['precedents_created'], 0,
"Second bootstrap should create zero precedents (idempotent)")
self.assertGreaterEqual(result2['precedents_skipped'], created_first_run,
"Second bootstrap should skip at least what the first one created")
def test_bootstrap_refreshes_mv_without_error(self):
"""The bootstrap call must not raise even when there's nothing to do."""
wizard = self.env['fusion.migration.wizard'].create({})
try:
result = wizard._bank_rec_bootstrap_step()
except Exception as e: # noqa: BLE001
self.fail(f"Bootstrap raised: {e}")
self.assertIn('mv_refreshed', result)
self.assertIn('patterns_refreshed', result)

View File

@@ -0,0 +1,82 @@
"""Smoke tests for the fusion_unreconciled_bank_line_mv materialized view.
Notes on transactional semantics:
- REFRESH MATERIALIZED VIEW (non-CONCURRENTLY) IS transactional and runs
inside the current transaction. Postgres always shows a transaction
its own (uncommitted) writes, so an INSERT followed by a REFRESH in
the same transaction picks up the new row — no `cr.commit()` needed.
- Odoo's TransactionCase forbids cr.commit() anyway (it would break the
per-test savepoint rollback). We rely on rollback to clean up both
the test fixtures and the MV-table mutations from the refresh.
- REFRESH MATERIALIZED VIEW CONCURRENTLY must run OUTSIDE a transaction
block; we always pass concurrently=False from tests. The production
cron path (Task 25) will open a dedicated autocommit cursor for the
concurrent refresh.
"""
from odoo.tests.common import TransactionCase, tagged
from . import _factories as f
@tagged('post_install', '-at_install')
class TestUnreconciledBankLineMV(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({
'name': 'MV Test Partner',
})
# Refresh once at the start so the MV reflects the current snapshot
# (including any rows inserted earlier in this savepoint chain).
self.env['fusion.unreconciled.bank.line.mv']._refresh(
concurrently=False)
def test_mv_exists_and_is_queryable(self):
# Smoke: the model can be searched without error.
rows = self.env['fusion.unreconciled.bank.line.mv'].search(
[], limit=10)
self.assertIsNotNone(rows)
def test_mv_includes_unreconciled_line(self):
bank_line = f.make_bank_line(
self.env, amount=999.99, partner=self.partner)
self.env['fusion.unreconciled.bank.line.mv']._refresh(
concurrently=False)
mv_row = self.env['fusion.unreconciled.bank.line.mv'].search([
('id', '=', bank_line.id),
])
self.assertTrue(
mv_row,
"MV should contain freshly-inserted unreconciled line")
self.assertAlmostEqual(mv_row.amount, 999.99, places=2)
# No suggestion yet -> band 'none', confidence 0.
self.assertEqual(mv_row.confidence_band, 'none')
self.assertEqual(mv_row.attachment_count, 0)
def test_mv_excludes_reconciled_line(self):
bank_line, recv_lines = f.make_reconcileable_pair(
self.env, amount=100.00, partner=self.partner)
self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
self.env['fusion.unreconciled.bank.line.mv']._refresh(
concurrently=False)
mv_row = self.env['fusion.unreconciled.bank.line.mv'].search([
('id', '=', bank_line.id),
])
self.assertFalse(
mv_row, "Reconciled line should be excluded from MV")
def test_mv_confidence_band_high_for_high_conf_suggestion(self):
bank_line = f.make_bank_line(
self.env, amount=500.00, partner=self.partner)
f.make_suggestion(
self.env, statement_line=bank_line, confidence=0.92)
self.env['fusion.unreconciled.bank.line.mv']._refresh(
concurrently=False)
mv_row = self.env['fusion.unreconciled.bank.line.mv'].search([
('id', '=', bank_line.id),
])
self.assertTrue(mv_row, "MV row should exist for suggestion line")
# 0.92 falls in the 'high' band per the SQL CASE (>= 0.85).
self.assertEqual(mv_row.confidence_band, 'high')
self.assertAlmostEqual(mv_row.top_confidence, 0.92, places=2)

View File

@@ -0,0 +1,73 @@
from datetime import date, timedelta, datetime
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_bank_rec.services.pattern_extractor import (
extract_pattern_for_partner,
)
@tagged('post_install', '-at_install')
class TestPatternExtractor(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Pattern Test Partner'})
self.currency = self.env.ref('base.CAD')
self.company = self.env.company
def _make_precedent(self, *, amount, days_ago, memo='RBC,ETF', count=1, source='manual'):
return self.env['fusion.reconcile.precedent'].create({
'company_id': self.company.id,
'partner_id': self.partner.id,
'amount': amount,
'currency_id': self.currency.id,
'date': date.today() - timedelta(days=days_ago),
'memo_tokens': memo,
'matched_move_line_count': count,
'reconciled_at': datetime.now() - timedelta(days=days_ago),
'source': source,
})
def test_extracts_typical_amount_range(self):
for d in [10, 24, 38, 52]:
self._make_precedent(amount=1847.50, days_ago=d)
pattern_vals = extract_pattern_for_partner(
self.env, company_id=self.company.id, partner_id=self.partner.id)
self.assertIn('typical_amount_range', pattern_vals)
self.assertEqual(pattern_vals['reconcile_count'], 4)
def test_detects_exact_amount_strategy(self):
for d in range(0, 56, 14):
self._make_precedent(amount=1847.50, days_ago=d, count=1)
pattern_vals = extract_pattern_for_partner(
self.env, company_id=self.company.id, partner_id=self.partner.id)
self.assertEqual(pattern_vals['pref_strategy'], 'exact_amount')
def test_detects_multi_invoice_strategy(self):
for d in range(0, 56, 14):
self._make_precedent(amount=2500.00, days_ago=d, count=3)
pattern_vals = extract_pattern_for_partner(
self.env, company_id=self.company.id, partner_id=self.partner.id)
self.assertEqual(pattern_vals['pref_strategy'], 'multi_invoice')
def test_computes_cadence_days(self):
for d in [0, 14, 28, 42]:
self._make_precedent(amount=1000, days_ago=d)
pattern_vals = extract_pattern_for_partner(
self.env, company_id=self.company.id, partner_id=self.partner.id)
self.assertAlmostEqual(pattern_vals['typical_cadence_days'], 14.0, delta=1)
def test_extracts_common_memo_tokens(self):
self._make_precedent(amount=1000, days_ago=10, memo='RBC,ETF,REF')
self._make_precedent(amount=1000, days_ago=24, memo='RBC,ETF,DEPOSIT')
self._make_precedent(amount=1000, days_ago=38, memo='RBC,ETF,REF')
pattern_vals = extract_pattern_for_partner(
self.env, company_id=self.company.id, partner_id=self.partner.id)
self.assertIn('RBC', pattern_vals['common_memo_tokens'])
self.assertIn('ETF', pattern_vals['common_memo_tokens'])
def test_returns_zero_count_for_partner_with_no_precedents(self):
other_partner = self.env['res.partner'].create({'name': 'Empty Partner'})
pattern_vals = extract_pattern_for_partner(
self.env, company_id=self.company.id, partner_id=other_partner.id)
self.assertEqual(pattern_vals['reconcile_count'], 0)

View File

@@ -0,0 +1,188 @@
"""Performance benchmarks with P95 targets.
Tagged with ``benchmark`` so they can be selected explicitly:
odoo --test-tags 'benchmark' ...
These tests measure wall-clock time and assert P95 stays within plan
budgets. They run a small N (e.g. 10 iterations) so total test time
stays under 30s. For real load testing, use a separate harness.
Hard-fail thresholds are 5x the plan budget — they catch egregious
regressions without flaking on cold-start variance in CI.
"""
import json
import statistics
import time
from odoo.tests.common import HttpCase, TransactionCase, new_test_user, tagged
from . import _factories as f
def _percentile(samples, p):
"""Return the ``p``-th percentile of ``samples`` (0-100)."""
if not samples:
return None
if len(samples) == 1:
return samples[0]
return statistics.quantiles(samples, n=100)[p - 1]
@tagged('post_install', '-at_install', 'benchmark')
class TestEngineBenchmarks(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Bench Partner'})
# Pre-create a dedicated journal+statement and reuse them across all
# iterations -- otherwise the second make_bank_line() collides on the
# (code, company) unique constraint of the default 'TEST' journal.
self.journal = f.make_bank_journal(
self.env, name='Engine Bench Bank', code='EBB')
self.statement = f.make_bank_statement(
self.env, journal=self.journal, name='Engine Bench Stmt')
# Pre-create some invoices so suggest_matches has something to score
self.invoices = []
for amount in (100, 200, 300, 400, 500):
inv = f.make_invoice(self.env, partner=self.partner, amount=amount)
self.invoices.append(inv)
def test_suggest_matches_p95_under_500ms(self):
timings = []
for _ in range(10):
line = f.make_bank_line(
self.env, journal=self.journal, statement=self.statement,
amount=300, partner=self.partner)
start = time.perf_counter()
self.env['fusion.reconcile.engine'].suggest_matches(
line, limit_per_line=3)
elapsed = (time.perf_counter() - start) * 1000 # ms
timings.append(elapsed)
timings.sort()
p95 = _percentile(timings, 95)
median = statistics.median(timings)
msg = f"suggest_matches: median={median:.1f}ms p95={p95:.1f}ms"
print(f"\n PERF: {msg} (target <500ms)")
# Soft assertion -- log but don't fail under 5x budget (cold-start
# variance). Hard fail above 5x catches egregious regressions.
self.assertLess(
p95, 2500,
f"suggest_matches P95 way over budget: {msg} "
f"(target <500ms, hard fail >2500ms)")
def test_reconcile_batch_p95_under_5s(self):
# Create 50 matchable pairs on a shared journal/statement so we
# don't blow the (code, company) constraint.
journal = f.make_bank_journal(
self.env, name='Batch Bench Bank', code='BBB')
statement = f.make_bank_statement(
self.env, journal=journal, name='Batch Bench Stmt')
line_ids = []
for i in range(50):
invoice = f.make_invoice(
self.env, partner=self.partner, amount=100 + i)
del invoice # ensures the receivable JE exists for engine to find
line = f.make_bank_line(
self.env, journal=journal, statement=statement,
amount=100 + i, partner=self.partner)
line_ids.append(line.id)
lines = self.env['account.bank.statement.line'].browse(line_ids)
start = time.perf_counter()
result = self.env['fusion.reconcile.engine'].reconcile_batch(
lines, strategy='auto')
elapsed = (time.perf_counter() - start) * 1000
msg = (f"reconcile_batch(50 lines): {elapsed:.0f}ms, "
f"reconciled={result.get('reconciled_count', 'n/a')}")
print(f"\n PERF: {msg} (target <5000ms)")
self.assertLess(
elapsed, 25000,
f"reconcile_batch way over budget: {msg} "
f"(target <5000ms, hard fail >25000ms)")
@tagged('post_install', '-at_install', 'benchmark')
class TestControllerBenchmarks(HttpCase):
USER_LOGIN = 'bench_ctrl_user'
USER_PASSWORD = 'bench_ctrl_user'
def setUp(self):
super().setUp()
# Mirrors test_controller.py auth setup -- a fresh test user with
# the same group bundle the controller expects. The dev DB's admin
# password is non-default, so we cannot rely on 'admin'/'admin'.
new_test_user(
self.env,
login=self.USER_LOGIN,
password=self.USER_PASSWORD,
groups=(
'base.group_user,'
'account.group_account_user,'
'fusion_accounting_core.group_fusion_accounting_admin'
),
)
def test_list_unreconciled_p95_under_200ms(self):
partner = self.env['res.partner'].create({'name': 'Ctrl Bench'})
journal = f.make_bank_journal(
self.env, name='Ctrl Bench Bank', code='CBB')
statement = f.make_bank_statement(
self.env, journal=journal, name='Ctrl Bench Stmt')
for i in range(50):
f.make_bank_line(
self.env, journal=journal, statement=statement,
amount=100 + i, partner=partner,
memo=f'Ctrl bench line {i}')
self.authenticate(self.USER_LOGIN, self.USER_PASSWORD)
body = json.dumps({
'jsonrpc': '2.0',
'method': 'call',
'params': {
'journal_id': journal.id,
'limit': 50,
'offset': 0,
'company_id': self.env.company.id,
},
'id': 1,
})
timings = []
for _ in range(10):
start = time.perf_counter()
response = self.url_open(
'/fusion/bank_rec/list_unreconciled',
data=body,
headers={'Content-Type': 'application/json'},
)
elapsed = (time.perf_counter() - start) * 1000
self.assertEqual(response.status_code, 200)
timings.append(elapsed)
timings.sort()
p95 = _percentile(timings, 95)
median = statistics.median(timings)
msg = f"list_unreconciled: median={median:.1f}ms p95={p95:.1f}ms"
print(f"\n PERF: {msg} (target <200ms)")
self.assertLess(
p95, 1000,
f"list_unreconciled P95 way over budget: {msg} "
f"(target <200ms, hard fail >1000ms)")
@tagged('post_install', '-at_install', 'benchmark')
class TestMVBenchmarks(TransactionCase):
def test_mv_refresh_under_2s(self):
# Non-concurrent refresh works even before the MV has been seeded
# with a concurrent-refresh-eligible state.
start = time.perf_counter()
self.env['fusion.unreconciled.bank.line.mv']._refresh(
concurrently=False)
elapsed = (time.perf_counter() - start) * 1000
msg = (f"MV refresh: {elapsed:.0f}ms "
f"(current row count varies with DB state)")
print(f"\n PERF: {msg} (target <2000ms)")
# Soft hard ceiling: 10s
self.assertLess(
elapsed, 10000,
f"MV refresh way over budget: {msg} "
f"(target <2000ms, hard fail >10000ms)")

View File

@@ -0,0 +1,73 @@
from datetime import date
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_bank_rec.services.precedent_lookup import (
find_nearest_precedents, PrecedentMatch,
)
@tagged('post_install', '-at_install')
class TestPrecedentLookup(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create({'name': 'Precedent Lookup Partner'})
self.currency = self.env.ref('base.CAD')
self.company = self.env.company
for amt in [1847.50, 1847.50, 1800.00]:
self.env['fusion.reconcile.precedent'].create({
'company_id': self.company.id,
'partner_id': self.partner.id,
'amount': amt,
'currency_id': self.currency.id,
'date': date.today(),
'memo_tokens': 'RBC,ETF,REF',
'matched_move_line_count': 1,
'source': 'manual',
})
def test_finds_amount_exact_precedents(self):
results = find_nearest_precedents(
self.env, partner_id=self.partner.id, amount=1847.50, k=5)
amounts = [r.amount for r in results]
self.assertEqual(amounts.count(1847.50), 2)
def test_returns_empty_for_unknown_partner(self):
results = find_nearest_precedents(
self.env, partner_id=999999, amount=1847.50, k=5)
self.assertEqual(results, [])
def test_respects_k_limit(self):
for i in range(10):
self.env['fusion.reconcile.precedent'].create({
'company_id': self.company.id,
'partner_id': self.partner.id,
'amount': 1847.50,
'currency_id': self.currency.id,
'date': date.today(),
'matched_move_line_count': 1,
'source': 'manual',
})
results = find_nearest_precedents(
self.env, partner_id=self.partner.id, amount=1847.50, k=3)
self.assertEqual(len(results), 3)
def test_results_sorted_by_similarity_desc(self):
results = find_nearest_precedents(
self.env, partner_id=self.partner.id, amount=1847.50, k=5)
if len(results) >= 2:
self.assertGreaterEqual(results[0].similarity_score, results[1].similarity_score)
def test_memo_overlap_boosts_score(self):
results_with_memo = find_nearest_precedents(
self.env, partner_id=self.partner.id, amount=1847.50, k=5,
memo_tokens=['RBC', 'ETF', 'REF'])
results_no_memo = find_nearest_precedents(
self.env, partner_id=self.partner.id, amount=1847.50, k=5)
if results_with_memo and results_no_memo:
self.assertGreaterEqual(results_with_memo[0].similarity_score,
results_no_memo[0].similarity_score - 0.001)
def test_amount_outside_tolerance_excluded(self):
results = find_nearest_precedents(
self.env, partner_id=self.partner.id, amount=2000.00, k=5)
self.assertEqual(results, [])

View File

@@ -0,0 +1,201 @@
"""Integration tests for the reconcile engine.
These tests use the test factories (_factories.py) to set up realistic
bank-line + invoice scenarios, then call engine methods and assert the
account.partial.reconcile rows produced have the right shape.
Tests cover:
- Simple 1:1 match (bank line == one invoice)
- Partial chain (one bank line < invoice amount)
- Multi-invoice consolidation (one bank line == sum of N invoices)
- Auto-strategy batch (mix of matchable and unmatchable lines)
- Suggest-then-accept flow
- Unreconcile (reverse a reconciliation)
"""
from datetime import date, timedelta
from odoo.tests.common import TransactionCase, tagged
from . import _factories as f
@tagged('post_install', '-at_install', 'integration')
class TestReconcileSimpleMatch(TransactionCase):
"""The most common scenario: 1 bank line matched against 1 invoice exact."""
def test_simple_match_creates_partial_reconcile(self):
bank_line, recv_lines = f.make_reconcileable_pair(self.env, amount=100.00)
result = self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
self.assertGreater(len(result['partial_ids']), 0)
partial = self.env['account.partial.reconcile'].browse(result['partial_ids'])
self.assertAlmostEqual(sum(partial.mapped('amount')), 100.00, places=2)
def test_simple_match_marks_line_reconciled(self):
bank_line, recv_lines = f.make_reconcileable_pair(self.env, amount=250.00)
self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
bank_line.invalidate_recordset(['is_reconciled'])
self.assertTrue(bank_line.is_reconciled)
def test_simple_match_records_precedent(self):
bank_line, recv_lines = f.make_reconcileable_pair(self.env, amount=500.00)
partner = bank_line.partner_id
Precedent = self.env['fusion.reconcile.precedent']
before = Precedent.search_count([('partner_id', '=', partner.id)])
self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
after = Precedent.search_count([('partner_id', '=', partner.id)])
self.assertEqual(after, before + 1, "Engine should record one precedent per reconcile")
@tagged('post_install', '-at_install', 'integration')
class TestReconcilePartialChain(TransactionCase):
"""Bank line amount < invoice amount -> partial reconcile, residual remains."""
def test_partial_reconcile_leaves_residual(self):
partner = self.env['res.partner'].create({'name': 'Partial Partner'})
invoice = f.make_invoice(self.env, partner=partner, amount=300.00)
recv_lines = invoice.line_ids.filtered(
lambda l: l.account_id.account_type == 'asset_receivable')
bank_line = f.make_bank_line(self.env, amount=100.00, partner=partner)
result = self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
self.assertGreater(len(result['partial_ids']), 0)
invoice.invalidate_recordset(['payment_state', 'amount_residual'])
self.assertAlmostEqual(invoice.amount_residual, 200.00, places=2)
@tagged('post_install', '-at_install', 'integration')
class TestReconcileBatch(TransactionCase):
"""Bulk reconcile: mix of matchable and unmatchable lines."""
def test_batch_reconciles_matchable_lines_only(self):
partner = self.env['res.partner'].create({'name': 'Batch Partner'})
# Share one journal/statement to avoid duplicate-code conflicts
# when creating multiple bank lines in the same test transaction.
shared_journal = f.make_bank_journal(self.env, name='Batch Bank', code='BBNK')
shared_statement = f.make_bank_statement(self.env, journal=shared_journal)
pairs = []
for amount in [100.00, 200.00, 300.00]:
invoice = f.make_invoice(self.env, partner=partner, amount=amount)
recv_lines = invoice.line_ids.filtered(
lambda l: l.account_id.account_type == 'asset_receivable')
bank_line = f.make_bank_line(
self.env, statement=shared_statement, amount=amount,
partner=partner)
pairs.append((bank_line, recv_lines))
orphan_line = f.make_bank_line(
self.env, statement=shared_statement, amount=999.99, partner=partner)
all_lines = self.env['account.bank.statement.line'].browse(
[p[0].id for p in pairs] + [orphan_line.id])
result = self.env['fusion.reconcile.engine'].reconcile_batch(
all_lines, strategy='auto')
self.assertEqual(result['reconciled_count'], 3)
self.assertGreaterEqual(result['skipped'], 1)
self.assertEqual(len(result['errors']), 0)
def test_batch_handles_empty_recordset(self):
empty = self.env['account.bank.statement.line']
result = self.env['fusion.reconcile.engine'].reconcile_batch(empty)
self.assertEqual(result['reconciled_count'], 0)
@tagged('post_install', '-at_install', 'integration')
class TestSuggestThenAccept(TransactionCase):
"""Full flow: suggest_matches creates suggestions; accept_suggestion reconciles."""
def test_suggest_then_accept(self):
partner = self.env['res.partner'].create({'name': 'Suggest Then Accept'})
invoice = f.make_invoice(self.env, partner=partner, amount=750.00)
bank_line = f.make_bank_line(self.env, amount=750.00, partner=partner,
memo='Test suggest accept')
suggestions = self.env['fusion.reconcile.engine'].suggest_matches(
bank_line, limit_per_line=3)
self.assertIn(bank_line.id, suggestions)
self.assertGreater(len(suggestions[bank_line.id]), 0,
"Engine should suggest at least one candidate for matching invoice")
top_suggestion_id = suggestions[bank_line.id][0]['id']
sug = self.env['fusion.reconcile.suggestion'].browse(top_suggestion_id)
result = self.env['fusion.reconcile.engine'].accept_suggestion(sug)
self.assertGreater(len(result['partial_ids']), 0)
sug.invalidate_recordset(['state', 'accepted_at', 'accepted_by'])
self.assertEqual(sug.state, 'accepted')
self.assertTrue(sug.accepted_at)
bank_line.invalidate_recordset(['is_reconciled'])
self.assertTrue(bank_line.is_reconciled)
def test_suggest_supersedes_prior_pending(self):
partner = self.env['res.partner'].create({'name': 'Supersede Test'})
bank_line = f.make_bank_line(self.env, amount=100.00, partner=partner)
invoice = f.make_invoice(self.env, partner=partner, amount=100.00)
self.env['fusion.reconcile.engine'].suggest_matches(bank_line)
first_pending = self.env['fusion.reconcile.suggestion'].search([
('statement_line_id', '=', bank_line.id),
('state', '=', 'pending'),
])
self.assertGreater(len(first_pending), 0)
self.env['fusion.reconcile.engine'].suggest_matches(bank_line)
first_pending.invalidate_recordset(['state'])
for s in first_pending:
self.assertEqual(s.state, 'superseded')
@tagged('post_install', '-at_install', 'integration')
class TestUnreconcile(TransactionCase):
"""Reverse a reconciliation."""
def test_unreconcile_removes_partial(self):
bank_line, recv_lines = f.make_reconcileable_pair(self.env, amount=100.00)
result = self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
partials = self.env['account.partial.reconcile'].browse(result['partial_ids'])
self.assertGreater(len(partials), 0)
unrec_result = self.env['fusion.reconcile.engine'].unreconcile(partials)
self.assertGreater(len(unrec_result['unreconciled_line_ids']), 0)
self.assertFalse(partials.exists())
bank_line.invalidate_recordset(['is_reconciled'])
self.assertFalse(bank_line.is_reconciled)
@tagged('post_install', '-at_install', 'integration')
class TestEngineEdgeCases(TransactionCase):
"""Edge cases that came up during engine implementation."""
def test_reconcile_validates_line_exists(self):
from odoo.exceptions import ValidationError
with self.assertRaises(ValidationError):
self.env['fusion.reconcile.engine'].reconcile_one(
self.env['account.bank.statement.line'],
against_lines=self.env['account.move.line'])
def test_already_reconciled_line_skipped_in_batch(self):
partner = self.env['res.partner'].create({'name': 'Already Reconciled'})
bank_line, recv_lines = f.make_reconcileable_pair(
self.env, amount=50.00, partner=partner)
self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=recv_lines)
bank_line.invalidate_recordset(['is_reconciled'])
self.assertTrue(bank_line.is_reconciled)
result = self.env['fusion.reconcile.engine'].reconcile_batch(bank_line)
self.assertGreater(result['skipped'], 0)

View File

@@ -0,0 +1,216 @@
"""Property-based tests for reconcile engine invariants.
Hypothesis generates random input combinations to catch edge cases that
example-based TDD missed. Each test runs N times (default 50 -- bumpable
via @settings)."""
from datetime import date
from hypothesis import HealthCheck, given, settings, strategies as st
from odoo.tests.common import TransactionCase, tagged
from odoo.addons.fusion_accounting_bank_rec.services.matching_strategies import (
AmountExactStrategy,
Candidate,
FIFOStrategy,
MultiInvoiceStrategy,
)
@tagged('post_install', '-at_install', 'property_based')
class TestMatchingStrategyInvariants(TransactionCase):
"""Pure-Python invariants on the matching strategies (no ORM needed).
Faster + more iterations than DB-backed property tests."""
@given(
bank_amount=st.floats(min_value=0.01, max_value=100000.00,
allow_nan=False, allow_infinity=False),
invoice_amounts=st.lists(
st.floats(min_value=0.01, max_value=100000.00,
allow_nan=False, allow_infinity=False),
min_size=1, max_size=10,
),
)
@settings(max_examples=100, deadline=2000,
suppress_health_check=[HealthCheck.function_scoped_fixture])
def test_amount_exact_picks_only_when_amount_matches(
self, bank_amount, invoice_amounts):
"""AmountExactStrategy returns picks IFF some candidate amount matches
bank_amount within tolerance."""
candidates = [
Candidate(id=i, amount=round(amt, 2), partner_id=1, age_days=10)
for i, amt in enumerate(invoice_amounts)
]
bank_amount = round(bank_amount, 2)
result = AmountExactStrategy().match(
bank_amount=bank_amount, candidates=candidates)
has_match = any(
abs(c.amount - bank_amount) < 0.005 for c in candidates)
if has_match:
self.assertEqual(
len(result.picked_ids), 1,
f"bank=${bank_amount} candidates={[c.amount for c in candidates]} "
f"has_match={has_match} -> expected 1 pick, got {result.picked_ids}",
)
self.assertEqual(result.confidence, 1.0)
else:
self.assertEqual(result.picked_ids, [])
@given(
bank_amount=st.floats(min_value=10.00, max_value=10000.00,
allow_nan=False, allow_infinity=False),
invoice_amounts=st.lists(
st.floats(min_value=1.00, max_value=10000.00,
allow_nan=False, allow_infinity=False),
min_size=1, max_size=8,
),
)
@settings(max_examples=100, deadline=2000,
suppress_health_check=[HealthCheck.function_scoped_fixture])
def test_fifo_picks_oldest_first(self, bank_amount, invoice_amounts):
"""FIFOStrategy picks candidates in order of decreasing age_days
(oldest first), stopping when remaining <= 0."""
candidates = [
Candidate(id=i, amount=round(amt, 2), partner_id=1,
age_days=100 - i)
for i, amt in enumerate(invoice_amounts)
]
bank_amount = round(bank_amount, 2)
result = FIFOStrategy().match(
bank_amount=bank_amount, candidates=candidates)
if not candidates:
return
oldest_first_ids = [
c.id for c in sorted(candidates, key=lambda c: -c.age_days)]
self.assertEqual(
result.picked_ids,
oldest_first_ids[:len(result.picked_ids)],
)
picked_sum = sum(
c.amount for c in candidates if c.id in result.picked_ids)
self.assertAlmostEqual(
result.residual, bank_amount - picked_sum, places=2)
@given(
amounts=st.lists(
st.floats(min_value=1.00, max_value=1000.00,
allow_nan=False, allow_infinity=False),
min_size=2, max_size=6,
),
)
@settings(max_examples=50, deadline=2000,
suppress_health_check=[HealthCheck.function_scoped_fixture])
def test_multi_invoice_finds_combination_when_one_exists(self, amounts):
"""If amounts can sum to a target via <=3 elements, MultiInvoiceStrategy
finds SOME valid combination."""
rounded = [round(a, 2) for a in amounts]
candidates = [
Candidate(id=i, amount=amt, partner_id=1, age_days=10)
for i, amt in enumerate(rounded)
]
target = round(rounded[0] + rounded[1], 2)
result = MultiInvoiceStrategy(max_combinations=3).match(
bank_amount=target, candidates=candidates)
if result.picked_ids:
picked_sum = sum(
c.amount for c in candidates if c.id in result.picked_ids)
self.assertAlmostEqual(
picked_sum, target, places=2,
msg=(f"target={target} picks={result.picked_ids} "
f"sum={picked_sum} candidates={rounded}"),
)
@tagged('post_install', '-at_install', 'property_based', 'engine_invariants')
class TestReconcileEngineInvariants(TransactionCase):
"""ORM-backed property tests against the engine.
Slower because each test creates real bank_lines + invoices."""
def setUp(self):
super().setUp()
self.partner = self.env['res.partner'].create(
{'name': 'Engine Property Partner'})
self.journal = self.env['account.journal'].create({
'name': 'Engine Property Bank',
'type': 'bank',
'code': 'EPB',
})
self.receivable_account = self.env['account.account'].search([
('account_type', '=', 'asset_receivable'),
('company_ids', 'in', self.env.company.id),
], limit=1)
if not self.receivable_account:
self.skipTest("No receivable account in chart of accounts")
def _make_bank_line(self, amount):
statement = self.env['account.bank.statement'].create({
'name': f'Test stmt {amount}',
'journal_id': self.journal.id,
})
return self.env['account.bank.statement.line'].create({
'statement_id': statement.id,
'journal_id': self.journal.id,
'date': date.today(),
'payment_ref': f'Test {amount}',
'amount': amount,
'partner_id': self.partner.id,
})
def _make_invoice(self, amount):
product = self.env['product.product'].search(
[('type', '=', 'service')], limit=1)
if not product:
product = self.env['product.product'].create({
'name': 'Property Test Service',
'type': 'service',
})
move = self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': self.partner.id,
'invoice_date': date.today(),
'invoice_line_ids': [(0, 0, {
'product_id': product.id,
'name': 'Property Test',
'quantity': 1,
'price_unit': amount,
})],
})
move.action_post()
return move
@given(amount=st.floats(min_value=10.00, max_value=10000.00,
allow_nan=False, allow_infinity=False))
@settings(max_examples=10, deadline=10000,
suppress_health_check=[HealthCheck.function_scoped_fixture])
def test_invariant_simple_reconcile_balances(self, amount):
"""For any bank_amount = invoice_amount, reconcile_one produces:
- exactly 1 partial reconcile
- amount equal to the bank line amount
- bank line is_reconciled = True"""
amount = round(amount, 2)
bank_line = self._make_bank_line(amount)
invoice = self._make_invoice(amount)
invoice_recv_lines = invoice.line_ids.filtered(
lambda line: line.account_id.account_type == 'asset_receivable')
result = self.env['fusion.reconcile.engine'].reconcile_one(
bank_line, against_lines=invoice_recv_lines)
self.assertGreater(
len(result['partial_ids']), 0,
f"Expected partial_ids non-empty for amount={amount}, got {result}",
)
partials = self.env['account.partial.reconcile'].browse(
result['partial_ids'])
self.assertAlmostEqual(
sum(partials.mapped('amount')), amount, places=2)
bank_line.invalidate_recordset(['is_reconciled'])
self.assertTrue(
bank_line.is_reconciled,
f"is_reconciled expected True after reconcile for amount={amount}",
)

View File

@@ -0,0 +1,348 @@
"""Unit tests for fusion.reconcile.engine — the 6-method public API.
Test layers:
- Layer 1: API surface (registry + method existence)
- Layer 2: unreconcile
- Layer 3: reconcile_one happy path
- Layer 4: accept_suggestion
- Layer 5: suggest_matches
- Layer 6: reconcile_batch
- Layer 7: write_off
Tests share a common setUpClass fixture providing a partner, bank
journal, statement, receivable account, and a small helper to mint a
posted customer invoice + bank statement line at given amounts.
"""
from datetime import date
from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase, tagged
@tagged('post_install', '-at_install')
class TestReconcileEngineBase(TransactionCase):
"""Shared fixtures for engine tests."""
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.engine = cls.env['fusion.reconcile.engine']
cls.company = cls.env.company
cls.currency = cls.company.currency_id
cls.partner = cls.env['res.partner'].create({
'name': 'Engine Test Partner',
})
cls.bank_journal = cls.env['account.journal'].create({
'name': 'Engine Test Bank',
'type': 'bank',
'code': 'ETBK',
'company_id': cls.company.id,
})
cls.sales_journal = cls.env['account.journal'].search([
('type', '=', 'sale'),
('company_id', '=', cls.company.id),
], limit=1)
if not cls.sales_journal:
cls.sales_journal = cls.env['account.journal'].create({
'name': 'Engine Test Sales',
'type': 'sale',
'code': 'ETSAL',
'company_id': cls.company.id,
})
cls.receivable_account = cls.env['account.account'].search([
('account_type', '=', 'asset_receivable'),
('company_ids', 'in', cls.company.id),
], limit=1)
cls.income_account = cls.env['account.account'].search([
('account_type', '=', 'income'),
('company_ids', 'in', cls.company.id),
], limit=1)
cls.expense_account = cls.env['account.account'].search([
('account_type', '=', 'expense'),
('company_ids', 'in', cls.company.id),
], limit=1)
def _make_statement_line(self, amount, *, partner=None, ref='ENGTEST',
line_date=None):
statement = self.env['account.bank.statement'].create({
'name': 'Engine Test Statement',
'journal_id': self.bank_journal.id,
})
return self.env['account.bank.statement.line'].create({
'statement_id': statement.id,
'journal_id': self.bank_journal.id,
'date': line_date or date.today(),
'payment_ref': ref,
'amount': amount,
'partner_id': (partner or self.partner).id,
})
def _make_invoice(self, amount, *, partner=None, inv_date=None):
"""Create + post a customer invoice for the given amount."""
inv = self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': (partner or self.partner).id,
'invoice_date': inv_date or date.today(),
'journal_id': self.sales_journal.id,
'invoice_line_ids': [(0, 0, {
'name': 'Engine test product',
'quantity': 1,
'price_unit': amount,
'account_id': self.income_account.id,
'tax_ids': [(6, 0, [])],
})],
})
inv.action_post()
return inv
def _receivable_line(self, invoice):
return invoice.line_ids.filtered(
lambda line: line.account_id.account_type == 'asset_receivable'
)
# ============================================================
# Layer 1: API surface
# ============================================================
@tagged('post_install', '-at_install')
class TestReconcileEngineApi(TestReconcileEngineBase):
"""Layer 1: the engine class exists in the registry and exposes the
six expected methods."""
def test_engine_in_registry(self):
self.assertIn('fusion.reconcile.engine', self.env.registry)
def test_engine_is_abstract_model(self):
engine = self.env['fusion.reconcile.engine']
self.assertTrue(engine._abstract)
def test_six_public_methods_callable(self):
engine = self.env['fusion.reconcile.engine']
for name in ('reconcile_one', 'reconcile_batch', 'suggest_matches',
'accept_suggestion', 'write_off', 'unreconcile'):
self.assertTrue(callable(getattr(engine, name, None)),
f"engine.{name} must be callable")
def test_reconcile_one_requires_arguments(self):
line = self._make_statement_line(100.0)
with self.assertRaises(ValidationError):
self.engine.reconcile_one(line)
# ============================================================
# Layer 2: unreconcile
# ============================================================
@tagged('post_install', '-at_install')
class TestReconcileEngineUnreconcile(TestReconcileEngineBase):
def test_unreconcile_removes_partial_reconcile(self):
line = self._make_statement_line(100.0)
invoice = self._make_invoice(100.0)
receivable = self._receivable_line(invoice)
result = self.engine.reconcile_one(
line, against_lines=receivable)
self.assertTrue(result['partial_ids'],
"reconcile_one should produce partial_ids")
partials = self.env['account.partial.reconcile'].browse(
result['partial_ids']).exists()
self.assertTrue(partials)
out = self.engine.unreconcile(partials)
self.assertIn('unreconciled_line_ids', out)
self.assertTrue(out['unreconciled_line_ids'])
self.assertFalse(partials.exists(),
"Partials should be deleted after unreconcile")
receivable.invalidate_recordset(['reconciled', 'amount_residual'])
self.assertFalse(receivable.reconciled)
def test_unreconcile_empty_recordset_returns_empty(self):
empty = self.env['account.partial.reconcile']
out = self.engine.unreconcile(empty)
self.assertEqual(out, {'unreconciled_line_ids': []})
# ============================================================
# Layer 3: reconcile_one happy path
# ============================================================
@tagged('post_install', '-at_install')
class TestReconcileEngineReconcileOne(TestReconcileEngineBase):
def test_reconcile_one_simple_invoice_match(self):
line = self._make_statement_line(250.0)
invoice = self._make_invoice(250.0)
receivable = self._receivable_line(invoice)
self.assertFalse(receivable.reconciled)
result = self.engine.reconcile_one(
line, against_lines=receivable)
self.assertIsInstance(result, dict)
self.assertIn('partial_ids', result)
self.assertIn('exchange_diff_move_id', result)
self.assertIn('write_off_move_id', result)
self.assertTrue(result['partial_ids'])
receivable.invalidate_recordset(['reconciled', 'amount_residual'])
self.assertTrue(receivable.reconciled)
self.assertAlmostEqual(receivable.amount_residual, 0.0, places=2)
def test_reconcile_one_creates_precedent(self):
line = self._make_statement_line(125.0, ref='Engine REF#42')
invoice = self._make_invoice(125.0)
receivable = self._receivable_line(invoice)
before = self.env['fusion.reconcile.precedent'].search_count([
('partner_id', '=', self.partner.id),
])
self.engine.reconcile_one(line, against_lines=receivable)
after = self.env['fusion.reconcile.precedent'].search_count([
('partner_id', '=', self.partner.id),
])
self.assertEqual(after, before + 1)
# ============================================================
# Layer 4: accept_suggestion
# ============================================================
@tagged('post_install', '-at_install')
class TestReconcileEngineAcceptSuggestion(TestReconcileEngineBase):
def test_accept_suggestion_reconciles_and_marks_accepted(self):
line = self._make_statement_line(310.0)
invoice = self._make_invoice(310.0)
receivable = self._receivable_line(invoice)
sug = self.env['fusion.reconcile.suggestion'].create({
'company_id': self.company.id,
'statement_line_id': line.id,
'proposed_move_line_ids': [(6, 0, receivable.ids)],
'confidence': 0.97,
'rank': 1,
'reasoning': 'Exact amount match',
'state': 'pending',
})
result = self.engine.accept_suggestion(sug)
self.assertTrue(result['partial_ids'])
self.assertEqual(sug.state, 'accepted')
self.assertTrue(sug.accepted_at)
self.assertEqual(sug.accepted_by, self.env.user)
def test_accept_suggestion_by_id(self):
line = self._make_statement_line(75.0)
invoice = self._make_invoice(75.0)
receivable = self._receivable_line(invoice)
sug = self.env['fusion.reconcile.suggestion'].create({
'company_id': self.company.id,
'statement_line_id': line.id,
'proposed_move_line_ids': [(6, 0, receivable.ids)],
'confidence': 0.91,
'rank': 1,
'reasoning': 'OK',
'state': 'pending',
})
result = self.engine.accept_suggestion(sug.id)
self.assertTrue(result['partial_ids'])
self.assertEqual(sug.state, 'accepted')
# ============================================================
# Layer 5: suggest_matches
# ============================================================
@tagged('post_install', '-at_install')
class TestReconcileEngineSuggestMatches(TestReconcileEngineBase):
def test_suggest_matches_persists_pending_suggestions(self):
line = self._make_statement_line(420.0)
invoice = self._make_invoice(420.0)
# second open invoice for same partner — also a candidate
self._make_invoice(99.0)
out = self.engine.suggest_matches(line)
self.assertIn(line.id, out)
self.assertTrue(out[line.id])
suggestions = self.env['fusion.reconcile.suggestion'].search([
('statement_line_id', '=', line.id),
('state', '=', 'pending'),
])
self.assertTrue(suggestions)
# Top suggestion should reference the matching invoice's receivable
top = max(suggestions, key=lambda s: s.confidence)
receivable = self._receivable_line(invoice)
self.assertIn(receivable.id, top.proposed_move_line_ids.ids)
def test_suggest_matches_supersedes_prior_pending(self):
line = self._make_statement_line(180.0)
self._make_invoice(180.0)
old_sug = self.env['fusion.reconcile.suggestion'].create({
'company_id': self.company.id,
'statement_line_id': line.id,
'confidence': 0.5,
'rank': 1,
'reasoning': 'prior',
'state': 'pending',
})
self.engine.suggest_matches(line)
old_sug.invalidate_recordset(['state'])
self.assertEqual(old_sug.state, 'superseded')
def test_suggest_matches_returns_empty_for_no_candidates(self):
partner = self.env['res.partner'].create({'name': 'Empty Partner'})
line = self._make_statement_line(10.0, partner=partner)
out = self.engine.suggest_matches(line)
self.assertEqual(out, {})
# ============================================================
# Layer 6: reconcile_batch
# ============================================================
@tagged('post_install', '-at_install')
class TestReconcileEngineBatch(TestReconcileEngineBase):
def test_reconcile_batch_auto_strategy_matches_n_lines(self):
amounts = [100.0, 200.0, 333.33]
lines = self.env['account.bank.statement.line']
for amt in amounts:
invoice = self._make_invoice(amt)
self.assertTrue(invoice)
lines |= self._make_statement_line(amt, ref=f'BATCH-{amt}')
result = self.engine.reconcile_batch(lines, strategy='auto')
self.assertEqual(result['reconciled_count'], len(amounts))
self.assertEqual(result['skipped'], 0)
self.assertEqual(result['errors'], [])
def test_reconcile_batch_skips_already_reconciled(self):
line = self._make_statement_line(50.0)
invoice = self._make_invoice(50.0)
receivable = self._receivable_line(invoice)
self.engine.reconcile_one(line, against_lines=receivable)
result = self.engine.reconcile_batch(line, strategy='auto')
self.assertEqual(result['reconciled_count'], 0)
self.assertEqual(result['skipped'], 1)
# ============================================================
# Layer 7: write_off
# ============================================================
@tagged('post_install', '-at_install')
class TestReconcileEngineWriteOff(TestReconcileEngineBase):
def test_write_off_clears_bank_line(self):
line = self._make_statement_line(40.0, ref='Bank fee')
# No invoices exist; write off the whole amount to expense.
result = self.engine.write_off(
line,
account=self.expense_account,
amount=40.0,
label='Bank fees',
)
self.assertIn('write_off_move_id', result)
line.invalidate_recordset(['is_reconciled'])
self.assertTrue(line.is_reconciled)