Commit c80c5658 authored by Julien Muchembled's avatar Julien Muchembled

Fix testComplexTradeModelLineUseCase

git-svn-id: https://svn.erp5.org/repos/public/erp5/sandbox/amount_generator@39069 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e2075796
......@@ -57,17 +57,8 @@ class BaseAmount(dict):
def getContext(self):
return self._context
def updateCache(self, base_amount_set, amount_generator_line):
cache = self._cache
base_amount_set = base_amount_set.difference(cache)
if base_amount_set:
method = amount_generator_line._getTypeBasedMethod(
'getBaseAmountQuantityMethod')
for base_amount in base_amount_set:
if method is None:
cache[base_amount] = amount_generator_line.getBaseAmountQuantity
else:
cache[base_amount] = method(base_amount)
def setAmountGeneratorLine(self, amount_generator_line):
self._amount_generator_line = amount_generator_line
def recurse(self, portal_type=None):
for amount in self._context.objectValues(portal_type=portal_type):
......@@ -92,7 +83,9 @@ class BaseAmount(dict):
return dict.__getitem__(self, key)
except KeyError:
value = 0
amount_generator_line = self._amount_generator_line
for lazy in self._lazy:
lazy._amount_generator_line = amount_generator_line
value += lazy.getQuantity(key)
self[key] = value
return value
......@@ -109,7 +102,17 @@ class BaseAmount(dict):
return dict.__getitem__(self, key)
self[key] # initialize entry before we freeze it
self._frozen.add(key)
self[key] = value = self._cache[key](self._context, key, **self._method_kw)
try:
method = self._cache[key]
except KeyError:
method = self._amount_generator_line._getTypeBasedMethod(
'getBaseAmountQuantityMethod')
if method is not None:
method = method(key)
if method is None:
method = self._amount_generator_line.getBaseAmountQuantity
self._cache[key] = method
self[key] = value = method(self._context, key, **self._method_kw)
return value
......@@ -241,11 +244,9 @@ class AmountGeneratorMixin:
cell.getBaseContributionList())
property_dict['causality_value_list'].append(cell)
base_amount.setAmountGeneratorLine(self)
for property_dict in cell_aggregate.itervalues():
base_application_set = property_dict['base_application_set']
# Cache must be prepared with the right context in case that we iterate
# through different kinds of amount generator lines.
base_amount.updateCache(base_application_set, self)
# property_dict may include
# resource - VAT service or a Component in MRP
# (if unset, the amount will only be used for reporting)
......@@ -293,13 +294,10 @@ class AmountGeneratorMixin:
amount = getRoundingProxy(amount, context=self)
result.append(amount)
# Contribute
base_contribution_set = property_dict['base_contribution_set']
if base_contribution_set:
quantity *= (property_dict.get('price') or 1) / \
(property_dict.get('efficiency') or 1)
base_amount.updateCache(base_contribution_set, self)
for base_contribution in base_contribution_set:
base_amount[base_contribution] += quantity
quantity *= (property_dict.get('price') or 1) / \
(property_dict.get('efficiency') or 1)
for base_contribution in property_dict['base_contribution_set']:
base_amount[base_contribution] += quantity
is_mapped_value = isinstance(self, MappedValue)
......
......@@ -60,7 +60,7 @@ class TestBPMMixin(ERP5TypeTestCase):
def createCategories(self):
category_tool = self.portal.portal_categories
self.createCategoriesInCategory(category_tool.base_amount, ['discount',
'tax', 'total_tax', 'total_discount', 'total', 'fixed_quantity'])
'tax', 'total_tax', 'total_discount', 'total'])
self.createCategoriesInCategory(category_tool.use,
self.normal_resource_use_category_list + \
self.invoicing_resource_use_category_list)
......
......@@ -56,6 +56,8 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
"""Provides methods to implementations sharing similar logic to Trade Model Lines"""
# Constants and variables shared by tests
base_unit_quantity = 0.01
node_portal_type = 'Organisation'
order_date = DateTime()
def setBaseAmountQuantityMethod(self, base_amount_id, text):
"""Populate TradeModelLine_getBaseAmountQuantityMethod shared script
......@@ -65,10 +67,11 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
- data produced by previous still behaves as expected
"""
base_amount = self.portal.portal_categories.base_amount
try:
base_amount = base_amount[self._testMethodName]
except KeyError:
base_amount = base_amount.newContent(self._testMethodName)
for name in self.__class__.__name__, self._testMethodName:
try:
base_amount = base_amount[name]
except KeyError:
base_amount = base_amount.newContent(name)
try:
return base_amount[base_amount_id].getRelativeUrl()
except KeyError:
......@@ -79,7 +82,7 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
try:
old_text = '\n' + skin[script_id].body()
except KeyError:
old_text = "\nreturn context.getBaseAmountQuantity"
old_text = ''
else:
skin._delObject(script_id)
text = test + '\n '.join(text.splitlines()) + old_text
......@@ -176,19 +179,29 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
order.newContent(portal_type=self.order_line_portal_type, **line_kw)
return order
def getAggregatedAmountDict(self, amount_generator, **expected_amount_dict):
def getAggregatedAmountDict(self, amount_generator, partial_check=False,
**expected_amount_dict):
amount_list = amount_generator.getAggregatedAmountList()
amount_dict = {}
for amount in amount_list:
reference = amount.getReference()
expected_amount = expected_amount_dict.pop(reference)
for k, v in expected_amount.iteritems():
if k == 'causality_value_list':
self.assertEqual(v, amount.getValueList('causality'))
else:
self.assertEqual(v, amount.getProperty(k))
amount_dict[reference] = amount
self.assertEqual({}, expected_amount_dict)
try:
expected_amount = expected_amount_dict.pop(reference)
except KeyError:
if not partial_check:
raise
else:
for k, v in expected_amount.iteritems():
if k == 'causality_value_list':
self.assertEqual(v, amount.getValueList('causality'))
else:
self.assertEqual(v, amount.getProperty(k))
amount_dict[reference] = amount
if partial_check:
for value in expected_amount_dict.itervalues():
self.assertEqual(None, value)
else:
self.assertEqual({}, expected_amount_dict)
return amount_dict
def getTradeModelSimulationMovementList(self, delivery_line):
......@@ -220,9 +233,6 @@ class TestTradeModelLine(TestTradeModelLineMixin):
new_discount_ratio = -0.04 # -4%
new_tax_ratio = 0.22 # 22%
node_portal_type = 'Organisation'
order_date = DateTime()
modified_order_line_price_ratio = 2.0
modified_packing_list_line_quantity_ratio = 0.4
modified_invoice_line_quantity_ratio = modified_order_line_quantity_ratio \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment