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

125 lines
5.5 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.
import datetime as dt
from decimal import Decimal
2016-02-06 19:13:01 +01:00
from trytond.modules.account.tests import create_chart
2021-12-11 17:58:43 +01:00
from trytond.modules.company.tests import (
CompanyTestMixin, PartyCompanyCheckEraseMixin, create_company, set_company)
from trytond.pool import Pool
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
2021-12-11 17:58:43 +01:00
from trytond.transaction import Transaction
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')
Purchase = pool.get('purchase.purchase')
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),
'purchasable': True,
'products': [('create', [{
'cost_price': Decimal(3),
}])],
}])
product, = template.products
supplier, = Party.create([{
'name': 'Supplier',
'account_receivable': receivable.id,
'account_payable': payable.id,
'addresses': [('create', [{}])],
}])
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),
}])],
}])
purchase, = Purchase.create([{
'party': supplier.id,
'invoice_address': supplier.addresses[0].id,
'purchase_date': dt.date.today(),
'lines': [('create', [{
'product': product.id,
'quantity': 10,
'unit': kg.id,
'unit_price': Decimal(2000),
}])],
}])
purchase.state = 'confirmed'
purchase.save()
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
del ModuleTestCase