[Erp5-report] r12216 - in /erp5/trunk/products/ERP5Type: Constraint/ tests/
nobody at svn.erp5.org
nobody at svn.erp5.org
Mon Jan 22 22:08:14 CET 2007
Author: rafael
Date: Mon Jan 22 22:08:00 2007
New Revision: 12216
URL: http://svn.erp5.org?rev=12216&view=rev
Log:
Added ContentExistence Constraint (Made by romain)
Added StringAttributeMatch Constraint (made by Romain, modified by Me)
Added tests for both Constraints.
Added:
erp5/trunk/products/ERP5Type/Constraint/ContentExistence.py
erp5/trunk/products/ERP5Type/Constraint/StringAttributeMatch.py
Modified:
erp5/trunk/products/ERP5Type/Constraint/__init__.py
erp5/trunk/products/ERP5Type/tests/testConstraint.py
Added: erp5/trunk/products/ERP5Type/Constraint/ContentExistence.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/ContentExistence.py?rev=12216&view=auto
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/ContentExistence.py (added)
+++ erp5/trunk/products/ERP5Type/Constraint/ContentExistence.py Mon Jan 22 22:08:00 2007
@@ -1,0 +1,63 @@
+##############################################################################
+#
+# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
+# Romain Courteaud <romain at nexedi.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsability of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# garantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+##############################################################################
+
+from Products.ERP5Type.Constraint import Constraint
+
+class ContentExistence(Constraint):
+ """
+ This constraint class allows to check / fix
+ that object contains one subobject.
+ Configuration example:
+ { 'id' : 'line',
+ 'description' : 'Object have to contain a Line',
+ 'type' : 'ContentExistence',
+ 'portal_type' : ('Line', ),
+ },
+ """
+
+ def checkConsistency(self, object, fixit=0):
+ """
+ This is the check method, we return a list of string,
+ each string corresponds to an error.
+ We are checking that object contains a subobject.
+ """
+ obj = object
+ errors = []
+ if self._checkConstraintCondition(object):
+ # Retrieve values inside de PropertySheet (_constraints)
+ portal_type = self.constraint_definition['portal_type']
+ # Check arity and compare it with the min and max
+ arity = len(obj.contentValues(portal_type=portal_type))
+ if (arity == 0):
+ # Generate error message
+ error_message = "Does not contain any subobject"
+ if portal_type is not ():
+ error_message += " of portal type: '%s'" % str(portal_type)
+ # Add error
+ errors.append(self._generateError(obj, error_message))
+ return errors
Added: erp5/trunk/products/ERP5Type/Constraint/StringAttributeMatch.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/StringAttributeMatch.py?rev=12216&view=auto
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/StringAttributeMatch.py (added)
+++ erp5/trunk/products/ERP5Type/Constraint/StringAttributeMatch.py Mon Jan 22 22:08:00 2007
@@ -1,0 +1,65 @@
+##############################################################################
+#
+# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
+# Romain Courteaud <romain at nexedi.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsability of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# garantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+##############################################################################
+
+import re
+from Products.ERP5Type.Constraint.PropertyExistence import PropertyExistence
+
+class StringAttributeMatch(PropertyExistence):
+ """
+ This constraint class allows to check
+ that a string property match a regular
+ expression. Configuration example:
+ { 'id' : 'title_not_empty',
+ 'description' : 'Title must be defined',
+ 'type' : 'StringAttributeMatch',
+ 'title' : '^[^ ]'
+ },
+ """
+
+ def checkConsistency(self, object, fixit=0):
+ """
+ This is the check method, we return a list of string,
+ each string corresponds to an error.
+ Check that each attribute does not match the RE
+ """
+ errors = PropertyExistence.checkConsistency(self, object, fixit=fixit)
+ for attribute_name, attribute_value in self.constraint_definition.items():
+ error_message = None
+ # If property does not exist, error will be raise by
+ # PropertyExistence Constraint.
+ current_value = object.getProperty(attribute_name)
+ regexp = re.compile(attribute_value)
+ if (current_value is not None) and \
+ (regexp.match(current_value) is None):
+ # Generate error_message
+ error_message = "Attribute %s is '%s' and not match '%s'" % \
+ (attribute_name, current_value,
+ attribute_value)
+ # Generate error
+ errors.append(self._generateError(object, error_message))
+ return errors
Modified: erp5/trunk/products/ERP5Type/Constraint/__init__.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Constraint/__init__.py?rev=12216&r1=12215&r2=12216&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Constraint/__init__.py (original)
+++ erp5/trunk/products/ERP5Type/Constraint/__init__.py Mon Jan 22 22:08:00 2007
@@ -8,3 +8,5 @@
from PortalTypeClass import PortalTypeClass
from CategoryAcquiredMembershipArity import CategoryAcquiredMembershipArity
from TALESConstraint import TALESConstraint
+from ContentExistence import ContentExistence
+from StringAttributeMatch import StringAttributeMatch
Modified: erp5/trunk/products/ERP5Type/tests/testConstraint.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/tests/testConstraint.py?rev=12216&r1=12215&r2=12216&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/tests/testConstraint.py (original)
+++ erp5/trunk/products/ERP5Type/tests/testConstraint.py Mon Jan 22 22:08:00 2007
@@ -44,6 +44,7 @@
quiet = 1
object_portal_type = "Organisation"
+ object_content_portal_type = "Address"
object_title = "Title test"
def getTitle(self):
@@ -1174,6 +1175,121 @@
# now we can use testing_category as any category accessor
self.assertEquals(obj, obj.getTestingCategoryValue())
+ def stepCreateContentExistence(self, sequence=None, sequence_list=None, **kw):
+ """
+ Create a Content Existence Constraint
+ """
+ self._createGenericConstraint(
+ sequence,
+ klass_name='ContentExistence',
+ id='ContentExistence',
+ description='ContentExistence test',
+ portal_type=(self.object_content_portal_type, )
+ )
+
+ def stepCreateContentObject(self, sequence=None, sequence_list=None, **kw):
+ """
+ Create a Content Object inside one Object
+ """
+ object = sequence.get('object')
+ content_object = object.newContent(portal_type=self.object_content_portal_type)
+ sequence.edit(
+ content_object = content_object,
+ )
+
+ def test_ContentExistenceConstraint(self, quiet=quiet, run=run_all_test):
+ """
+ Tests Content Existence
+ """
+
+ if not run: return
+ sequence_list = SequenceList()
+ # Test Constraint without any content
+ sequence_string = '\
+ CreateObject \
+ CreateContentExistence \
+ CallCheckConsistency \
+ CheckIfConstraintFailed \
+ '
+ sequence_list.addSequenceString(sequence_string)
+ # Test Constraint with content
+ sequence_string = '\
+ CreateObject \
+ CreateContentExistence \
+ CreateContentObject \
+ CallCheckConsistency \
+ CheckIfConstraintSucceeded \
+ '
+ sequence_list.addSequenceString(sequence_string)
+ sequence_list.play(self, quiet=quiet)
+
+ def stepCreateStringAttributeMatch(self, sequence=None, sequence_list=None, **kw):
+ """
+ Create a String Atribute Match Constraint
+ """
+ self._createGenericConstraint(
+ sequence,
+ klass_name='StringAttributeMatch',
+ id='StringAttributeMatch',
+ description='StringAttributeMatch test',
+ title='^[^ ]'
+ )
+
+ def stepSetObjectTitle0(self, sequence=None, sequence_list=None, **kw):
+ """
+ Set valid Title to Object
+ """
+ object = sequence.get('object')
+ object.setTitle(self.object_title)
+ sequence.edit(
+ object = object,
+ )
+
+ def stepSetObjectTitle1(self, sequence=None, sequence_list=None, **kw):
+ """
+ Set empty (or invalid string) to Object
+ """
+ object = sequence.get('object')
+ object.setTitle(' ')
+ sequence.edit(
+ object = object,
+ )
+
+ def test_StringAttributeMatchConstraint(self, quiet=quiet, run=run_all_test):
+ """
+ Tests Content Existence
+ """
+ if not run: return
+ sequence_list = SequenceList()
+ # Test Constraint with empty Title
+ sequence_string = '\
+ CreateObject \
+ CreateStringAttributeMatch \
+ CallCheckConsistency \
+ CheckIfConstraintFailed \
+ '
+ sequence_list.addSequenceString(sequence_string)
+ # Test Constraint with Title
+ sequence_string = '\
+ CreateObject \
+ CreateStringAttributeMatch \
+ SetObjectTitle0 \
+ CallCheckConsistency \
+ CheckIfConstraintSucceeded \
+ '
+ sequence_list.addSequenceString(sequence_string)
+ # Test Constraint with invalid Title
+ # Not match with regex
+ sequence_string = '\
+ CreateObject \
+ CreateStringAttributeMatch \
+ SetObjectTitle1 \
+ CallCheckConsistency \
+ CheckIfConstraintFailed \
+ '
+ sequence_list.addSequenceString(sequence_string)
+
+ sequence_list.play(self, quiet=quiet)
if __name__ == '__main__':
framework()
More information about the Erp5-report
mailing list