From 07214d8a8e27224a09616576d8898788a02e748a Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Thu, 22 Aug 2019 16:52:59 +0200 Subject: [PATCH] [FIX+IMP] account_payment_order: Make tests resilient + inbound inheritable + SavepointCase Previous tests did several risk operations on tests, like performing an unbound search on payment orders, what can lead to errors if there are some data on DB (or for example we add in the future some payment orders in demo. This commit also split the tests for inbound payment in a base inheritable class that can be reused in other dependant modules, and the test of this module. Finally, we use SavepointCase for improving performance in tests. --- .../tests/test_payment_order_inbound.py | 50 +++++++++++-------- .../tests/test_payment_order_outbound.py | 14 ++++-- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/account_payment_order/tests/test_payment_order_inbound.py b/account_payment_order/tests/test_payment_order_inbound.py index fcd25b7f9..45b645a0f 100644 --- a/account_payment_order/tests/test_payment_order_inbound.py +++ b/account_payment_order/tests/test_payment_order_inbound.py @@ -1,17 +1,19 @@ -# © 2017 Camptocamp SA -# © 2017 Creu Blanca +# Copyright 2017 Camptocamp SA +# Copyright 2017 Creu Blanca +# Copyright 2019 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo.tests.common import TransactionCase +from odoo.tests.common import SavepointCase from odoo.exceptions import ValidationError, UserError from datetime import date, timedelta -class TestPaymentOrderInbound(TransactionCase): - - def setUp(self): - super(TestPaymentOrderInbound, self).setUp() - self.inbound_mode = self.browse_ref( +class TestPaymentOrderInboundBase(SavepointCase): + @classmethod + def setUpClass(cls): + self = cls + super().setUpClass() + self.inbound_mode = self.env.ref( 'account_payment_mode.payment_mode_inbound_dd1' ) self.invoice_line_account = self.env['account.account'].search( @@ -22,12 +24,27 @@ class TestPaymentOrderInbound(TransactionCase): [('type', '=', 'bank')], limit=1 ) self.inbound_mode.variable_journal_ids = self.journal + # Make sure no others orders are present + self.domain = [ + ('state', '=', 'draft'), + ('payment_type', '=', 'inbound'), + ] + self.payment_order_obj = self.env['account.payment.order'] + self.payment_order_obj.search(self.domain).unlink() + # Create payment order self.inbound_order = self.env['account.payment.order'].create({ 'payment_type': 'inbound', 'payment_mode_id': self.inbound_mode.id, 'journal_id': self.journal.id, }) - self.invoice = self._create_customer_invoice() + # Open invoice + self.invoice = self._create_customer_invoice(self) + self.invoice.action_invoice_open() + # Add to payment order using the wizard + self.env['account.invoice.payment.line.multi'].with_context( + active_model='account.invoice', + active_ids=self.invoice.ids + ).create({}).run() def _create_customer_invoice(self): invoice_account = self.env['account.account'].search( @@ -40,7 +57,6 @@ class TestPaymentOrderInbound(TransactionCase): 'type': 'out_invoice', 'payment_mode_id': self.inbound_mode.id }) - self.env['account.invoice.line'].create({ 'product_id': self.env.ref('product.product_product_4').id, 'quantity': 1.0, @@ -49,9 +65,10 @@ class TestPaymentOrderInbound(TransactionCase): 'name': 'product that cost 100', 'account_id': self.invoice_line_account, }) - return invoice + +class TestPaymentOrderInbound(TestPaymentOrderInboundBase): def test_constrains_type(self): with self.assertRaises(ValidationError): order = self.env['account.payment.order'].create({ @@ -66,14 +83,7 @@ class TestPaymentOrderInbound(TransactionCase): days=1) def test_creation(self): - # Open invoice - self.invoice.action_invoice_open() - # Add to payment order using the wizard - self.env['account.invoice.payment.line.multi'].with_context( - active_model='account.invoice', - active_ids=self.invoice.ids - ).create({}).run() - payment_order = self.env['account.payment.order'].search([]) + payment_order = self.inbound_order self.assertEqual(len(payment_order.ids), 1) bank_journal = self.env['account.journal'].search( [('type', '=', 'bank')], limit=1) @@ -108,4 +118,4 @@ class TestPaymentOrderInbound(TransactionCase): self.assertEqual(payment_order.state, 'cancel') payment_order.cancel2draft() payment_order.unlink() - self.assertEqual(len(self.env['account.payment.order'].search([])), 0) + self.assertEqual(len(self.payment_order_obj.search(self.domain)), 0) diff --git a/account_payment_order/tests/test_payment_order_outbound.py b/account_payment_order/tests/test_payment_order_outbound.py index 36619c1f7..713396c84 100644 --- a/account_payment_order/tests/test_payment_order_outbound.py +++ b/account_payment_order/tests/test_payment_order_outbound.py @@ -26,6 +26,12 @@ class TestPaymentOrderOutbound(TransactionCase): 'account_payment_mode.payment_mode_outbound_dd1') self.bank_journal = self.env['account.journal'].search( [('type', '=', 'bank')], limit=1) + # Make sure no other payment orders are in the DB + self.domain = [ + ('state', '=', 'draft'), + ('payment_type', '=', 'outbound'), + ] + self.env['account.payment.order'].search(self.domain).unlink() def _create_supplier_invoice(self): invoice_account = self.env['account.account'].search( @@ -136,8 +142,8 @@ class TestPaymentOrderOutbound(TransactionCase): active_ids=self.invoice.ids ).create({}).run() - payment_order = self.env['account.payment.order'].search([]) - self.assertEqual(len(payment_order.ids), 1) + payment_order = self.env['account.payment.order'].search(self.domain) + self.assertEqual(len(payment_order), 1) bank_journal = self.env['account.journal'].search( [('type', '=', 'bank')], limit=1) # Set journal to allow cancelling entries @@ -171,7 +177,9 @@ class TestPaymentOrderOutbound(TransactionCase): self.assertEqual(payment_order.state, 'cancel') payment_order.cancel2draft() payment_order.unlink() - self.assertEqual(len(self.env['account.payment.order'].search([])), 0) + self.assertEqual( + len(self.env['account.payment.order'].search(self.domain)), 0, + ) def test_constrains(self): outbound_order = self.env['account.payment.order'].create({