From 1c7b0aa606fd35ee58d8048ce94a1ffc59876b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 6 Feb 2016 19:11:56 +0100 Subject: [PATCH] Make TestCase create and drop its database and use with_transaction decorator. issue5263 review20711002 --- tests/__init__.py | 4 +- tests/test_account_invoice.py | 146 ++++++++++++++++++---------------- 2 files changed, 79 insertions(+), 71 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index b2f4e54be5..fcab20999e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,6 +1,6 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. -from .test_account_invoice import suite +from .test_account_invoice import suite, set_invoice_sequences -__all__ = ['suite'] +__all__ = ['suite', 'set_invoice_sequences'] diff --git a/tests/test_account_invoice.py b/tests/test_account_invoice.py index f0d7759105..9cc8661804 100644 --- a/tests/test_account_invoice.py +++ b/tests/test_account_invoice.py @@ -5,84 +5,92 @@ import doctest import datetime from decimal import Decimal import trytond.tests.test_tryton -from trytond.tests.test_tryton import ModuleTestCase -from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT +from trytond.tests.test_tryton import ModuleTestCase, with_transaction from trytond.tests.test_tryton import doctest_setup, doctest_teardown -from trytond.transaction import Transaction +from trytond.pool import Pool + +from trytond.modules.currency.tests import create_currency + + +def set_invoice_sequences(fiscalyear): + pool = Pool() + Sequence = pool.get('ir.sequence.strict') + + sequence = Sequence(name=fiscalyear.name, code='account.invoice') + sequence.company = fiscalyear.company + sequence.save() + for type_ in ['out', 'in']: + for invoice in ['invoice', 'credit_note']: + name = '%s_%s_sequence' % (type_, invoice) + setattr(fiscalyear, name, sequence) + return fiscalyear class AccountInvoiceTestCase(ModuleTestCase): 'Test AccountInvoice module' module = 'account_invoice' - def setUp(self): - super(AccountInvoiceTestCase, self).setUp() - self.payment_term = POOL.get('account.invoice.payment_term') - self.currency = POOL.get('currency.currency') - - def test0010payment_term(self): + @with_transaction() + def test_payment_term(self): 'Test payment_term' - with Transaction().start(DB_NAME, USER, context=CONTEXT): - cu1, = self.currency.create([{ - 'name': 'cu1', - 'symbol': 'cu1', - 'code': 'cu1' - }]) + pool = Pool() + PaymentTerm = pool.get('account.invoice.payment_term') - term, = self.payment_term.create([{ - 'name': '30 days, 1 month, 1 month + 15 days', - 'lines': [ - ('create', [{ - 'sequence': 0, - 'type': 'percent', - 'divisor': 4, - 'ratio': Decimal('.25'), - 'relativedeltas': [('create', [{ - 'days': 30, - }, - ]), - ], - }, { - 'sequence': 1, - 'type': 'percent_on_total', - 'divisor': 4, - 'ratio': Decimal('.25'), - 'relativedeltas': [('create', [{ - 'months': 1, - }, - ]), - ], - }, { - 'sequence': 2, - 'type': 'fixed', - 'amount': Decimal('396.84'), - 'currency': cu1.id, - 'relativedeltas': [('create', [{ - 'months': 1, - 'days': 30, - }, - ]), - ], - }, { - 'sequence': 3, - 'type': 'remainder', - 'relativedeltas': [('create', [{ - 'months': 2, - 'days': 30, - 'day': 15, - }, - ]), - ], - }])] - }]) - terms = term.compute(Decimal('1587.35'), cu1, - date=datetime.date(2011, 10, 1)) - self.assertEqual(terms, [ - (datetime.date(2011, 10, 31), Decimal('396.84')), - (datetime.date(2011, 11, 01), Decimal('396.84')), - (datetime.date(2011, 12, 01), Decimal('396.84')), - (datetime.date(2012, 01, 14), Decimal('396.83')), - ]) + cu1 = create_currency('cu1') + term, = PaymentTerm.create([{ + 'name': '30 days, 1 month, 1 month + 15 days', + 'lines': [ + ('create', [{ + 'sequence': 0, + 'type': 'percent', + 'divisor': 4, + 'ratio': Decimal('.25'), + 'relativedeltas': [('create', [{ + 'days': 30, + }, + ]), + ], + }, { + 'sequence': 1, + 'type': 'percent_on_total', + 'divisor': 4, + 'ratio': Decimal('.25'), + 'relativedeltas': [('create', [{ + 'months': 1, + }, + ]), + ], + }, { + 'sequence': 2, + 'type': 'fixed', + 'amount': Decimal('396.84'), + 'currency': cu1.id, + 'relativedeltas': [('create', [{ + 'months': 1, + 'days': 30, + }, + ]), + ], + }, { + 'sequence': 3, + 'type': 'remainder', + 'relativedeltas': [('create', [{ + 'months': 2, + 'days': 30, + 'day': 15, + }, + ]), + ], + }])] + }]) + terms = term.compute(Decimal('1587.35'), cu1, + date=datetime.date(2011, 10, 1)) + self.assertEqual(terms, [ + (datetime.date(2011, 10, 31), Decimal('396.84')), + (datetime.date(2011, 11, 01), Decimal('396.84')), + (datetime.date(2011, 12, 01), Decimal('396.84')), + (datetime.date(2012, 01, 14), Decimal('396.83')), + ]) def suite():