Created
January 18, 2014 13:04
-
-
Save vishesh/8490226 to your computer and use it in GitHub Desktop.
Test to demonstrate failing sale https://bugs.tryton.org/issue3602
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """ | |
| test_sale | |
| Test Sale | |
| :copyright: (c) 2013-2014 by Openlabs Technologies & Consulting (P) Limited | |
| :license: BSD, see LICENSE for more details. | |
| """ | |
| import unittest | |
| from decimal import Decimal | |
| import trytond.tests.test_tryton | |
| from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT | |
| from trytond.transaction import Transaction | |
| from trytond.pyson import Eval | |
| class TestSale(unittest.TestCase): | |
| ''' | |
| Test sale | |
| ''' | |
| def setUp(self): | |
| """ | |
| Set up data used in the tests. | |
| this method is called before each test function execution. | |
| """ | |
| trytond.tests.test_tryton.install_module('sale_price_list') | |
| trytond.tests.test_tryton.install_module('sale_shipment_cost') | |
| self.Party = POOL.get('party.party') | |
| self.Address = POOL.get('party.address') | |
| self.Company = POOL.get('company.company') | |
| self.Currency = POOL.get('currency.currency') | |
| def _create_coa_minimal(self, company): | |
| """Create a minimal chart of accounts | |
| """ | |
| AccountTemplate = POOL.get('account.account.template') | |
| Account = POOL.get('account.account') | |
| account_create_chart = POOL.get( | |
| 'account.create_chart', type="wizard") | |
| account_template, = AccountTemplate.search( | |
| [('parent', '=', None)] | |
| ) | |
| session_id, _, _ = account_create_chart.create() | |
| create_chart = account_create_chart(session_id) | |
| create_chart.account.account_template = account_template | |
| create_chart.account.company = company | |
| create_chart.transition_create_account() | |
| receivable, = Account.search([ | |
| ('kind', '=', 'receivable'), | |
| ('company', '=', company), | |
| ]) | |
| payable, = Account.search([ | |
| ('kind', '=', 'payable'), | |
| ('company', '=', company), | |
| ]) | |
| create_chart.properties.company = company | |
| create_chart.properties.account_receivable = receivable | |
| create_chart.properties.account_payable = payable | |
| create_chart.transition_create_properties() | |
| def _get_account_by_kind(self, kind, company=None, silent=True): | |
| """Returns an account with given spec | |
| :param kind: receivable/payable/expense/revenue | |
| :param silent: dont raise error if account is not found | |
| """ | |
| Account = POOL.get('account.account') | |
| Company = POOL.get('company.company') | |
| if company is None: | |
| company, = Company.search([], limit=1) | |
| accounts = Account.search([ | |
| ('kind', '=', kind), | |
| ('company', '=', company) | |
| ], limit=1) | |
| if not accounts and not silent: | |
| raise Exception("Account not found") | |
| return accounts[0] if accounts else False | |
| def _create_payment_term(self): | |
| """Create a simple payment term with all advance | |
| """ | |
| PaymentTerm = POOL.get('account.invoice.payment_term') | |
| return PaymentTerm.create([{ | |
| 'name': 'Direct', | |
| 'lines': [('create', [{'type': 'remainder'}])] | |
| }]) | |
| def create_defaults(self): | |
| "Create defaults" | |
| Template = POOL.get('product.template') | |
| Product = POOL.get('product.product') | |
| Uom = POOL.get('product.uom') | |
| PriceList = POOL.get('product.price_list') | |
| User = POOL.get('res.user') | |
| with Transaction().set_context({'company': None}): | |
| party, = self.Party.create([{ | |
| 'name': 'testuser', | |
| }]) | |
| self.assert_(party.id) | |
| self.assertEqual(len(party.addresses), 1) | |
| Currency = POOL.get('currency.currency') | |
| currency = Currency( | |
| name='Euro', symbol=u'€', code='EUR', | |
| rounding=Decimal('0.01'), mon_grouping='[3, 3, 0]', | |
| mon_decimal_point=',' | |
| ) | |
| currency.save() | |
| company, = self.Company.create([{ | |
| 'party': party.id, | |
| 'currency': currency | |
| }]) | |
| company2, = self.Company.create([{ | |
| 'party': party.id, | |
| 'currency': currency | |
| }]) | |
| User.write( | |
| [User(USER)], { | |
| 'main_company': company.id, | |
| 'company': company.id, | |
| } | |
| ) | |
| self._create_coa_minimal(company=company.id) | |
| account_revenue = self._get_account_by_kind('revenue') | |
| uom, = Uom.search([('name', '=', 'Unit')]) | |
| template1, = Template.create([{ | |
| 'name': 'product', | |
| 'type': 'goods', | |
| 'list_price': Decimal('20'), | |
| 'cost_price': Decimal('5'), | |
| 'default_uom': uom, | |
| 'salable': True, | |
| 'sale_uom': uom.id, | |
| 'account_revenue': account_revenue.id, | |
| }]) | |
| product1, = Product.create([{ | |
| 'template': template1.id, | |
| }]) | |
| price_list, = PriceList.create([{ | |
| 'name': 'Price List', | |
| 'company': company.id, | |
| 'lines': [ | |
| ('create', [ | |
| { | |
| 'price_list': Eval('id'), | |
| 'product': product1.id, | |
| 'sequence': 1, | |
| 'quantity': Decimal(1.0), | |
| 'formula': 'unit_price * 100' | |
| }, | |
| ]) | |
| ] | |
| }]) | |
| return { | |
| 'party': party, | |
| 'company': company, | |
| 'currency': currency, | |
| 'price_list': price_list, | |
| 'uom': uom, | |
| 'product1': product1, | |
| 'payment_term': self._create_payment_term()[0] | |
| } | |
| def test0010saleline(self): | |
| ''' | |
| Test sale line with simple values | |
| ''' | |
| Sale = POOL.get('sale.sale') | |
| with Transaction().start(DB_NAME, USER, context=CONTEXT): | |
| defaults = self.create_defaults() | |
| sale, = Sale.create([{ | |
| 'payment_term': defaults['payment_term'], | |
| 'currency': defaults['currency'].id, | |
| 'party': defaults['party'].id, | |
| 'company': defaults['company'].id, | |
| 'price_list': defaults['price_list'], | |
| 'lines': [('create', [{ | |
| 'sequence': 1, | |
| 'type': 'line', | |
| 'quantity': Decimal(2.0), | |
| 'unit': defaults['uom'].id, | |
| 'unit_price': Decimal(10.0), | |
| 'description': 'Hello World!', | |
| }])] | |
| }]) | |
| saleline, = sale.lines | |
| def suite(): | |
| """ | |
| Define suite | |
| """ | |
| test_suite = trytond.tests.test_tryton.suite() | |
| test_suite.addTests( | |
| unittest.TestLoader().loadTestsFromTestCase(TestSale) | |
| ) | |
| return test_suite | |
| if __name__ == '__main__': | |
| unittest.TextTestRunner(verbosity=2).run(suite()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment