Commit f4b3760b authored by Alexandre Boeglin's avatar Alexandre Boeglin

Fixed 'default' handling in Base Accessor, added test.

Immediate issue was fixed, but more needs to be done.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@10254 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent a99063c9
...@@ -119,6 +119,7 @@ class Getter(Method): ...@@ -119,6 +119,7 @@ class Getter(Method):
func_code.co_varnames = ('self',) func_code.co_varnames = ('self',)
func_code.co_argcount = 1 func_code.co_argcount = 1
func_defaults = () func_defaults = ()
_default=None
def __init__(self, id, key, property_type, default=None, storage_id=None): def __init__(self, id, key, property_type, default=None, storage_id=None):
self._id = id self._id = id
...@@ -132,11 +133,7 @@ class Getter(Method): ...@@ -132,11 +133,7 @@ class Getter(Method):
self._storage_id = storage_id self._storage_id = storage_id
self._is_tales_type = (property_type == 'tales') self._is_tales_type = (property_type == 'tales')
def __call__(self, instance, object=None, *args, **kw): def __call__(self, instance, default=_default, object=None, *args, **kw):
if len(args) > 0:
default = args[0]
else:
default = self._default
# No acquisition on properties # No acquisition on properties
value = getattr(aq_base(instance), self._storage_id, None) value = getattr(aq_base(instance), self._storage_id, None)
if value is not None: if value is not None:
......
...@@ -679,6 +679,44 @@ class TestPropertySheet: ...@@ -679,6 +679,44 @@ class TestPropertySheet:
""" """
pass pass
def test_15_DefaultValue(self):
"""
Tests that the default value is returned correctly
"""
portal = self.getPortal()
module = self.getPersonModule()
person = module.newContent(id='1', portal_type='Person')
def getFirstName(object, default=None):
"dummy method to check default is passed correctly"
print (object, default)
return default
from Products.ERP5.Document.Person import Person
Person.getFirstName = getFirstName
# test static method
self.assertEquals(person.getFirstName(), None)
self.assertEquals(person.getFirstName('foo'), 'foo')
self.assertEquals(person.getFirstName(default='foo'), 'foo')
# test dynamic method
self.assertEquals(person.getLastName(), None)
self.assertEquals(person.getLastName('foo'), 'foo')
self.assertEquals(person.getLastName(default='foo'), 'foo')
# test static method through getProperty
#self.assertEquals(person.getProperty('first_name'), None)
#self.assertEquals(person.getProperty('first_name', 'foo'), 'foo')
#self.assertEquals(person.getProperty('first_name', d='foo'), 'foo')
# test dynamic method through getProperty
#self.assertEquals(person.getProperty('last_name'), None)
#self.assertEquals(person.getProperty('last_name', 'foo'), 'foo')
#self.assertEquals(person.getProperty('last_name', d='foo'), 'foo')
# test simple property through getProperty
property_name = 'XXXthis_property_does_not_exist123123'
self.assertEquals(person.getProperty(property_name), None)
self.assertEquals(person.getProperty(property_name, 'foo'), 'foo')
self.assertEquals(person.getProperty(property_name, d='foo'), 'foo')
if __name__ == '__main__': if __name__ == '__main__':
framework() framework()
......
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