Browse Source

properties need to be lists, as there might be multiple ones..

add new_version that creates a new version with modifications
main
John-Mark Gurney 5 years ago
parent
commit
5359b43697
2 changed files with 42 additions and 5 deletions
  1. +41
    -4
      ui/cli.py
  2. +1
    -1
      ui/fixtures/genfixtures.py

+ 41
- 4
ui/cli.py View File

@@ -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)


+ 1
- 1
ui/fixtures/genfixtures.py View File

@@ -11,7 +11,7 @@ map(objst.loadobj,
'uuid': '3e466e06-45de-4ecc-84ba-2d2a3d970e96',
'created_by_ref': cbr,
'modified': datetime.datetime(2019, 5, 31, 14, 5, 3),
'dc:creator': u'John-Mark Gurney',
'dc:creator': [ u'John-Mark Gurney' ],
'hashes': [ 'sha256:91751cee0a1ab8414400238a761411daa29643ab4b8243e9a91649e25be53ada', 'sha512:7d5768d47b6bc27dc4fa7e9732cfa2de506ca262a2749cb108923e5dddffde842bbfee6cb8d692fb43aca0f12946c521cce2633887914ca1f96898478d10ad3f' ],
'lang': 'en'
}


Loading…
Cancel
Save