|
|
@@ -66,18 +66,30 @@ class MDBase(object): |
|
|
|
If the correct type is not found, a ValueError is raised.''' |
|
|
|
|
|
|
|
if isinstance(obj, cls): |
|
|
|
# XXX - copy? |
|
|
|
return obj |
|
|
|
obj = obj._obj |
|
|
|
|
|
|
|
ty = obj['type'] |
|
|
|
|
|
|
|
for i in cls.__subclasses__(): |
|
|
|
for i in MDBase.__subclasses__(): |
|
|
|
if i._type == ty: |
|
|
|
return i(obj) |
|
|
|
else: |
|
|
|
raise ValueError('Unable to find class for type %s' % |
|
|
|
`ty`) |
|
|
|
|
|
|
|
def new_version(self, *args): |
|
|
|
'''Add the property k as an additional one (or new on if |
|
|
|
first), with the value v.''' |
|
|
|
|
|
|
|
obj = copy.deepcopy(self._obj) |
|
|
|
|
|
|
|
for k, v in args: |
|
|
|
obj.setdefault(k, []).append(v) |
|
|
|
|
|
|
|
del obj['modified'] |
|
|
|
|
|
|
|
return self.create_obj(obj) |
|
|
|
|
|
|
|
def __getattr__(self, k): |
|
|
|
return self._obj[k] |
|
|
|
|
|
|
@@ -90,6 +102,9 @@ class MDBase(object): |
|
|
|
def __eq__(self, o): |
|
|
|
return cmp(self._obj, o) == 0 |
|
|
|
|
|
|
|
def __contains__(self, k): |
|
|
|
return k in self._obj |
|
|
|
|
|
|
|
def items(self, skipcommon=True): |
|
|
|
return [ (k, v) for k, v in self._obj.items() if k not in |
|
|
|
self._common_names ] |
|
|
@@ -317,10 +332,32 @@ class _TestCases(unittest.TestCase): |
|
|
|
'created_by_ref': self.created_by_ref, |
|
|
|
} |
|
|
|
origbase = copy.deepcopy(baseobj) |
|
|
|
|
|
|
|
# that when an MDBase object is created |
|
|
|
md = MDBase.create_obj(baseobj) |
|
|
|
|
|
|
|
# it doesn't modify the passed in object (when adding |
|
|
|
# generated properties) |
|
|
|
self.assertEqual(baseobj, origbase) |
|
|
|
|
|
|
|
# and it has the generted properties |
|
|
|
# Note: cannot mock the functions as they are already |
|
|
|
# referenced at creation time |
|
|
|
self.assertIn('uuid', md) |
|
|
|
self.assertIn('modified', md) |
|
|
|
|
|
|
|
# That you can create a new version using new_version |
|
|
|
md2 = md.new_version(('dc:creator', 'Jim Bob',)) |
|
|
|
|
|
|
|
# that they are different |
|
|
|
self.assertNotEqual(md, md2) |
|
|
|
|
|
|
|
# and that the new modified time is different from the old |
|
|
|
self.assertNotEqual(md.modified, md2.modified) |
|
|
|
|
|
|
|
# and that the modification is present |
|
|
|
self.assertEqual(md2['dc:creator'], [ 'Jim Bob' ]) |
|
|
|
|
|
|
|
def test_makehash(self): |
|
|
|
self.assertRaises(ValueError, ObjectStore.makehash, 'slkj') |
|
|
|
self.assertRaises(ValueError, ObjectStore.makehash, 'sha256:91751cee0a1ab8414400238a761411daa29643ab4b8243e9a91649e25be53ADA') |
|
|
@@ -376,7 +413,7 @@ class _TestCases(unittest.TestCase): |
|
|
|
r = byid |
|
|
|
|
|
|
|
self.assertEqual(r.uuid, '3e466e06-45de-4ecc-84ba-2d2a3d970e96') |
|
|
|
self.assertEqual(r['dc:creator'], 'John-Mark Gurney') |
|
|
|
self.assertEqual(r['dc:creator'], [ u'John-Mark Gurney' ]) |
|
|
|
|
|
|
|
fname = 'testfile.pasn1' |
|
|
|
objst.store(fname) |
|
|
|