Commit 9331e8e4 authored by Jérome Perrin's avatar Jérome Perrin

session instance should raise AttributeError / KeyError when accessing non

existant attribute / item


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@17522 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 963f8c22
......@@ -57,14 +57,20 @@ class Session(UserDict):
""" Update current aquisition context. """
self._aq_context = aq_context
def __getattr__(self, key):
def __getattr__(self, key, default=_marker):
if key in self.data:
return self.__getitem__(key)
if default is not _marker:
return default
raise AttributeError, key
def __getitem__(self, key):
value = self.data.get(key, None)
if key is not None and hasattr(value, '__of__'):
if key in self.data:
value = self.data[key]
if hasattr(value, '__of__'):
value = value.__of__(self._aq_context)
return value
raise KeyError, key
def __setitem__(self, key, item):
self.data[key] = aq_base(item)
......
......@@ -146,6 +146,23 @@ class TestSessionTool(ERP5TypeTestCase):
session = portal_sessions[self.session_id]
self.assert_(0 == len(session.keys()))
def test_session_dict_interface(self):
session = self.portal.portal_sessions[self.session_id]
session['foo'] = 'Bar'
self.assertTrue('foo' in session)
self.assertEquals('Bar', session['foo'])
self.assertEquals('Bar', session.get('foo'))
self.assertFalse('bar' in session)
self.assertEquals('Default', session.get('bar', 'Default'))
self.assertRaises(KeyError, session.__getitem__, 'bar')
def test_session_getattr(self):
session = self.portal.portal_sessions[self.session_id]
session['foo'] = 'Bar'
self.assertEquals('Bar', session.foo)
self.assertEquals('Default', getattr(session, 'bar', 'Default'))
self.assertRaises(AttributeError, getattr, session, 'bar')
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestSessionTool))
......
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