diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index a7a0df7c8c9..7c89038e691 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -398,6 +398,13 @@ class AccountChartTemplate(models.AbstractModel): if model in data: data[model] = data.pop(model) + # Remove data of unknown fields present in the company template + company_data = data.get('res.company') + if company_data and not self.env.context.get('l10n_check_fields_complete'): + for fname in list(company_data.get(company.id)): + if fname not in company._fields: + del data['res.company'][company.id][fname] + return data def _load_data(self, data): diff --git a/addons/account/tests/test_account_all_l10n.py b/addons/account/tests/test_account_all_l10n.py index 1fc08c895ef..03db87135c8 100644 --- a/addons/account/tests/test_account_all_l10n.py +++ b/addons/account/tests/test_account_all_l10n.py @@ -2,6 +2,8 @@ import logging from odoo.tests import standalone +from odoo.addons.account.models.chart_template import AccountChartTemplate +from unittest.mock import patch _logger = logging.getLogger(__name__) @@ -13,6 +15,14 @@ def test_all_l10n(env): As the module install is not yet fully transactional, the modules will remain installed after the test. """ + + try_loading = type(env['account.chart.template']).try_loading + + def try_loading_patch(self, template_code, company, install_demo=True): + self = self.with_context(l10n_check_fields_complete=True) + return try_loading(self, template_code, company, install_demo) + + # Ensure the presence of demo data, to see if they can be correctly installed assert env.ref('base.module_account').demo, "Need the demo to test with data" @@ -21,7 +31,17 @@ def test_all_l10n(env): ('name', '=like', 'l10n%'), ('state', '=', 'uninstalled'), ]) - l10n_mods.button_immediate_install() + with patch.object(AccountChartTemplate, 'try_loading', try_loading_patch): + l10n_mods.button_immediate_install() + + # In all_l10n tests we need to verify demo data + demo_failures = env['ir.demo_failure'].search([]) + if demo_failures: + _logger.warning("Error while testing demo data for all_l10n tests.") + for failure in demo_failures: + _logger.warning("Demo data of module %s has failed: %s", + failure.module_id.name, failure.error) + env.reset() # clear the set of environments env = env() # get an environment that refers to the new registry @@ -47,6 +67,6 @@ def test_all_l10n(env): _logger.info('Testing COA: %s (company: %s)', template_code, company.name) try: with env.cr.savepoint(): - env['account.chart.template'].try_loading(template_code, company, install_demo=True) + env['account.chart.template'].with_context(l10n_check_fields_complete=True).try_loading(template_code, company, install_demo=True) except Exception: _logger.error("Error when creating COA %s", template_code, exc_info=True) diff --git a/addons/account/tests/test_chart_template.py b/addons/account/tests/test_chart_template.py index e1d2feea92f..cd37c32980b 100644 --- a/addons/account/tests/test_chart_template.py +++ b/addons/account/tests/test_chart_template.py @@ -462,3 +462,27 @@ class TestChartTemplate(TransactionCase): for model in TEMPLATE_MODELS + sub_models: data_after = self.env[model].search(get_domain(model)) self.assertEqual(data_before[model], data_after) + + def test_unknown_company_fields(self): + """ Tests that if a key is not known in the company template data when the + context value 'l10n_check_fields_complete' is set, an error is raised. If a + key is not known in the company template data but the context value is not + set, that key is skipped and no error is raised.""" + + def local_get_data(self, template_code): + data = test_get_data(self, template_code) + data['res.company'][company.id]['unknown_company_key'] = 'unknown_company_value' + return data + + company = self.company_1 + + with patch.object(AccountChartTemplate, '_get_chart_template_data', side_effect=local_get_data, autospec=True): + # hard fail the loading if the context key is set to ensure `test_all_l10n` works as expected + with ( + self.assertRaisesRegex(ValueError, 'unknown_company_key'), + self.env.cr.savepoint(), + ): + self.env['account.chart.template'].with_context(l10n_check_fields_complete=True).try_loading('test', company=company, install_demo=False) + + # silently ignore if the field doesn't exist (yet) + self.env['account.chart.template'].try_loading('test', company=company, install_demo=False)