Pure-Python helper for FX conversion at report end-date. Handles direct rates, inverse rates, and fallback to most-recent-rate-on-or-before. fetch_rates() pulls from res.currency.rate using the same 1/rate inversion convention Odoo uses internally. Made-with: Cursor
54 lines
2.1 KiB
Python
54 lines
2.1 KiB
Python
"""Unit tests for currency_conversion service."""
|
|
|
|
from datetime import date
|
|
|
|
from odoo.tests.common import TransactionCase, tagged
|
|
from odoo.addons.fusion_accounting_reports.services.currency_conversion import (
|
|
convert_amount, fetch_rates,
|
|
)
|
|
|
|
|
|
@tagged('post_install', '-at_install')
|
|
class TestCurrencyConversion(TransactionCase):
|
|
|
|
def test_same_currency_returns_unchanged(self):
|
|
result = convert_amount(100, source_currency='USD',
|
|
target_currency='USD',
|
|
rate_date=date(2026, 4, 19), rates={})
|
|
self.assertEqual(result, 100)
|
|
|
|
def test_direct_rate(self):
|
|
rates = {('USD', 'CAD', date(2026, 4, 19)): 1.35}
|
|
result = convert_amount(100, source_currency='USD',
|
|
target_currency='CAD',
|
|
rate_date=date(2026, 4, 19), rates=rates)
|
|
self.assertEqual(result, 135)
|
|
|
|
def test_inverse_rate(self):
|
|
rates = {('CAD', 'USD', date(2026, 4, 19)): 0.74}
|
|
result = convert_amount(100, source_currency='USD',
|
|
target_currency='CAD',
|
|
rate_date=date(2026, 4, 19), rates=rates)
|
|
self.assertAlmostEqual(result, 100 / 0.74, places=2)
|
|
|
|
def test_falls_back_to_most_recent_rate(self):
|
|
rates = {
|
|
('USD', 'CAD', date(2026, 1, 1)): 1.30,
|
|
('USD', 'CAD', date(2026, 3, 1)): 1.32,
|
|
}
|
|
result = convert_amount(100, source_currency='USD',
|
|
target_currency='CAD',
|
|
rate_date=date(2026, 4, 19), rates=rates)
|
|
self.assertEqual(result, 132)
|
|
|
|
def test_raises_when_no_rate(self):
|
|
with self.assertRaises(ValueError):
|
|
convert_amount(100, source_currency='EUR',
|
|
target_currency='CAD',
|
|
rate_date=date(2026, 4, 19), rates={})
|
|
|
|
def test_fetch_rates_from_env(self):
|
|
cad = self.env.ref('base.CAD')
|
|
rates = fetch_rates(self.env, target_currency_id=cad.id, as_of=date(2026, 4, 19))
|
|
self.assertIsInstance(rates, dict)
|