From c1f1ec9706fa5091a5eef90eb10408e4b8cca95b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marques?= Date: Mon, 1 Mar 2021 10:47:13 +0000 Subject: [PATCH] [FIX] account_financial_report: Refactor tests Use core account test base, helping to ensure we have the correct accounting environment setup. If any of the localization modules are loaded, this will make this tests be skipped, instead of simply failing TT28423 --- .../tests/test_general_ledger.py | 53 ++--- .../tests/test_journal_ledger.py | 142 +++++--------- .../tests/test_open_items.py | 9 +- .../tests/test_trial_balance.py | 90 ++++----- .../tests/test_vat_report.py | 181 ++++++++++-------- 5 files changed, 212 insertions(+), 263 deletions(-) diff --git a/account_financial_report/tests/test_general_ledger.py b/account_financial_report/tests/test_general_ledger.py index 16ca64fd..b864a3bc 100644 --- a/account_financial_report/tests/test_general_ledger.py +++ b/account_financial_report/tests/test_general_ledger.py @@ -7,45 +7,34 @@ import time from datetime import date from odoo import api, fields -from odoo.tests import common - -class TestGeneralLedgerReport(common.TransactionCase): - def setUp(self): - super(TestGeneralLedgerReport, self).setUp() - self.env.user.company_id = self.env.ref("base.main_company").id - self.before_previous_fy_year = fields.Date.from_string("2014-05-05") - self.previous_fy_date_start = fields.Date.from_string("2015-01-01") - self.previous_fy_date_end = fields.Date.from_string("2015-12-31") - self.fy_date_start = fields.Date.from_string("2016-01-01") - self.fy_date_end = fields.Date.from_string("2016-12-31") - - self.receivable_account = self.env["account.account"].search( - [ - ("user_type_id.name", "=", "Receivable"), - ("company_id", "=", self.env.user.company_id.id), - ], - limit=1, - ) - self.income_account = self.env["account.account"].search( - [ - ("user_type_id.name", "=", "Income"), - ("company_id", "=", self.env.user.company_id.id), - ], - limit=1, - ) - self.unaffected_account = self.env["account.account"].search( +from odoo.addons.account.tests.common import AccountTestInvoicingCommon + + +class TestGeneralLedgerReport(AccountTestInvoicingCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + cls.before_previous_fy_year = fields.Date.from_string("2014-05-05") + cls.previous_fy_date_start = fields.Date.from_string("2015-01-01") + cls.previous_fy_date_end = fields.Date.from_string("2015-12-31") + cls.fy_date_start = fields.Date.from_string("2016-01-01") + cls.fy_date_end = fields.Date.from_string("2016-12-31") + # Get accounts + cls.receivable_account = cls.company_data["default_account_receivable"] + cls.income_account = cls.company_data["default_account_revenue"] + cls.unaffected_account = cls.env["account.account"].search( [ ( "user_type_id", "=", - self.env.ref("account.data_unaffected_earnings").id, + cls.env.ref("account.data_unaffected_earnings").id, ), - ("company_id", "=", self.env.user.company_id.id), + ("company_id", "=", cls.env.user.company_id.id), ], limit=1, ) - self.partner = self.env.ref("base.res_partner_12") + cls.partner = cls.env.ref("base.res_partner_12") def _add_move( self, @@ -104,7 +93,7 @@ class TestGeneralLedgerReport(common.TransactionCase): centralize = True if with_partners: centralize = False - company = self.env.ref("base.main_company") + company = self.env.user.company_id general_ledger = self.env["general.ledger.report.wizard"].create( { "date_from": self.fy_date_start, @@ -685,7 +674,7 @@ class TestGeneralLedgerReport(common.TransactionCase): self.assertEqual(wizard._default_partners(), expected_list) def test_validate_date(self): - company_id = self.env.ref("base.main_company") + company_id = self.env.user.company_id company_id.write({"fiscalyear_last_day": 31, "fiscalyear_last_month": "12"}) user = self.env.ref("base.user_root").with_context(company_id=company_id.id) wizard = self.env["general.ledger.report.wizard"].with_context(user=user.id) diff --git a/account_financial_report/tests/test_journal_ledger.py b/account_financial_report/tests/test_journal_ledger.py index d1549462..5ebd3abb 100644 --- a/account_financial_report/tests/test_journal_ledger.py +++ b/account_financial_report/tests/test_journal_ledger.py @@ -7,93 +7,46 @@ from datetime import datetime from dateutil.relativedelta import relativedelta from odoo.fields import Date -from odoo.tests.common import Form, TransactionCase - - -class TestJournalReport(TransactionCase): - def setUp(self): - super(TestJournalReport, self).setUp() - self.env.user.company_id = self.env.ref("base.main_company").id - self.AccountObj = self.env["account.account"] - self.InvoiceObj = self.env["account.move"] - self.JournalObj = self.env["account.journal"] - self.MoveObj = self.env["account.move"] - self.TaxObj = self.env["account.tax"] - - self.JournalLedgerReportWizard = self.env["journal.ledger.report.wizard"] - self.JournalLedgerReport = self.env[ +from odoo.tests.common import Form + +from odoo.addons.account.tests.common import AccountTestInvoicingCommon + + +class TestJournalReport(AccountTestInvoicingCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + cls.AccountObj = cls.env["account.account"] + cls.InvoiceObj = cls.env["account.move"] + cls.JournalObj = cls.env["account.journal"] + cls.MoveObj = cls.env["account.move"] + cls.TaxObj = cls.env["account.tax"] + cls.JournalLedgerReportWizard = cls.env["journal.ledger.report.wizard"] + cls.JournalLedgerReport = cls.env[ "report.account_financial_report.journal_ledger" ] - self.company = self.env.ref("base.main_company") - self.company.account_sale_tax_id = False - self.company.account_purchase_tax_id = False - + cls.company = cls.company_data["company"] + cls.company.account_sale_tax_id = False + cls.company.account_purchase_tax_id = False today = datetime.today() last_year = today - relativedelta(years=1) - - self.previous_fy_date_start = Date.to_string(last_year.replace(month=1, day=1)) - self.previous_fy_date_end = Date.to_string(last_year.replace(month=12, day=31)) - self.fy_date_start = Date.to_string(today.replace(month=1, day=1)) - self.fy_date_end = Date.to_string(today.replace(month=12, day=31)) - - self.receivable_account = self.AccountObj.search( - [ - ("user_type_id.name", "=", "Receivable"), - ("company_id", "=", self.env.user.company_id.id), - ], - limit=1, - ) - self.income_account = self.AccountObj.search( - [ - ("user_type_id.name", "=", "Income"), - ("company_id", "=", self.env.user.company_id.id), - ], - limit=1, - ) - self.expense_account = self.AccountObj.search( - [ - ("user_type_id.name", "=", "Expenses"), - ("company_id", "=", self.env.user.company_id.id), - ], - limit=1, - ) - self.payable_account = self.AccountObj.search( - [ - ("user_type_id.name", "=", "Payable"), - ("company_id", "=", self.env.user.company_id.id), - ], - limit=1, - ) - - self.journal_sale = self.JournalObj.create( - { - "name": "Test journal sale", - "code": "TST-JRNL-S", - "type": "sale", - "company_id": self.company.id, - } - ) - self.journal_purchase = self.JournalObj.create( - { - "name": "Test journal purchase", - "code": "TST-JRNL-P", - "type": "purchase", - "company_id": self.company.id, - } - ) - - self.tax_15_s = self.TaxObj.create( - { - "sequence": 30, - "name": "Tax 15.0% (Percentage of Price)", - "amount": 15.0, - "amount_type": "percent", - "include_base_amount": False, - "type_tax_use": "sale", - } - ) - - self.tax_20_s = self.TaxObj.create( + cls.previous_fy_date_start = Date.to_string(last_year.replace(month=1, day=1)) + cls.previous_fy_date_end = Date.to_string(last_year.replace(month=12, day=31)) + cls.fy_date_start = Date.to_string(today.replace(month=1, day=1)) + cls.fy_date_end = Date.to_string(today.replace(month=12, day=31)) + cls.receivable_account = cls.company_data["default_account_receivable"] + cls.income_account = cls.company_data["default_account_revenue"] + cls.expense_account = cls.company_data["default_account_expense"] + cls.payable_account = cls.company_data["default_account_payable"] + cls.journal_sale = cls.company_data["default_journal_sale"] + cls.journal_purchase = cls.company_data["default_journal_purchase"] + cls.tax_15_s = cls.company_data["default_tax_sale"] + cls.tax_15_s.sequence = 30 + cls.tax_15_s.amount = 15.0 + cls.tax_15_s.amount_type = "percent" + cls.tax_15_s.include_base_amount = False + cls.tax_15_s.type_tax_use = "sale" + cls.tax_20_s = cls.tax_15_s.copy( { "sequence": 30, "name": "Tax 20.0% (Percentage of Price)", @@ -103,19 +56,13 @@ class TestJournalReport(TransactionCase): "type_tax_use": "sale", } ) - - self.tax_15_p = self.TaxObj.create( - { - "sequence": 30, - "name": "Tax 15.0% (Percentage of Price)", - "amount": 15.0, - "amount_type": "percent", - "include_base_amount": False, - "type_tax_use": "purchase", - } - ) - - self.tax_20_p = self.TaxObj.create( + cls.tax_15_p = cls.company_data["default_tax_purchase"] + cls.tax_15_p.sequence = 30 + cls.tax_15_p.amount = 15.0 + cls.tax_15_p.amount_type = "percent" + cls.tax_15_p.include_base_amount = False + cls.tax_15_p.type_tax_use = "purchase" + cls.tax_20_p = cls.tax_15_p.copy( { "sequence": 30, "name": "Tax 20.0% (Percentage of Price)", @@ -125,8 +72,7 @@ class TestJournalReport(TransactionCase): "type_tax_use": "purchase", } ) - - self.partner_2 = self.env.ref("base.res_partner_2") + cls.partner_2 = cls.env.ref("base.res_partner_2") def _add_move( self, diff --git a/account_financial_report/tests/test_open_items.py b/account_financial_report/tests/test_open_items.py index 5ac93fef..5d9901ad 100644 --- a/account_financial_report/tests/test_open_items.py +++ b/account_financial_report/tests/test_open_items.py @@ -2,12 +2,15 @@ # Copyright 2016 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo.tests.common import TransactionCase +from odoo.addons.account.tests.common import AccountTestInvoicingCommon -class TestOpenItems(TransactionCase): +class TestOpenItems(AccountTestInvoicingCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + def test_partner_filter(self): - self.env.user.company_id = self.env.ref("base.main_company").id partner_1 = self.env.ref("base.res_partner_1") partner_2 = self.env.ref("base.res_partner_2") partner_3 = self.env.ref("base.res_partner_3") diff --git a/account_financial_report/tests/test_trial_balance.py b/account_financial_report/tests/test_trial_balance.py index 13bd5f3d..e9c2b173 100644 --- a/account_financial_report/tests/test_trial_balance.py +++ b/account_financial_report/tests/test_trial_balance.py @@ -3,85 +3,79 @@ # Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo.tests import common +from odoo.addons.account.tests.common import AccountTestInvoicingCommon -class TestTrialBalanceReport(common.TransactionCase): - def setUp(self): - super(TestTrialBalanceReport, self).setUp() - self.env.user.company_id = self.env.ref("base.main_company").id - group_obj = self.env["account.group"] +class TestTrialBalanceReport(AccountTestInvoicingCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) # Remove previous account groups and related invoices to avoid conflicts - group_obj.search([("code_prefix_start", "in", ["1", "2", "11"])]).unlink() - self.group1 = group_obj.create({"code_prefix_start": "1", "name": "Group 1"}) - self.group11 = group_obj.create( - {"code_prefix_start": "11", "name": "Group 11", "parent_id": self.group1.id} - ) - self.group2 = group_obj.create({"code_prefix_start": "2", "name": "Group 2"}) - self.account100 = self._create_account_account( - { - "code": "100", - "name": "Account 100", - "group_id": self.group1.id, - "user_type_id": self.env.ref("account.data_account_type_receivable").id, - "reconcile": True, - } - ) - self.account110 = self.env["account.account"].search( + group_obj = cls.env["account.group"] + cls.group1 = group_obj.create({"code_prefix_start": "1", "name": "Group 1"}) + cls.group11 = group_obj.create( + {"code_prefix_start": "11", "name": "Group 11", "parent_id": cls.group1.id} + ) + cls.group2 = group_obj.create({"code_prefix_start": "2", "name": "Group 2"}) + # Set accounts + cls.account100 = cls.company_data["default_account_receivable"] + cls.account100.group_id = cls.group1.id + cls.account110 = cls.env["account.account"].search( [ ( "user_type_id", "=", - self.env.ref("account.data_unaffected_earnings").id, + cls.env.ref("account.data_unaffected_earnings").id, ), - ("company_id", "=", self.env.user.company_id.id), ], limit=1, ) - self.account200 = self._create_account_account( + cls.account200 = cls._create_account_account( + cls, { "code": "200", "name": "Account 200", - "group_id": self.group2.id, - "user_type_id": self.env.ref( + "group_id": cls.group2.id, + "user_type_id": cls.env.ref( "account.data_account_type_other_income" ).id, - } + }, ) - self.account300 = self._create_account_account( + cls.account300 = cls._create_account_account( + cls, { "code": "300", "name": "Account 300", - "user_type_id": self.env.ref( + "user_type_id": cls.env.ref( "account.data_account_type_other_income" ).id, - } + }, ) - self.account301 = self._create_account_account( + cls.account301 = cls._create_account_account( + cls, { "code": "301", "name": "Account 301", - "group_id": self.group2.id, - "user_type_id": self.env.ref( + "group_id": cls.group2.id, + "user_type_id": cls.env.ref( "account.data_account_type_other_income" ).id, - } - ) - self.previous_fy_date_start = "2015-01-01" - self.previous_fy_date_end = "2015-12-31" - self.fy_date_start = "2016-01-01" - self.fy_date_end = "2016-12-31" - self.date_start = "2016-01-01" - self.date_end = "2016-12-31" - self.partner = self.env.ref("base.res_partner_12") - self.unaffected_account = self.env["account.account"].search( + }, + ) + cls.previous_fy_date_start = "2015-01-01" + cls.previous_fy_date_end = "2015-12-31" + cls.fy_date_start = "2016-01-01" + cls.fy_date_end = "2016-12-31" + cls.date_start = "2016-01-01" + cls.date_end = "2016-12-31" + cls.partner = cls.env.ref("base.res_partner_12") + cls.unaffected_account = cls.env["account.account"].search( [ ( "user_type_id", "=", - self.env.ref("account.data_unaffected_earnings").id, + cls.env.ref("account.data_unaffected_earnings").id, ), - ("company_id", "=", self.env.user.company_id.id), ], limit=1, ) @@ -166,7 +160,7 @@ class TestTrialBalanceReport(common.TransactionCase): move.action_post() def _get_report_lines(self, with_partners=False, hierarchy_on="computed"): - company = self.env.ref("base.main_company") + company = self.env.user.company_id trial_balance = self.env["trial.balance.report.wizard"].create( { "date_from": self.date_start, @@ -694,7 +688,7 @@ class TestTrialBalanceReport(common.TransactionCase): move = self.env["account.move"].create(move_vals) move.action_post() # Generate the trial balance line - company = self.env.ref("base.main_company") + company = self.env.user.company_id trial_balance = self.env["trial.balance.report.wizard"].create( { "date_from": self.date_start, diff --git a/account_financial_report/tests/test_vat_report.py b/account_financial_report/tests/test_vat_report.py index ef3965ec..629f0c78 100644 --- a/account_financial_report/tests/test_vat_report.py +++ b/account_financial_report/tests/test_vat_report.py @@ -5,81 +5,103 @@ import time from datetime import date -from odoo.tests import common +from odoo import fields +from odoo.tests.common import Form +from odoo.addons.account.tests.common import AccountTestInvoicingCommon -class TestVATReport(common.TransactionCase): - def setUp(self): - super(TestVATReport, self).setUp() - self.env.user.company_id = self.env.ref("base.main_company").id - self.date_from = time.strftime("%Y-%m-01") - self.date_to = time.strftime("%Y-%m-28") - self.company = self.env.ref("base.main_company") - self.receivable_account = self.env["account.account"].search( - [ - ("company_id", "=", self.company.id), - ("user_type_id.name", "=", "Receivable"), - ], - limit=1, - ) - self.income_account = self.env["account.account"].search( - [ - ("company_id", "=", self.company.id), - ("user_type_id.name", "=", "Income"), - ], - limit=1, - ) - self.tax_account = self.env["account.account"].search( + +class TestVATReport(AccountTestInvoicingCommon): + @classmethod + def init_invoice( + cls, + move_type, + name=None, + partner=None, + invoice_date=None, + post=False, + lines=None, + taxes=None, + ): + move_form = Form( + cls.env["account.move"].with_context(default_move_type=move_type) + ) + move_form.invoice_date = invoice_date or fields.Date.from_string("2019-01-01") + move_form.partner_id = partner or cls.partner_a + move_form.name = name or "Test" + lines = lines or [] + for line in lines: + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = line[0] + line_form.name = "Test" + line_form.account_id = line[1] + line_form.quantity = line[2] + line_form.price_unit = line[3] + if taxes: + line_form.tax_ids.clear() + line_form.tax_ids.add(taxes) + rslt = move_form.save() + if post: + rslt.action_post() + return rslt + + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + cls.date_from = time.strftime("%Y-%m-01") + cls.date_to = time.strftime("%Y-%m-28") + cls.company = cls.env.user.company_id + cls.company.country_id = cls.env.ref("base.us").id + cls.receivable_account = cls.company_data["default_account_receivable"] + cls.income_account = cls.company_data["default_account_revenue"] + cls.expense_account = cls.company_data["default_account_expense"] + cls.tax_account = cls.env["account.account"].search( [ - ("company_id", "=", self.company.id), + ("company_id", "=", cls.company.id), ( "user_type_id", "=", - self.env.ref( - "account.data_account_type_non_current_liabilities" - ).id, + cls.env.ref("account.data_account_type_non_current_liabilities").id, ), ], limit=1, ) - self.bank_journal = self.env["account.journal"].search( - [("type", "=", "bank"), ("company_id", "=", self.company.id)], limit=1 - ) - self.tax_tag_01 = self.env["account.account.tag"].create( + cls.bank_journal = cls.company_data["default_journal_bank"] + cls.tax_tag_01 = cls.env["account.account.tag"].create( { "name": "Tag 01", "applicability": "taxes", - "country_id": self.company.country_id.id, + "country_id": cls.company.country_id.id, } ) - self.tax_tag_02 = self.env["account.account.tag"].create( + cls.tax_tag_02 = cls.env["account.account.tag"].create( { "name": "Tag 02", "applicability": "taxes", - "country_id": self.company.country_id.id, + "country_id": cls.company.country_id.id, } ) - self.tax_tag_03 = self.env["account.account.tag"].create( + cls.tax_tag_03 = cls.env["account.account.tag"].create( { "name": "Tag 03", "applicability": "taxes", - "country_id": self.company.country_id.id, + "country_id": cls.company.country_id.id, } ) - self.tax_group_10 = self.env["account.tax.group"].create( + cls.tax_group_10 = cls.env["account.tax.group"].create( {"name": "Tax 10%", "sequence": 1} ) - self.tax_group_20 = self.env["account.tax.group"].create( + cls.tax_group_20 = cls.env["account.tax.group"].create( {"name": "Tax 20%", "sequence": 2} ) - self.tax_10 = self.env["account.tax"].create( + cls.tax_10 = cls.env["account.tax"].create( { "name": "Tax 10.0%", "amount": 10.0, "amount_type": "percent", "type_tax_use": "sale", - "company_id": self.company.id, - "tax_group_id": self.tax_group_10.id, + "company_id": cls.company.id, + "tax_group_id": cls.tax_group_10.id, "invoice_repartition_line_ids": [ (0, 0, {"factor_percent": 100, "repartition_type": "base"}), ( @@ -88,10 +110,8 @@ class TestVATReport(common.TransactionCase): { "factor_percent": 100, "repartition_type": "tax", - "account_id": self.tax_account.id, - "tag_ids": [ - (6, 0, [self.tax_tag_01.id, self.tax_tag_02.id]) - ], + "account_id": cls.tax_account.id, + "tag_ids": [(6, 0, [cls.tax_tag_01.id, cls.tax_tag_02.id])], }, ), ], @@ -103,22 +123,22 @@ class TestVATReport(common.TransactionCase): { "factor_percent": 100, "repartition_type": "tax", - "account_id": self.tax_account.id, + "account_id": cls.tax_account.id, }, ), ], } ) - self.tax_20 = self.env["account.tax"].create( + cls.tax_20 = cls.env["account.tax"].create( { "sequence": 30, "name": "Tax 20.0%", "amount": 20.0, "amount_type": "percent", "type_tax_use": "sale", - "company_id": self.company.id, - "cash_basis_transition_account_id": self.tax_account.id, - "tax_group_id": self.tax_group_20.id, + "company_id": cls.company.id, + "cash_basis_transition_account_id": cls.tax_account.id, + "tax_group_id": cls.tax_group_20.id, "invoice_repartition_line_ids": [ (0, 0, {"factor_percent": 100, "repartition_type": "base"}), ( @@ -127,10 +147,8 @@ class TestVATReport(common.TransactionCase): { "factor_percent": 100, "repartition_type": "tax", - "account_id": self.tax_account.id, - "tag_ids": [ - (6, 0, [self.tax_tag_02.id, self.tax_tag_03.id]) - ], + "account_id": cls.tax_account.id, + "tag_ids": [(6, 0, [cls.tax_tag_02.id, cls.tax_tag_03.id])], }, ), ], @@ -142,40 +160,39 @@ class TestVATReport(common.TransactionCase): { "factor_percent": 100, "repartition_type": "tax", - "account_id": self.tax_account.id, + "account_id": cls.tax_account.id, }, ), ], } ) - - move_form = common.Form( - self.env["account.move"].with_context(default_move_type="out_invoice") - ) - move_form.partner_id = self.env.ref("base.res_partner_2") - move_form.invoice_date = time.strftime("%Y-%m-03") - with move_form.invoice_line_ids.new() as line_form: - line_form.product_id = self.env.ref("product.product_product_4") - line_form.quantity = 1.0 - line_form.price_unit = 100.0 - line_form.account_id = self.income_account - line_form.tax_ids.add(self.tax_10) - invoice = move_form.save() - invoice.action_post() - - move_form = common.Form( - self.env["account.move"].with_context(default_move_type="out_invoice") + cls.init_invoice( + "out_invoice", + name="Test invoice 1", + partner=cls.env.ref("base.res_partner_2"), + invoice_date=time.strftime("%Y-%m-03"), + post=True, + lines=[ + (cls.env.ref("product.product_product_4"), cls.income_account, 1, 100.0) + ], + taxes=cls.tax_10, + ) + cls.init_invoice( + "out_invoice", + name="Test invoice 2", + partner=cls.env.ref("base.res_partner_2"), + invoice_date=time.strftime("%Y-%m-04"), + post=True, + lines=[ + ( + cls.env.ref("product.product_product_4"), + cls.income_account, + 1, + 250.0, + ), + ], + taxes=cls.tax_20, ) - move_form.partner_id = self.env.ref("base.res_partner_2") - move_form.invoice_date = time.strftime("%Y-%m-04") - with move_form.invoice_line_ids.new() as line_form: - line_form.product_id = self.env.ref("product.product_product_4") - line_form.quantity = 1.0 - line_form.price_unit = 250.0 - line_form.account_id = self.income_account - line_form.tax_ids.add(self.tax_20) - invoice = move_form.save() - invoice.action_post() def _get_report_lines(self, taxgroups=False): based_on = "taxtags"