Commit f1f635c2 authored by Jérome Perrin's avatar Jérome Perrin

Change max_arity to be optional in MembershipArity. Simplify tests



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@18215 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 1229f408
......@@ -60,18 +60,26 @@ class CategoryMembershipArity(Constraint):
# Retrieve values inside de PropertySheet (_constraints)
base_category = self.constraint_definition['base_category']
min_arity = int(self.constraint_definition['min_arity'])
max_arity = int(self.constraint_definition['max_arity'])
max_arity = None
if 'max_arity' in self.constraint_definition:
max_arity = int(self.constraint_definition['max_arity'])
portal_type = self.constraint_definition['portal_type']
# Check arity and compare it with the min and max
arity = len(obj.getCategoryMembershipList(base_category,
portal_type=portal_type))
if (arity < min_arity) or (arity > max_arity):
arity = len(obj.getCategoryMembershipList(base_category,
portal_type=portal_type))
if not (max_arity is None and (min_arity <= arity)
or (min_arity <= arity <= max_arity)):
# Generate error message
error_message = "Arity error for relation '%s'" % \
base_category
if portal_type is not ():
error_message += " and portal_type: '%s'" % str(portal_type)
error_message += \
if max_arity is None:
error_message += \
", arity is equal to %i but should be at least %i" % \
(arity, min_arity)
else:
error_message += \
", arity is equal to %i but should be between %i and %i" % \
(arity, min_arity, max_arity)
# Add error
......
......@@ -61,18 +61,26 @@ class CategoryRelatedMembershipArity(Constraint):
# Retrieve values inside de PropertySheet (_constraints)
base_category = self.constraint_definition['base_category']
min_arity = int(self.constraint_definition['min_arity'])
max_arity = int(self.constraint_definition['max_arity'])
max_arity = None
if 'max_arity' in self.constraint_definition:
max_arity = int(self.constraint_definition['max_arity'])
portal_type = self.constraint_definition['portal_type']
# Check arity and compare it with the min and max
arity = len(obj._getRelatedValueList(base_category,
portal_type=portal_type))
if (arity < min_arity) or (arity > max_arity):
if not (max_arity is None and (min_arity <= arity)
or (min_arity <= arity <= max_arity)):
# Generate error message
error_message = "Arrity error for reverse relation '%s'" % \
base_category
if portal_type is not ():
error_message += " and portal_type: '%s'" % str(portal_type)
error_message += \
if max_arity is None:
error_message += \
", arity is equal to %i but should be at least %i" % \
(arity, min_arity)
else:
error_message += \
", arity is equal to %i but should be between %i and %i" % \
(arity, min_arity, max_arity)
# Add error
......
......@@ -62,6 +62,13 @@ class TestConstraint(PropertySheetTestCase):
self.category_tool = self.getCategoryTool()
self.createCategories()
def beforeTearDown(self):
get_transaction().abort()
module = self.portal.organisation_module
module.manage_delObjects(list(module.objectIds()))
get_transaction().commit()
self.tic()
def stepTic(self,**kw):
self.tic()
......@@ -202,7 +209,7 @@ class TestConstraint(PropertySheetTestCase):
object = sequence.get('object')
object.edit(local_prop = 12345)
def _createGenericConstraint(self, sequence, klass_name='Constraint',
def _createGenericConstraint(self, sequence=None, klass_name='Constraint',
**kw):
"""
Create a Constraint
......@@ -215,9 +222,8 @@ class TestConstraint(PropertySheetTestCase):
klass = file
# klass = getattr(file, klass_name)
constraint = klass(**kw)
sequence.edit(
constraint=constraint,
)
if sequence is not None:
sequence.edit(constraint=constraint,)
return constraint
def stepCallCheckConsistency(self, sequence=None,
......@@ -950,6 +956,19 @@ class TestConstraint(PropertySheetTestCase):
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self, quiet=quiet)
def test_CategoryMembershipArityNoMax(self):
obj = self._makeOne()
constraint = self._createGenericConstraint(
id='dummy_constraint',
portal_type=('Category',),
base_category=('group',),
klass_name='CategoryMembershipArity',
min_arity=1)
self.assertEquals(1, len(constraint.checkConsistency(obj)))
obj.setGroup('testGroup1')
self.assertEquals(0, len(constraint.checkConsistency(obj)))
def stepCreateCategoryRelatedMembershipArity0(self, sequence=None,
sequence_list=None, **kw):
"""
......@@ -1066,6 +1085,21 @@ class TestConstraint(PropertySheetTestCase):
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self, quiet=quiet)
def test_RelatedCategoryMembershipArityNoMax(self):
related_obj = self._makeOne()
obj = self.portal.portal_categories.group.testGroup1
constraint = self._createGenericConstraint(
id='dummy_constraint',
portal_type=('Organisation',),
base_category=('group',),
klass_name='CategoryRelatedMembershipArity',
min_arity=1)
self.assertEquals(1, len(constraint.checkConsistency(obj)))
related_obj.setGroupValue(obj)
get_transaction().commit()
self.tic()
self.assertEquals(0, len(constraint.checkConsistency(obj)))
def test_BooleanPropertiesPropertyTypeValidity(self):
"""Tests PropertyTypeValidity can handle boolean values.
"""
......@@ -1088,7 +1122,7 @@ class TestConstraint(PropertySheetTestCase):
def test_TALESConstraint(self):
"""Tests TALESConstraint
"""
constraint = self._createGenericConstraint(Sequence(),
constraint = self._createGenericConstraint(
klass_name='TALESConstraint',
id='tales_constraint',
expression='python: object.getTitle() != "foo"')
......@@ -1100,7 +1134,7 @@ class TestConstraint(PropertySheetTestCase):
def test_TALESConstraintInvalidExpression(self):
"""Tests TALESConstraint with an invalid expression
"""
constraint = self._createGenericConstraint(Sequence(),
constraint = self._createGenericConstraint(
klass_name='TALESConstraint',
id='tales_constraint',
expression='python: None / 3') # ValueError
......@@ -1109,14 +1143,14 @@ class TestConstraint(PropertySheetTestCase):
self.assertEquals(1, len(constraint.checkConsistency(obj)))
# an error during expression compilation is reraised to the programmer
constraint = self._createGenericConstraint(Sequence(),
constraint = self._createGenericConstraint(
klass_name='TALESConstraint',
id='tales_constraint',
expression='python: None (" ')
from Products.PageTemplates.TALES import CompilerError
self.assertRaises(CompilerError, constraint.checkConsistency, obj)
constraint = self._createGenericConstraint(Sequence(),
constraint = self._createGenericConstraint(
klass_name='TALESConstraint',
id='tales_constraint',
expression='error: " ')
......@@ -1126,7 +1160,7 @@ class TestConstraint(PropertySheetTestCase):
"""Tests PropertyTypeValidity can repairs local property when this property
is added on the class later.
"""
constraint = self._createGenericConstraint(Sequence(),
constraint = self._createGenericConstraint(
klass_name='PropertyTypeValidity',
id='type_validity_constraint', )
obj = self._makeOne()
......@@ -1144,7 +1178,7 @@ class TestConstraint(PropertySheetTestCase):
"""Tests PropertyTypeValidity can repairs local property of type content
when this property is added on the class later.
"""
constraint = self._createGenericConstraint(Sequence(),
constraint = self._createGenericConstraint(
klass_name='PropertyTypeValidity',
id='type_validity_constraint', )
obj = self._makeOne()
......@@ -1177,7 +1211,7 @@ class TestConstraint(PropertySheetTestCase):
bc = self.getPortal().portal_categories.newContent(
portal_type='Base Category',
id='testing_category')
constraint = self._createGenericConstraint(Sequence(),
constraint = self._createGenericConstraint(
klass_name='PropertyTypeValidity',
id='type_validity_constraint', )
obj = self._makeOne()
......@@ -1307,6 +1341,7 @@ class TestConstraint(PropertySheetTestCase):
sequence_list.play(self, quiet=quiet)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestConstraint))
......
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