Merge "Add __contains__ to ModelBase to fully behave like a dict"

This commit is contained in:
Jenkins 2014-06-29 08:37:08 +00:00 committed by Gerrit Code Review
commit 9c423b43e6
2 changed files with 14 additions and 0 deletions

View File

@ -54,6 +54,9 @@ class ModelBase(six.Iterator):
def __getitem__(self, key):
return getattr(self, key)
def __contains__(self, key):
return hasattr(self, key)
def get(self, key, default=None):
return getattr(self, key, default)

View File

@ -30,6 +30,7 @@ class ModelBaseTest(test_base.DbTestCase):
def test_modelbase_has_dict_methods(self):
dict_methods = ('__getitem__',
'__setitem__',
'__contains__',
'__iter__',
'get',
'next',
@ -51,6 +52,16 @@ class ModelBaseTest(test_base.DbTestCase):
for key in h.keys():
self.assertEqual(mb[key], h[key])
def test_modelbase_contains(self):
mb = models.ModelBase()
h = {'a': '1', 'b': '2'}
mb.update(h)
for key in h.keys():
# Test 'in' syntax (instead of using .assertIn)
self.assertTrue(key in mb)
self.assertFalse('non-existent-key' in mb)
def test_modelbase_iteritems(self):
self.skipTest("Requires DB")
mb = models.ModelBase()