Commit d8d32b7e authored by Jean-Paul Smets's avatar Jean-Paul Smets

Added test for List accessors

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@10031 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 54351d51
...@@ -72,38 +72,28 @@ class DefaultSetter(Method): ...@@ -72,38 +72,28 @@ class DefaultSetter(Method):
if not self._reindex: if not self._reindex:
# Modify the property # Modify the property
if self._is_tales_type: if self._is_tales_type:
if value in self._null: setattr(instance, self._storage_id, str(value))
value = None
setattr(instance, self._storage_id, value)
elif value in self._null: elif value in self._null:
# The value has no default property -> it is empty # The value has no default property -> it is empty
setattr(instance, self._storage_id, ()) setattr(instance, self._storage_id, ())
else: else:
value = self._cast(args[0])
if self._item_cast is not identity: if self._item_cast is not identity:
value = map(self._item_cast, value) value = self._item_cast(value)
if len(value) > 0: default_value = value
default_value = value[0]
list_value = getattr(instance, self._storage_id, None) list_value = getattr(instance, self._storage_id, None)
if list_value is None: list_value = [] if list_value is None: list_value = []
new_list_value = [default_value] my_dict = dict([(x, 0) for x in list_value if x != default_value])
keep_value = 0 new_list_value = my_dict.keys()
for k in list_value: new_list_value.insert(0, default_value)
# Only delete the first occurence if exists value = new_list_value
if keep_value or k is not default_value: setattr(instance, self._storage_id, tuple(value))
new_list_value += [k]
keep_value = 1
else:
# The list has no default property -> it is empty
new_list_value = []
setattr(instance, self._storage_id, tuple(new_list_value))
else: else:
# Call the private setter # Call the private setter
method = getattr(instance, '_' + self._id) method = getattr(instance, '_' + self._id)
method(*args, **kw) method(*args, **kw)
if self._reindex: instance.reindexObject() if self._reindex: instance.reindexObject()
class Setter(DefaultSetter): class ListSetter(DefaultSetter):
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
value = args[0] value = args[0]
...@@ -125,8 +115,7 @@ class Setter(DefaultSetter): ...@@ -125,8 +115,7 @@ class Setter(DefaultSetter):
if self._reindex: instance.reindexObject() if self._reindex: instance.reindexObject()
ListSetter = Setter Setter = ListSetter
class SetSetter(Method): class SetSetter(Method):
""" """
...@@ -166,11 +155,8 @@ class SetSetter(Method): ...@@ -166,11 +155,8 @@ class SetSetter(Method):
if not self._reindex: if not self._reindex:
# Modify the property # Modify the property
if self._is_tales_type: if self._is_tales_type:
if value in self._null: setattr(instance, self._storage_id, str(value))
value = None
setattr(instance, self._storage_id, value)
elif value in self._null: elif value in self._null:
# The value has no default property -> it is empty
setattr(instance, self._storage_id, ()) setattr(instance, self._storage_id, ())
else: else:
value = self._cast(args[0]) value = self._cast(args[0])
...@@ -179,27 +165,9 @@ class SetSetter(Method): ...@@ -179,27 +165,9 @@ class SetSetter(Method):
if len(value) > 0: if len(value) > 0:
list_value = getattr(instance, self._storage_id, None) list_value = getattr(instance, self._storage_id, None)
if list_value is None: list_value = [] if list_value is None: list_value = []
# if len(list_value) > 0:
# my_dict = {}
# default_value = list_value[0]
# for v in value:
# my_dict[v] = 0
# if my_dict.has_key(default_value):
# del my_dict[default_value]
# # If we change the set, the default value must be in the new set
# if default_value in value:
# new_list_value = [default_value] + my_dict.keys()
# else:
# new_list_value = my_dict.keys()
# else:
# new_list_value = value
if len(list_value) > 0: if len(list_value) > 0:
default_value = list_value[0] default_value = list_value[0]
my_dict = dict([(x, 0) for x in value if x!=default_value]) my_dict = dict([(x, 0) for x in value if x != default_value])
new_list_value = my_dict.keys() new_list_value = my_dict.keys()
# If we change the set, # If we change the set,
# the default value must be in the new set # the default value must be in the new set
...@@ -207,7 +175,6 @@ class SetSetter(Method): ...@@ -207,7 +175,6 @@ class SetSetter(Method):
new_list_value.insert(0, default_value) new_list_value.insert(0, default_value)
else: else:
new_list_value = value new_list_value = value
else: else:
# The list has no default property -> it is empty # The list has no default property -> it is empty
new_list_value = [] new_list_value = []
...@@ -300,7 +267,7 @@ class ListGetter(Method): ...@@ -300,7 +267,7 @@ class ListGetter(Method):
if self._is_tales_type: if self._is_tales_type:
if kw.get('evaluate', 1): if kw.get('evaluate', 1):
if type(list_value) != type(''): if type(list_value) != type(''):
LOG('ListGetter', 0, 'instance = %r, self._storage_id = %r, list_value = %r' % (instance, self._storage_id, list_value,)) LOG('ListGetter', 0, 'instance = %r, self._storage_id = %r, list_value = %r' % (instance, self._storage_id, list_value,)) # If we reach this point, something strange is going on
list_value = evaluateTales(instance=instance, value=list_value) list_value = evaluateTales(instance=instance, value=list_value)
else: else:
return list_value return list_value
......
...@@ -161,6 +161,12 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -161,6 +161,12 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
getRegionRelatedValueList getRegionRelatedValueList
getRegionRelatedIdList getRegionRelatedIdList
getRegionRelatedTitleList getRegionRelatedTitleList
This tests also makes sure that the related accessors are
compatible with acquisition of category. Although region
is not defined on a Person, Person documents are member
of a region and should thus be accessible from the region
category through getRegionRelated accessors
""" """
if not run: return if not run: return
portal = self.getPortal() portal = self.getPortal()
...@@ -235,15 +241,36 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -235,15 +241,36 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
checkRelationUnset(self) checkRelationUnset(self)
def test_05_setProperty(self, quiet=quiet, run=run_all_test): def test_05_setProperty(self, quiet=quiet, run=run_all_test):
"""
In this test we create a subobject (ie. a phone number)
and show the difference between calling getProperty and
an accessor.
Accessors can be acquired thus returning a property value
defined on a parent object whereas getProperty / setProperty
always act at the level of the object itself.
We also do some basic tests on the telephone number parser
XXX I think this is inconsistent because it prevents from
using getProperty / setProperty as a generic way to use
accessors from subobjects.
"""
if not run: return if not run: return
portal = self.getPortal() portal = self.getPortal()
module = self.getOrganisationModule() module = self.getOrganisationModule()
organisation = module.newContent(id='1', portal_type='Organisation') organisation = module.newContent(id='1', portal_type='Organisation')
organisation.setDefaultTelephoneText('55 55 5555') organisation.setDefaultTelephoneText('+55(0)66-5555')
self.assertEquals(organisation.default_telephone.getTelephoneCountry(),'55')
self.assertEquals(organisation.default_telephone.getTelephoneArea(),'66')
self.assertEquals(organisation.default_telephone.getTelephoneNumber(),'5555')
organisation.setCorporateName('Nexedi') organisation.setCorporateName('Nexedi')
#self.assertEquals(organisation.default_telephone.getProperty('corporate_name'),'Nexedi') # Who is right ? XXX
organisation.default_telephone.setProperty('corporate_name','Toto') organisation.default_telephone.setProperty('corporate_name','Toto')
self.assertEquals(organisation.corporate_name,'Nexedi') self.assertEquals(organisation.corporate_name,'Nexedi')
self.assertEquals(organisation.default_telephone.getCorporateName(),'Nexedi')
self.assertEquals(organisation.default_telephone.corporate_name,'Toto') self.assertEquals(organisation.default_telephone.corporate_name,'Toto')
self.assertEquals(organisation.default_telephone.getProperty('corporate_name'),'Toto')
def test_06_CachingMethod(self, quiet=quiet, run=run_all_test): def test_06_CachingMethod(self, quiet=quiet, run=run_all_test):
"""Tests Caching methods.""" """Tests Caching methods."""
...@@ -337,6 +364,7 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -337,6 +364,7 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
def test_09_RenameObjects(self, quiet=quiet, run=run_all_test): def test_09_RenameObjects(self, quiet=quiet, run=run_all_test):
"""Test object renaming. """Test object renaming.
As we overloaded some parts of OFS, it's better to test again some basic As we overloaded some parts of OFS, it's better to test again some basic
features. features.
""" """
...@@ -361,7 +389,52 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -361,7 +389,52 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
new_id = '%s_new' % id_ new_id = '%s_new' % id_
self.assertEquals(folder._getOb(new_id).getId(), new_id) self.assertEquals(folder._getOb(new_id).getId(), new_id)
def test_10_valueAccessor(self, quiet=quiet, run=run_all_test): def test_10_ConstraintNotFound(self, quiet=quiet, run=run_all_test):
"""
When a Constraint is not found while importing a PropertySheet, AttributeError
was raised, and generated a infinite loop.
This is a test to make sure this will not happens any more
"""
if not run: return
# We will first define a new propertysheet
class_tool = self.getClassTool()
class_tool.newPropertySheet('TestPropertySheet')
text = """
class TestPropertySheet:
\"\"\"
TestPropertySheet for this unit test
\"\"\"
_properties = (
{ 'id' : 'strange_property',
'description' : 'A local property description',
'type' : 'string',
'mode' : '' },
)
_constraints = (
{ 'id' : 'toto',
'description' : 'define a bad constraint',
'type' : 'TestConstraintNotFoundClass',
},
)
"""
class_tool.editPropertySheet('TestPropertySheet',text)
class_tool.importPropertySheet('TestPropertySheet')
# We set the property sheet on the portal type Organisation
type_tool = self.getTypeTool()
organisation_portal_type = type_tool['Organisation']
organisation_portal_type.setPropertySheetList(['TestPropertySheet'])
folder = self.getOrganisationModule()
_aq_reset()
# We check that we raise exception when we create new object
from Products.ERP5Type.Utils import ConstraintNotFound
organisation = self.assertRaises(ConstraintNotFound,folder.newContent,
portal_type='Organisation')
def test_11_valueAccessor(self, quiet=quiet, run=run_all_test):
""" """
The purpose of this test is to make sure that category accessors The purpose of this test is to make sure that category accessors
work as expected. work as expected.
...@@ -374,8 +447,6 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -374,8 +447,6 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
The test is implemented for both Category and Value The test is implemented for both Category and Value
accessors. accessors.
The same test must be done for category accessors
for list accessors and for acquired property accessors
""" """
if not run: return if not run: return
...@@ -401,6 +472,7 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -401,6 +472,7 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
self.assertEquals(alpha.getRelativeUrl(), 'region/alpha') self.assertEquals(alpha.getRelativeUrl(), 'region/alpha')
#get_transaction().commit()
alpha.immediateReindexObject() alpha.immediateReindexObject()
beta.immediateReindexObject() beta.immediateReindexObject()
zeta.immediateReindexObject() zeta.immediateReindexObject()
...@@ -488,51 +560,74 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -488,51 +560,74 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
self.assertEquals(result, ['alpha', 'beta']) self.assertEquals(result, ['alpha', 'beta'])
self.assertEquals(person.getRegionList(), ['alpha', 'beta']) self.assertEquals(person.getRegionList(), ['alpha', 'beta'])
def test_11_ConstraintNotFound(self, quiet=quiet, run=run_all_test): def test_12_listAccessor(self, quiet=quiet, run=run_all_test):
""" """
When a Constraint is not found while importing a PropertySheet, AttributeError The purpose of this test is to make sure that accessor for
was raised, and generated a infinite loop. sequence types support the same kind of semantics as the
This is a test to make sure this will not happens any more one on categories. We use 'subject' of the DublinCore propertysheet
on organisation documents for this test.
""" """
if not run: return if not run: return
# We will first define a new propertysheet
class_tool = self.getClassTool()
class_tool.newPropertySheet('TestPropertySheet') if not quiet:
text = """ message = 'Test Category setters'
class TestPropertySheet: ZopeTestCase._print('\n '+message)
\"\"\" LOG('Testing... ', 0, message)
TestPropertySheet for this unit test
\"\"\"
_properties = ( # Create a new person
{ 'id' : 'strange_property', module = self.getPersonModule()
'description' : 'A local property description', person = module.newContent(portal_type='Person')
'type' : 'string',
'mode' : '' },
)
_constraints = ( # Do the same tests as in test_11_valueAccessor
{ 'id' : 'toto', person.setSubject('alpha')
'description' : 'define a bad constraint', self.assertEquals(person.getSubject(), 'alpha')
'type' : 'TestConstraintNotFoundClass', person.setSubjectList(['alpha', 'alpha'])
}, self.assertEquals(person.getSubjectList(), ['alpha', 'alpha'])
) person.setSubjectSet(['alpha', 'alpha'])
self.assertEquals(person.getSubjectSet(), ['alpha'])
person.setSubjectList(['alpha', 'beta', 'alpha'])
self.assertEquals(person.getSubjectList(), ['alpha', 'beta', 'alpha'])
person.setSubjectSet(['alpha', 'beta', 'alpha'])
result = person.getSubjectSet()
result.sort()
self.assertEquals(result, ['alpha', 'beta'])
person.setDefaultSubject('beta')
self.assertEquals(person.getDefaultSubject(), 'beta')
result = person.getSubjectSet()
result.sort()
self.assertEquals(result, ['alpha', 'beta'])
self.assertEquals(person.getSubjectList(), ['beta', 'alpha'])
person.setDefaultSubject('alpha')
self.assertEquals(person.getDefaultSubject(), 'alpha')
result = person.getSubjectSet()
result.sort()
self.assertEquals(result, ['alpha', 'beta'])
self.assertEquals(person.getSubjectList(), ['alpha', 'beta'])
""" def test_13_acquiredAccessor(self, quiet=quiet, run=run_all_test):
class_tool.editPropertySheet('TestPropertySheet',text) """
class_tool.importPropertySheet('TestPropertySheet') The purpose of this test is to make sure that accessor for
# We set the property sheet on the portal type Organisation sequence types support the same kind of semantics as the
type_tool = self.getTypeTool() one on categories. We use 'subject' of the DublinCore propertysheet
organisation_portal_type = type_tool['Organisation'] on organisation documents for this test.
organisation_portal_type.setPropertySheetList(['TestPropertySheet']) """
folder = self.getOrganisationModule()
_aq_reset() # If address is updated on subordination, then
# We check that we raise exception when we create new object # address is updated on person
from Products.ERP5Type.Utils import ConstraintNotFound
organisation = self.assertRaises(ConstraintNotFound,folder.newContent, # If address is changed on person, it rem
portal_type='Organisation')
# If address not available on one organisation
# it is found on the mapping related organisation
# which is one step higher in the site
pass
def test_14_bangAccessor(self, quiet=quiet, run=run_all_test):
"""
Bang accesors must be triggered each time another accessor is called
They are useful for centralising events ?
"""
pass
if __name__ == '__main__': if __name__ == '__main__':
......
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