Files
tryton/modules/purchase/tests/test_purchase.py
T

137 lines
6.1 KiB
Python
Raw Normal View History

2014-12-02 12:55:47 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2009-06-28 20:20:38 +02:00
import unittest
2012-09-30 21:19:45 +02:00
import doctest
from decimal import Decimal
2009-06-28 20:20:38 +02:00
import trytond.tests.test_tryton
2016-02-06 19:13:01 +01:00
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
2016-08-26 09:59:31 +02:00
from trytond.tests.test_tryton import doctest_teardown, doctest_checker
from trytond.transaction import Transaction
2016-02-06 19:13:01 +01:00
from trytond.pool import Pool
2020-12-05 11:53:48 +01:00
from trytond.modules.company.tests import (
create_company, set_company, PartyCompanyCheckEraseMixin, CompanyTestMixin)
2016-02-06 19:13:01 +01:00
from trytond.modules.account.tests import create_chart
2009-06-28 20:20:38 +02:00
class PurchaseTestCase(
PartyCompanyCheckEraseMixin, CompanyTestMixin, ModuleTestCase):
2014-01-12 20:00:01 +01:00
'Test Purchase module'
2015-01-27 18:13:55 +01:00
module = 'purchase'
2016-02-06 19:13:01 +01:00
@with_transaction()
def test_purchase_price(self):
'Test purchase price'
2016-02-06 19:13:01 +01:00
pool = Pool()
Account = pool.get('account.account')
Template = pool.get('product.template')
Product = pool.get('product.product')
Uom = pool.get('product.uom')
ProductSupplier = pool.get('purchase.product_supplier')
Party = pool.get('party.party')
2016-02-06 19:13:01 +01:00
company = create_company()
with set_company(company):
create_chart(company)
receivable, = Account.search([
2019-03-24 18:00:01 +01:00
('type.receivable', '=', True),
('company', '=', company.id),
])
payable, = Account.search([
2019-03-24 18:00:01 +01:00
('type.payable', '=', True),
('company', '=', company.id),
])
kg, = Uom.search([('name', '=', 'Kilogram')])
g, = Uom.search([('name', '=', 'Gram')])
template, = Template.create([{
'name': 'Product',
'default_uom': g.id,
'purchase_uom': kg.id,
'list_price': Decimal(5),
'products': [('create', [{
'cost_price': Decimal(2),
}])],
}])
product, = template.products
supplier, = Party.create([{
'name': 'Supplier',
'account_receivable': receivable.id,
'account_payable': payable.id,
}])
product_supplier, = ProductSupplier.create([{
'template': template.id,
'party': supplier.id,
'prices': [('create', [{
'sequence': 1,
'quantity': 1,
'unit_price': Decimal(3000),
}, {
'sequence': 2,
'quantity': 2,
'unit_price': Decimal(2500),
}])],
}])
prices = Product.get_purchase_price([product], quantity=100)
self.assertEqual(prices, {product.id: Decimal(2)})
prices = Product.get_purchase_price([product], quantity=1500)
self.assertEqual(prices, {product.id: Decimal(2)})
with Transaction().set_context(uom=kg.id):
prices = Product.get_purchase_price([product], quantity=0.5)
self.assertEqual(prices, {product.id: Decimal(2000)})
prices = Product.get_purchase_price([product], quantity=1.5)
self.assertEqual(prices, {product.id: Decimal(2000)})
with Transaction().set_context(supplier=supplier.id):
prices = Product.get_purchase_price([product], quantity=100)
self.assertEqual(prices, {product.id: Decimal(2)})
prices = Product.get_purchase_price([product], quantity=1500)
self.assertEqual(prices, {product.id: Decimal(3)})
prices = Product.get_purchase_price([product], quantity=3000)
self.assertEqual(prices, {product.id: Decimal('2.5')})
with Transaction().set_context(uom=kg.id, supplier=supplier.id):
prices = Product.get_purchase_price([product], quantity=0.5)
self.assertEqual(prices, {product.id: Decimal(2000)})
prices = Product.get_purchase_price([product], quantity=1.5)
self.assertEqual(prices, {product.id: Decimal(3000)})
prices = Product.get_purchase_price([product], quantity=3)
self.assertEqual(prices, {product.id: Decimal(2500)})
prices = Product.get_purchase_price([product], quantity=-4)
self.assertEqual(prices, {product.id: Decimal(2500)})
2012-04-03 20:07:31 +02:00
2009-06-28 20:20:38 +02:00
def suite():
2010-03-04 14:12:21 +01:00
suite = trytond.tests.test_tryton.suite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
PurchaseTestCase))
2012-09-30 21:19:45 +02:00
suite.addTests(doctest.DocFileSuite('scenario_purchase.rst',
2016-08-26 09:59:31 +02:00
tearDown=doctest_teardown, encoding='UTF-8',
2016-03-04 00:34:21 +01:00
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
checker=doctest_checker))
2017-07-26 23:23:58 +02:00
suite.addTests(doctest.DocFileSuite('scenario_purchase_empty.rst',
tearDown=doctest_teardown, encoding='UTF-8',
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
checker=doctest_checker))
suite.addTests(doctest.DocFileSuite(
'scenario_purchase_modify_header.rst',
tearDown=doctest_teardown, encoding='utf-8',
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
checker=doctest_checker))
2020-04-12 09:15:06 +02:00
suite.addTests(doctest.DocFileSuite(
'scenario_purchase_return_wizard.rst',
tearDown=doctest_teardown, encoding='utf-8',
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
checker=doctest_checker))
2020-05-01 20:32:01 +02:00
suite.addTests(doctest.DocFileSuite(
'scenario_purchase_copy_product_suppliers.rst',
tearDown=doctest_teardown, encoding='utf-8',
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
checker=doctest_checker))
2010-03-04 14:12:21 +01:00
return suite