359: Added Object.primary_key instance attribute; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-09-25 01:44:53 +00:00
parent 4267422793
commit 54c97b4948
2 changed files with 63 additions and 0 deletions

View File

@ -514,10 +514,12 @@ class Object(plugable.Plugin):
'methods',
'properties',
'params'
'primary_key',
))
methods = None
properties = None
params = None
primary_key = None
takes_params = tuple()
def set_api(self, api):
@ -531,6 +533,16 @@ class Object(plugable.Plugin):
self.params = plugable.NameSpace(
self.__get_params(), sort=False
)
pkeys = filter(lambda p: p.primary_key, self.params())
if len(pkeys) > 1:
raise ValueError(
'%s (Object) has multiple primary keys: %s' % (
self.name,
', '.join(p.name for p in pkeys),
)
)
if len(pkeys) == 1:
self.primary_key = pkeys[0]
def __get_attrs(self, name):
namespace = getattr(self.api, name)

View File

@ -858,6 +858,57 @@ class test_Object(ClassChecker):
assert p.required is True
assert p.multivalue is False
def test_primary_key(self):
"""
Test the `frontend.Object.primary_key` attribute.
"""
api = plugable.API(
frontend.Method,
frontend.Property,
)
api.finalize()
# Test with no primary keys:
class example1(self.cls):
takes_params = (
'one',
'two',
)
o = example1()
o.set_api(api)
assert o.primary_key is None
# Test with 1 primary key:
class example2(self.cls):
takes_params = (
'one',
'two',
frontend.Param('three',
primary_key=True,
),
'four',
)
o = example2()
o.set_api(api)
pk = o.primary_key
assert isinstance(pk, frontend.Param)
assert pk.name == 'three'
assert pk.primary_key is True
assert o.params[2] is o.primary_key
# Test with multiple primary_key:
class example3(self.cls):
takes_params = (
frontend.Param('one', primary_key=True),
frontend.Param('two', primary_key=True),
'three',
frontend.Param('four', primary_key=True),
)
o = example3()
e = raises(ValueError, o.set_api, api)
assert str(e) == \
'example3 (Object) has multiple primary keys: one, two, four'
class test_Attribute(ClassChecker):
"""