Commit bf5f12ee authored by Nicolas Dumazet's avatar Nicolas Dumazet

unit definitions: add validation workflow (2/2)

* Add validation workflow to Quantity Unit Conversion Definition & Group
* Only validated objects can define a valid quantity unit
* Add interaction workflow event: when an object is (in)validated,
  invalidate the Cache for site-wide quantities

* Fix b/c test by using proper tear down methods in other tests and by clearing
  the cache


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@32196 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 52a62a7d
...@@ -870,21 +870,29 @@ class Resource(XMLMatrix, Variated): ...@@ -870,21 +870,29 @@ class Resource(XMLMatrix, Variated):
QuantityUnitConversionModule_getUniversalDefinitionDict() QuantityUnitConversionModule_getUniversalDefinitionDict()
result = global_definition_dict.copy() result = global_definition_dict.copy()
for definition_list in self.objectValues(portal_type= \ for definition_group in self.objectValues(portal_type= \
'Quantity Unit Conversion Group'): 'Quantity Unit Conversion Group'):
standard_quantity_unit_uid = definition_list.getQuantityUnitUid() if definition_group.getValidationState() != "validated":
continue
standard_quantity_unit_uid = definition_group.getQuantityUnitUid()
if standard_quantity_unit_uid is None: if standard_quantity_unit_uid is None:
continue continue
reference_ratio = global_definition_dict[standard_quantity_unit_uid][1] reference_ratio = global_definition_dict[standard_quantity_unit_uid][1]
for definition in definition_list.objectValues(portal_type= \ for definition in definition_group.objectValues(portal_type= \
'Quantity Unit Conversion Definition'): 'Quantity Unit Conversion Definition'):
if definition.getValidationState() != "validated":
continue
unit_uid = definition.getQuantityUnitUid() unit_uid = definition.getQuantityUnitUid()
if unit_uid is None: if unit_uid is None:
continue continue
quantity = definition.getQuantity() quantity = definition.getQuantity()
if not quantity: if not quantity:
continue continue
result[unit_uid] = (definition.getUid(), quantity*reference_ratio) result[unit_uid] = (definition.getUid(), quantity*reference_ratio)
return result return result
......
...@@ -2395,15 +2395,20 @@ class BaseTestUnitConversion(InventoryAPITestCase): ...@@ -2395,15 +2395,20 @@ class BaseTestUnitConversion(InventoryAPITestCase):
portal_type='Quantity Unit Conversion Group', portal_type='Quantity Unit Conversion Group',
quantity_unit="%s/%s" % (base, standard), quantity_unit="%s/%s" % (base, standard),
immediate_reindex=1 ) immediate_reindex=1 )
if group.getValidationState() in ('draft', 'invalidated'):
group.validate()
for unit, amount in definition_dict.iteritems(): for unit, amount in definition_dict.iteritems():
if group._getOb(unit, None) is None: definition = group._getOb(unit, None)
group.newContent( if definition is None:
id=unit, definition = group.newContent(
portal_type="Quantity Unit Conversion Definition", id=unit,
quantity_unit="%s/%s" % (base, unit), portal_type="Quantity Unit Conversion Definition",
quantity=amount, quantity_unit="%s/%s" % (base, unit),
immediate_reindex=1) quantity=amount,
immediate_reindex=1)
if definition.getValidationState() in ('draft', 'invalidated'):
definition.validate()
def afterSetUp(self): def afterSetUp(self):
InventoryAPITestCase.afterSetUp(self) InventoryAPITestCase.afterSetUp(self)
...@@ -2434,6 +2439,16 @@ class BaseTestUnitConversion(InventoryAPITestCase): ...@@ -2434,6 +2439,16 @@ class BaseTestUnitConversion(InventoryAPITestCase):
category_list += InventoryAPITestCase.getNeededCategoryList(self) category_list += InventoryAPITestCase.getNeededCategoryList(self)
return category_list return category_list
def beforeTearDown(self):
# invalidating definitions is enough
unit_module = self.portal.quantity_unit_conversion_module
for obj in unit_module.objectValues():
if obj.getValidationState() == "validated":
obj.invalidate()
super(BaseTestUnitConversion, self).beforeTearDown()
class TestUnitConversion(BaseTestUnitConversion): class TestUnitConversion(BaseTestUnitConversion):
QUANTITY_UNIT_DICT = { QUANTITY_UNIT_DICT = {
# base: (reference, dict_of_others) # base: (reference, dict_of_others)
...@@ -2561,6 +2576,7 @@ class TestUnitConversionDefinition(BaseTestUnitConversion): ...@@ -2561,6 +2576,7 @@ class TestUnitConversionDefinition(BaseTestUnitConversion):
portal_type='Quantity Unit Conversion Group', portal_type='Quantity Unit Conversion Group',
quantity_unit='unit/unit', quantity_unit='unit/unit',
immediate_reindex=1) immediate_reindex=1)
base_unit.validate()
unit = base_unit.newContent( unit = base_unit.newContent(
...@@ -2568,9 +2584,18 @@ class TestUnitConversionDefinition(BaseTestUnitConversion): ...@@ -2568,9 +2584,18 @@ class TestUnitConversionDefinition(BaseTestUnitConversion):
quantity_unit='unit/lot', quantity_unit='unit/lot',
quantity=50, quantity=50,
immediate_reindex=1) immediate_reindex=1)
unit.validate()
self._safeTic() self._safeTic()
def beforeTearDown(self):
# invalidating definitions is enough
for obj in self.resource_bylot_overriding.objectValues("Quantity Unit " \
"Conversion Group"):
obj.invalidate()
super(TestUnitConversionDefinition, self).beforeTearDown()
def testAggregatedReports(self): def testAggregatedReports(self):
self.makeMovement(-10, self.resource_bylot) self.makeMovement(-10, self.resource_bylot)
self.makeMovement(-1, self.resource_bypack) self.makeMovement(-1, self.resource_bypack)
...@@ -2630,10 +2655,11 @@ class TestUnitConversionDefinition(BaseTestUnitConversion): ...@@ -2630,10 +2655,11 @@ class TestUnitConversionDefinition(BaseTestUnitConversion):
self.resource_bylot_overriding\ self.resource_bylot_overriding\
.convertQuantity(1, "unit/pack", "unit/lot")) .convertQuantity(1, "unit/pack", "unit/lot"))
def testResourceConvertQuantityAfterGlobalChange(self): def checkInitialStateAndGetLotDefinition(self):
""" """
after a change in a Global unit definition, definitions should get Helper function: check correctness of initial definitions
reindexed. and return the (sole) Definition object of unit/lot for further
changes.
""" """
# Before the global change, global definition reads 1000 # Before the global change, global definition reads 1000
self.assertEquals(1000, self.assertEquals(1000,
...@@ -2652,25 +2678,70 @@ class TestUnitConversionDefinition(BaseTestUnitConversion): ...@@ -2652,25 +2678,70 @@ class TestUnitConversionDefinition(BaseTestUnitConversion):
grand_parent_portal_type= \ grand_parent_portal_type= \
"Quantity Unit Conversion" \ "Quantity Unit Conversion" \
" Module", " Module",
validation_state="validated",
portal_type= \ portal_type= \
"Quantity Unit Conversion" \ "Quantity Unit Conversion" \
" Definition") " Definition")
self.assertEquals(1, len(query)) self.assertEquals(1, len(query))
query[0].getObject().setQuantity(500) return query[0].getObject()
# this change triggers Resource reindexations. Wait for 'em! def testResourceConvertQuantityAfterGlobalChange(self):
transaction.commit() """
self.tic() after a change in a Global unit definition, definitions should get
reindexed.
"""
lot_definition = self.checkInitialStateAndGetLotDefinition()
# SQL tables should have been updated: # We use a try...finally to avoid hitting unrelated tests
self.assertEquals(500, # in case of a failure
self.resource_bylot.convertQuantity(1, try:
"unit/lot", lot_definition.setQuantity(500)
"unit/unit"))
# without affecting resources that override the definition # this change triggers Resource reindexations. Wait for 'em!
self.assertEquals(1*50, transaction.commit()
self.resource_bylot_overriding\ self.tic()
.convertQuantity(1, "unit/lot", "unit/unit"))
# SQL tables should have been updated:
self.assertEquals(500,
self.resource_bylot.convertQuantity(1,
"unit/lot",
"unit/unit"))
# without affecting resources that override the definition
self.assertEquals(1*50,
self.resource_bylot_overriding\
.convertQuantity(1, "unit/lot", "unit/unit"))
finally:
# restore initial value
lot_definition.setQuantity(1000)
def testResourceConvertQuantityAfterInvalidation(self):
"""
after invalidating a Global unit definition, definitions should get
reindexed, and cache should be reloaded.
"""
lot_definition = self.checkInitialStateAndGetLotDefinition()
# Be careful to restore the object state whatever is the outcome
# of the test
try:
lot_definition.invalidate()
# this change triggers Resource reindexations. Wait for 'em!
transaction.commit()
self.tic()
# SQL tables should have been updated:
self.assertEquals(None,
self.resource_bylot.convertQuantity(1,
"unit/lot",
"unit/unit"))
# without affecting resources that override the definition
self.assertEquals(1*50,
self.resource_bylot_overriding\
.convertQuantity(1, "unit/lot", "unit/unit"))
finally:
# restore initial state
lot_definition.validate()
class TestUnitConversionBackwardCompatibility(BaseTestUnitConversion): class TestUnitConversionBackwardCompatibility(BaseTestUnitConversion):
QUANTITY_UNIT_DICT = { QUANTITY_UNIT_DICT = {
...@@ -2686,7 +2757,8 @@ class TestUnitConversionBackwardCompatibility(BaseTestUnitConversion): ...@@ -2686,7 +2757,8 @@ class TestUnitConversionBackwardCompatibility(BaseTestUnitConversion):
mass_category.gram.setProperty('quantity', 0.001) mass_category.gram.setProperty('quantity', 0.001)
mass_category.kilogram.setProperty('quantity', 1) mass_category.kilogram.setProperty('quantity', 1)
pass # clear cache to force recalculation of quantity units
self.portal.portal_caches.clearCache(('erp5_content_long',))
def testBackwardCompatibility(self): def testBackwardCompatibility(self):
delivery_rule = self.getRuleTool().default_delivery_rule delivery_rule = self.getRuleTool().default_delivery_rule
......
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