From 5648cc9aa489d5ddf26d65e6e6cf4ca07f3fb964 Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Tue, 23 Aug 2022 16:31:23 +0000 Subject: [PATCH] [FIX] base, l10n_*: set currency with existing lines Since #96791 the currency is reset to USD when starting a test to be less dependant of demo data when starting tests. Unfortunatelly, some l10n_modules will add account.move.line demo data leading to an error: You cannot change the currency of the company since some journal items already exist An initial solution would be to delete the existing account.move.line cls.env['account.move.line'].search([('company_id', '=', company.id)]).unlink() This is only possible passing some context flags in order to disable some checks existing = cls.env['account.move.line'].search([('company_id', '=', company.id)]) existing = existing.with_context(dynamic_unlink=True, force_delete=True) existing.unlink() Actually, unlinking account.move.line also creates other ones. existing = cls.env['account.move.line'].search([('company_id', '=', company.id)]) existing = existing.with_context(dynamic_unlink=True, force_delete=True) existing.unlink() existing = cls.env['account.move.line'].search([('company_id', '=', company.id)]) existing = existing.with_context(dynamic_unlink=True, force_delete=True) existing.unlink() Finally, it looks easier to bypass all business logic leading to the proposed solution. closes odoo/odoo#98736 Signed-off-by: Victor Feyens (vfe) --- addons/product/tests/test_common.py | 2 +- odoo/addons/base/tests/common.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/addons/product/tests/test_common.py b/addons/product/tests/test_common.py index 723e1ff6fae..245910ac309 100644 --- a/addons/product/tests/test_common.py +++ b/addons/product/tests/test_common.py @@ -7,7 +7,7 @@ from odoo.addons.product.tests.common import ProductCommon @tagged('post_install', '-at_install') -class TestProductCommon(ProductCommon): +class TestProduct(ProductCommon): def test_common(self): self.assertEqual(self.consumable_product.type, 'consu') diff --git a/odoo/addons/base/tests/common.py b/odoo/addons/base/tests/common.py index f5b57da1003..67608368a7a 100644 --- a/odoo/addons/base/tests/common.py +++ b/odoo/addons/base/tests/common.py @@ -39,7 +39,11 @@ class BaseCommon(TransactionCase): # Enforce constant currency currency = cls._enable_currency(currency_code) if not cls.env.company.currency_id == currency: - cls.env.company.currency_id = currency + cls.env.transaction.cache.set(cls.env.company, type(cls.env.company).currency_id, currency.id, dirty=True) + # this is equivalent to cls.env.company.currency_id = currency but without triggering buisness code checks. + # The value is added in cache, and the cache value is set as dirty so that that + # the value will be written to the database on next flush. + # this was needed because some journal entries may exist when running tests, especially l10n demo data. @classmethod def _enable_currency(cls, currency_code):