From 940435482633a29ecbc467e00eaba42020bf1af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Tue, 25 Feb 2025 12:47:32 +0100 Subject: [PATCH] [FIX] account_statement_import_sheet_file: Post-install test + fallback to load CoA Since odoo/odoo@d0342c8, the default existing company is not getting a CoA automatically, provoking than the current tests fail with the error: odoo.exceptions.UserError: No journal could be found in company My Company (San Francisco) for any of those types: sale Thus, we put tests post-install for being sure localization modules are installed, the same as AccountTestInvoicingCommon does, but we don't inherit from it, as it creates an overhead creating 2 new companies and loading their CoA and some more stuff, while we don't need all of that. Besides, if you don't have `l10n_generic_coa` installed, you can't use another CoA (like `l10n_es`) easily, so we put little code to select the first available CoA. --- ...est_account_statement_import_sheet_file.py | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/account_statement_import_sheet_file/tests/test_account_statement_import_sheet_file.py b/account_statement_import_sheet_file/tests/test_account_statement_import_sheet_file.py index de38578d0..9918d1d4f 100644 --- a/account_statement_import_sheet_file/tests/test_account_statement_import_sheet_file.py +++ b/account_statement_import_sheet_file/tests/test_account_statement_import_sheet_file.py @@ -9,50 +9,60 @@ from odoo import fields from odoo.exceptions import UserError -from odoo.tests import common +from odoo.tests import common, tagged from odoo.tools import float_round +@tagged("post_install", "-at_install") class TestAccountStatementImportSheetFile(common.TransactionCase): - def setUp(self): - super().setUp() - - self.now = fields.Datetime.now() - self.currency_eur = self.env.ref("base.EUR") - self.currency_usd = self.env.ref("base.USD") - self.currency_usd.active = True + @classmethod + def setUpClass(cls): + super().setUpClass() + if not cls.env.company.chart_template_id: + # Load a CoA if there's none in current company + coa = cls.env.ref("l10n_generic_coa.configurable_chart_template", False) + if not coa: + # Load the first available CoA + coa = cls.env["account.chart.template"].search( + [("visible", "=", True)], limit=1 + ) + coa.try_loading(company=cls.env.company, install_demo=False) + cls.now = fields.Datetime.now() + cls.currency_eur = cls.env.ref("base.EUR") + cls.currency_usd = cls.env.ref("base.USD") + cls.currency_usd.active = True # Make sure the currency of the company is USD, as this not always happens # To be removed in V17: https://github.com/odoo/odoo/pull/107113 - self.company = self.env.company - self.env.cr.execute( + cls.company = cls.env.company + cls.env.cr.execute( "UPDATE res_company SET currency_id = %s WHERE id = %s", - (self.env.ref("base.USD").id, self.company.id), + (cls.env.ref("base.USD").id, cls.company.id), ) # Activate EUR for unit test, by default is not active - self.currency_eur.active = True - self.sample_statement_map = self.env.ref( + cls.currency_eur.active = True + cls.sample_statement_map = cls.env.ref( "account_statement_import_sheet_file.sample_statement_map" ) - self.AccountJournal = self.env["account.journal"] - self.AccountBankStatement = self.env["account.bank.statement"] - self.AccountStatementImport = self.env["account.statement.import"] - self.AccountStatementImportSheetMapping = self.env[ + cls.AccountJournal = cls.env["account.journal"] + cls.AccountBankStatement = cls.env["account.bank.statement"] + cls.AccountStatementImport = cls.env["account.statement.import"] + cls.AccountStatementImportSheetMapping = cls.env[ "account.statement.import.sheet.mapping" ] - self.AccountStatementImportWizard = self.env["account.statement.import"] - self.suspense_account = self.env["account.account"].create( + cls.AccountStatementImportWizard = cls.env["account.statement.import"] + cls.suspense_account = cls.env["account.account"].create( { "code": "987654", "name": "Suspense Account", "account_type": "asset_current", } ) - self.parser = self.env["account.statement.import.sheet.parser"] + cls.parser = cls.env["account.statement.import.sheet.parser"] # Mock the mapping object to return predefined separators - self.mock_mapping_comma_dot = Mock() - self.mock_mapping_comma_dot._get_float_separators.return_value = (",", ".") - self.mock_mapping_dot_comma = Mock() - self.mock_mapping_dot_comma._get_float_separators.return_value = (".", ",") + cls.mock_mapping_comma_dot = Mock() + cls.mock_mapping_comma_dot._get_float_separators.return_value = (",", ".") + cls.mock_mapping_dot_comma = Mock() + cls.mock_mapping_dot_comma._get_float_separators.return_value = (".", ",") def _data_file(self, filename, encoding=None): mode = "rt" if encoding else "rb"