trytond-party_company/tests/test_module.py

80 lines
2.5 KiB
Python
Raw Permalink Normal View History

# 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 contextlib import contextmanager
2017-05-25 15:59:45 +02:00
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
from trytond.transaction import Transaction
2021-02-10 13:23:16 +01:00
from trytond.pool import Pool
from trytond.modules.company.tests import CompanyTestMixin, create_company
@contextmanager
def set_company(company):
pool = Pool()
User = pool.get('res.user')
User.write([User(Transaction().user)], {
2021-04-26 14:59:56 +02:00
'companies': [('add', [company.id])],
'company': company.id,
})
with Transaction().set_context(User.get_preferences(context_only=True)):
yield
2017-05-25 15:59:45 +02:00
class PartyCompanyTestCase(CompanyTestMixin, ModuleTestCase):
'Test PartyCompany module'
2017-05-25 15:59:45 +02:00
module = 'party_company'
extras = ['bank']
2017-05-25 15:59:45 +02:00
2017-05-26 13:46:50 +02:00
@with_transaction()
def test_party(self):
'Create party'
pool = Pool()
Party = pool.get('party.party')
party1, = Party.create([{
'name': 'Party 1',
}])
2018-08-17 18:51:03 +02:00
self.assertTrue(party1.id)
self.assertEqual(party1.companies, ())
2017-05-26 13:46:50 +02:00
@with_transaction()
def test_party_company(self):
'Create party company'
pool = Pool()
Party = pool.get('party.party')
Address = pool.get('party.address')
User = pool.get('res.user')
2017-05-26 13:46:50 +02:00
company = create_company()
with set_company(company):
party = Party()
party.name = 'Party 2'
party.companies = [company]
party.save()
2018-08-17 18:51:03 +02:00
self.assertTrue(party.id)
self.assertEqual(len(party.companies), 1)
address, = Address.create([{
'party': party.id,
'street': 'St sample, 15',
'city': 'City',
}])
self.assertEqual(address.companies == (company,), True)
2017-05-26 13:46:50 +02:00
address1, address2 = Address.search([])
self.assertEqual(address1.companies, ())
self.assertEqual(address2.companies == (company,), True)
2017-05-26 13:46:50 +02:00
user = User(Transaction().user)
2021-04-26 14:59:56 +02:00
self.assertEqual(len(user.companies) == 1, True)
self.assertEqual(user.companies[0] == company, True)
company2 = create_company()
with set_company(company2):
user = User(Transaction().user)
2021-04-26 14:59:56 +02:00
self.assertEqual(len(user.companies) == 2, True)
self.assertEqual(user.companies[1] == company2, True)
del ModuleTestCase