Commit 57c609da authored by Arnaud Fontaine's avatar Arnaud Fontaine

py3: TestTradeModelLineMixin inherited from UserDict() to store values on the...

py3: TestTradeModelLineMixin inherited from UserDict() to store values on the class direcly (!1751).

This does not work with py3:
    File "parts/erp5/Products/ERP5Type/tests/runUnitTest.py", line 941, in main
      result = runUnitTestList(test_list=args,
    File "parts/erp5/Products/ERP5Type/tests/runUnitTest.py", line 703, in runUnitTestList
      result = TestRunner(verbosity=verbosity).run(suite)
    [...]
    File "parts/python3/lib/python3.9/unittest/runner.py", line 184, in run
      test(result)
    [...]
    File "parts/python3/lib/python3.9/unittest/suite.py", line 84, in __call__
      return self.run(*args, **kwds)
    File "parts/python3/lib/python3.9/unittest/suite.py", line 111, in run
      if _isnotsuite(test):
    File "parts/python3/lib/python3.9/unittest/suite.py", line 369, in _isnotsuite
      iter(test)
    File "parts/python3/lib/python3.9/collections/__init__.py", line 1067, in __iter__
      return iter(self.data)
  AttributeError: 'TestComplexTradeModelLineUseCaseSale' object has no attribute 'data'

Use a dedicated dict() to store these values as there was no strong reasons to
inherit from UserDict() here and this makes test implementation easier too...
parent 1f369453
......@@ -54,7 +54,7 @@ class TestSimulationPerformance(TestTradeModelLineSale):
def perf_00_setupAndFillCache(self):
self.test_01_OrderWithSimpleTaxedAndDiscountedLines()
self.__class__._order = self['order'].getRelativeUrl()
self.__class__._order = self._storage['order'].getRelativeUrl()
self.runAlarms()
def perf_01_invoiceSimpleOrder(self, order_count=1):
......
......@@ -29,7 +29,6 @@
##############################################################################
import six
from six.moves import UserDict
import functools
import random
import unittest
......@@ -48,13 +47,13 @@ def save_result_as(name):
def decorator(function):
def wrapper(self, *args, **kw):
result = function(self, *args, **kw)
self[name] = result
self._storage[name] = result
return result
return wrapper
return decorator
class TestTradeModelLineMixin(TestBPMMixin, UserDict):
class TestTradeModelLineMixin(TestBPMMixin):
"""Provides methods to implementations sharing similar logic to Trade Model Lines"""
# Constants and variables shared by tests
base_unit_quantity = 0.01
......@@ -62,15 +61,6 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
order_date = DateTime()
amount_generator_line_portal_type = 'Trade Model Line'
# XXX-six.PY3: So that unittest.suite._isnotsuite returns False. This test
# inherits from `UserDict` so that the test can use dict() interface as
# "storage" for values, it does `self[some_key] = some_value`, but on python3
# this caused some errors in unittest framework. Refactor needed to not use
# dict() interface directly, instead use another attribute for "storage" such
# as `self._storage[some_key] = some_value`.
def __iter__(self):
raise TypeError()
def setBaseAmountQuantityMethod(self, base_amount_id, text):
"""Populate TradeModelLine_getBaseAmountQuantityMethod shared script
......@@ -103,13 +93,13 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
return base_amount
def afterSetUp(self):
UserDict.__init__(self)
self._storage = dict()
self.portal.portal_preferences.getActiveSystemPreference().setPreferredTaxUseList(['use/tax'])
self.tic()
return super(TestTradeModelLineMixin, self).afterSetUp()
def beforeTearDown(self):
UserDict.clear(self)
self._storage.clear()
return super(TestTradeModelLineMixin, self).beforeTearDown()
def clone(self, document):
......@@ -118,7 +108,7 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
parent.manage_copyObjects(ids=document.getId()))
clone = parent[clone['new_id']]
try:
self[clone.getPath()] = self[document.getPath()]
self._storage[clone.getPath()] = self._storage[document.getPath()]
except KeyError:
pass
return clone
......@@ -171,7 +161,7 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
]
for business_link in business_link_list:
link = self.createBusinessLink(business_process, **business_link)
self['business_link/' + link.getTradePhaseId()] = link
self._storage['business_link/' + link.getTradePhaseId()] = link
return business_process
@save_result_as('trade_condition')
......@@ -194,7 +184,7 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
line = document.newContent(portal_type='Trade Model Line', **kw)
reference = line.getReference()
if reference:
self['trade_model_line/' + reference] = line
self._storage['trade_model_line/' + reference] = line
return line
@save_result_as('order')
......@@ -213,7 +203,7 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
if order.getProperty(arrow) is None:
order._setProperty(arrow, self.createNode())
if not order.getPriceCurrency():
self['price_currency'] = price_currency = self.createCurrency()
self._storage['price_currency'] = price_currency = self.createCurrency()
order._setPriceCurrencyValue(price_currency)
for line_kw in order_line_list:
order.newContent(portal_type=self.order_line_portal_type, **line_kw)
......@@ -325,9 +315,9 @@ class TestTradeModelLine(TestTradeModelLineMixin):
self.assertEqual('packed', packing_list.getContainerState())
def copyExpectedAmountDict(self, delivery, ratio=1):
self[delivery.getPath()] = expected_amount_dict = {}
self._storage[delivery.getPath()] = expected_amount_dict = {}
causality = delivery.getCausalityValue()
for base_amount, amount_dict in six.iteritems(self[causality.getPath()]):
for base_amount, amount_dict in six.iteritems(self._storage[causality.getPath()]):
expected_amount_dict[base_amount] = new_amount_dict = {}
for line in delivery.getMovementList():
line_id = line.getCausalityId()
......@@ -363,7 +353,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
packing_list.deliver()
self.tic()
self['invoice'] = invoice
self._storage['invoice'] = invoice
if build == 'invoice':
return invoice
......@@ -405,12 +395,12 @@ class TestTradeModelLine(TestTradeModelLineMixin):
self.assertTrue(count, len(composed.objectValues()))
def checkAggregatedAmountList(self, order):
expected_result_dict = self[order.getPath()]
expected_result_dict = self._storage[order.getPath()]
def check(movement, movement_id):
kw = {}
for reference, result in six.iteritems(expected_result_dict):
total_price = result.get(movement_id) or 0.0
model_line = self['trade_model_line/' + reference]
model_line = self._storage['trade_model_line/' + reference]
kw[reference] = dict(total_price=total_price,
causality_value_list=[model_line],
base_application_list=model_line.getBaseApplicationList(),
......@@ -422,7 +412,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
check(line, line.getId())
def checkTradeModelRuleSimulationExpand(self, delivery):
expected_result_dict = self[delivery.getPath()]
expected_result_dict = self._storage[delivery.getPath()]
for line in delivery.getMovementList():
currency_precision = line.getPricePrecision()
......@@ -473,7 +463,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
line_dict[key] += line.getTotalPrice()
self.assertEqual(6, len(line_dict))
currency_precision = self['price_currency'].getQuantityPrecision()
currency_precision = self._storage['price_currency'].getQuantityPrecision()
rounded_total_price = round(line_dict['normal'], currency_precision)
rounded_tax_price = round(line_dict['tax'], currency_precision)
rounded_discount_price = round(line_dict['discount'], currency_precision)
......@@ -539,7 +529,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
discount = {None: (3*4 + 5*6) * self.default_discount_ratio,
'discounted': (3*4) * self.default_discount_ratio,
'taxed_discounted': (5*6) * self.default_discount_ratio}
self[order.getPath()] = dict(
self._storage[order.getPath()] = dict(
discount=discount,
tax={None: (1*2 + 5*6 + discount[None]) * self.default_tax_ratio,
'taxed': (1*2) * self.default_tax_ratio,
......@@ -577,7 +567,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
discount = {None: (6*10 + 10*15) * self.default_discount_ratio,
'discounted': (6*10) * self.default_discount_ratio,
'taxed_discounted': (10*15) * self.default_discount_ratio}
self[order.getPath()] = dict(
self._storage[order.getPath()] = dict(
discount=discount,
tax={None: (2*5 + 10*15 + discount[None]) * self.default_tax_ratio,
'taxed': (2*5) * self.default_tax_ratio,
......@@ -598,7 +588,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
portal_type=self.packing_list_portal_type)
self.copyExpectedAmountDict(packing_list)
self['packing_list'] = packing_list
self._storage['packing_list'] = packing_list
if build == 'packing_list':
return packing_list
......@@ -608,7 +598,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
invoice = self.test_01_OrderWithSimpleTaxedAndDiscountedLines('invoice')
# on invoice, make specialise point to a new TC and check it diverged
trade_condition = self['trade_condition']
trade_condition = self._storage['trade_condition']
new_trade_condition = self.clone(trade_condition)
line_dict = {line.getReference(): line
for line in new_trade_condition.objectValues()}
......@@ -628,7 +618,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
self.checkCausalityState(invoice, 'solved')
# check how is supported addition of invoice line to invoice
for line in self['order'].getMovementList():
for line in self._storage['order'].getMovementList():
line = invoice.newContent(portal_type=self.invoice_line_portal_type,
resource=line.getResource(),
quantity=line.getQuantity(),
......@@ -678,7 +668,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
self.tic()
self.checkCausalityState(packing_list, 'diverged')
order = self['order']
order = self._storage['order']
self.checkTradeModelRuleSimulationExpand(order)
self.copyExpectedAmountDict(packing_list,
self.modified_packing_list_line_quantity_ratio)
......@@ -777,7 +767,7 @@ class TestTradeModelLine(TestTradeModelLineMixin):
discount_price = (3*4) * 0.32
tax_price = (1*2) * 0.2
total_tax_price = tax_price * 2 * 0.12
self[order.getPath()] = dict(
self._storage[order.getPath()] = dict(
service_tax={None: tax_price, 'taxed': tax_price},
total_dicount_2={None: discount_price, 'discounted': discount_price},
service_tax_2={None: tax_price, 'taxed': tax_price},
......@@ -835,13 +825,13 @@ return getBaseAmountQuantity""")
base_application_list=[index[i] for i in base_application],
base_contribution_list=[index[i] for i in base_contribution],
*index)
createCells(self['trade_model_line/tax2'], {
createCells(self._storage['trade_model_line/tax2'], {
('tax_range/0_200', 'tax_share/A'): .1,
('tax_range/0_200', 'tax_share/B'): .2,
('tax_range/200_inf', 'tax_share/A'): .3,
('tax_range/200_inf', 'tax_share/B'): .4,
}, base_application=(0,), base_contribution=(1,))
createCells(self['trade_model_line/tax3'], {
createCells(self._storage['trade_model_line/tax3'], {
('tax_share/A',): .5,
('tax_share/B',): .6,
}, base_application=(0,))
......
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