Make TestCase create and drop its database

and use with_transaction decorator.

issue5263
review20711002
This commit is contained in:
Cédric Krier
2016-02-06 19:12:09 +01:00
parent 27627adeca
commit 7d00c24344
+30 -29
View File
@@ -1,38 +1,47 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import unittest
import doctest
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.transaction import Transaction
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
from trytond.pool import Pool
from trytond.exceptions import UserError
from trytond.modules.company.tests import create_company, set_company
from trytond.modules.account.tests import create_chart
class AccountProductTestCase(ModuleTestCase):
'Test AccountProduct module'
module = 'account_product'
@with_transaction()
def test_account_used(self):
'Test account used'
ProductTemplate = POOL.get('product.template')
ProductCategory = POOL.get('product.category')
Uom = POOL.get('product.uom')
Account = POOL.get('account.account')
pool = Pool()
ProductTemplate = pool.get('product.template')
ProductCategory = pool.get('product.category')
Uom = pool.get('product.uom')
Account = pool.get('account.account')
company = create_company()
with set_company(company):
create_chart(company)
with Transaction().start(DB_NAME, USER, context=CONTEXT):
unit, = Uom.search([
('name', '=', 'Unit'),
])
unit_id = unit.id
account_expense, = Account.search([
('kind', '=', 'expense'),
])
# raise when empty
template = ProductTemplate(
name='test account used',
list_price=Decimal(10),
cost_price=Decimal(3),
default_uom=unit_id,
default_uom=unit.id,
)
template.save()
@@ -41,24 +50,20 @@ class AccountProductTestCase(ModuleTestCase):
with self.assertRaises(UserError):
template.account_expense_used
with Transaction().start(DB_NAME, USER, context=CONTEXT):
account_expense, = Account.search([
('kind', '=', 'expense'),
])
account_expense_id = account_expense.id
# with account
template = ProductTemplate(
name='test account used',
list_price=Decimal(10),
cost_price=Decimal(3),
default_uom=unit_id,
account_expense=account_expense_id,
default_uom=unit.id,
account_expense=account_expense.id,
)
template.save()
self.assertEqual(template.account_expense, account_expense)
self.assertEqual(template.account_expense_used, account_expense)
# with account on category
category = ProductCategory(name='test account used',
account_expense=account_expense)
category.save()
@@ -70,6 +75,7 @@ class AccountProductTestCase(ModuleTestCase):
self.assertIsNone(template.account_expense)
self.assertEqual(template.account_expense_used, account_expense)
# with account on grant category
parent_category = ProductCategory(name='parent account used',
account_expense=account_expense)
parent_category.save()
@@ -82,24 +88,23 @@ class AccountProductTestCase(ModuleTestCase):
self.assertEqual(template.account_expense_used, account_expense)
self.assertEqual(category.account_expense_used, account_expense)
with Transaction().start(DB_NAME, USER, context=CONTEXT):
# raise only at direct usage
templates = ProductTemplate.create([{
'name': 'test with account',
'list_price': Decimal(10),
'cost_price': Decimal(3),
'default_uom': unit_id,
'account_expense': account_expense_id,
'default_uom': unit.id,
'account_expense': account_expense.id,
}, {
'name': 'test without account',
'list_price': Decimal(10),
'cost_price': Decimal(3),
'default_uom': unit_id,
'default_uom': unit.id,
'account_expense': None,
}])
self.assertEqual(templates[0].account_expense_used.id,
account_expense_id)
account_expense.id)
with self.assertRaises(UserError):
templates[1].account_expense_used
@@ -107,10 +112,6 @@ class AccountProductTestCase(ModuleTestCase):
def suite():
suite = trytond.tests.test_tryton.suite()
from trytond.modules.account.tests import test_account
for test in test_account.suite():
if test not in suite and not isinstance(test, doctest.DocTestCase):
suite.addTest(test)
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
AccountProductTestCase))
return suite