Skip to content

Commit 72be9c8

Browse files
d-w-moorealanking
authored andcommitted
[#364][#365] test and documentation for admin_KW in metadata APIs
1 parent a74c416 commit 72be9c8

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

README.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,27 @@ of the (type of) catalog object they once annotated:
411411
>>> len(list( session.query(ResourceMeta) ))
412412
0
413413

414+
When altering a fetched iRODSMeta, we must copy it first to avoid errors, due to the fact the reference
415+
is cached by the iRODS object reference. A shallow copy is sufficient:
416+
417+
>>> meta = album.metadata.items()[0]
418+
>>> meta.units
419+
'quid'
420+
>>> import copy; meta = copy.copy(meta); meta.units = 'pounds sterling'
421+
>>> album.metadata[ meta.name ] = meta
422+
423+
Fortunately, as of PRC >= 1.1.4, we can simply do this instead:
424+
425+
>>> album.metadata.set( meta )
426+
427+
In versions of iRODS 4.2.12 and later, we can also do:
428+
429+
>>> album.metadata.set( meta, **{kw.ADMIN_KW: ''} )
430+
431+
or even:
432+
433+
>>> album.metadata(admin = True)[meta.name] = meta
434+
414435

415436
Atomic operations on metadata
416437
-----------------------------

irods/meta.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ def __init__(self, name, value, units=None, avu_id=None):
88
self.value = value
99
self.units = units
1010

11+
def __eq__(self, other):
12+
return tuple(self) == tuple(other)
13+
14+
def __iter__(self):
15+
yield self.name
16+
yield self.value
17+
if self.units: yield self.units
18+
1119
def __repr__(self):
1220
return "<iRODSMeta {avu_id} {name} {value} {units}>".format(**vars(self))
1321

irods/test/meta_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from irods.manager.metadata_manager import InvalidAtomicAVURequest
99
from irods.models import (DataObject, Collection, Resource)
1010
import irods.test.helpers as helpers
11+
import irods.keywords as kw
12+
from irods.session import iRODSSession
1113
from six.moves import range
1214
from six import PY3
1315

@@ -218,6 +220,41 @@ def test_remove_obj_meta(self):
218220
assert len(meta) == 0
219221

220222

223+
def test_metadata_manipulations_with_admin_kw__364__365(self):
224+
try:
225+
d = user = None
226+
adm = self.sess
227+
# Create a rodsuser, and a session for that roduser.
228+
user = adm.users.create ( 'bobby','rodsuser' )
229+
user.modify('password','bpass')
230+
with iRODSSession (port=adm.port,zone=adm.zone,host=adm.host, user=user.name,password='bpass') as ses:
231+
# Create a data object owned by the rodsuser. Set AVUs in various ways and guarantee each attempt
232+
# has the desired effect.
233+
d = ses.data_objects.create('/{adm.zone}/home/{user.name}/testfile'.format(**locals()))
234+
235+
d.metadata.set('a','aa','1')
236+
self.assertIn(('a','aa','1'), d.metadata.items())
237+
238+
d.metadata.set('a','aa')
239+
self.assertEqual([('a','aa')], [tuple(_) for _ in d.metadata.items()])
240+
241+
d.metadata['a'] = iRODSMeta('a','bb')
242+
self.assertEqual([('a','bb')], [tuple(_) for _ in d.metadata.items()])
243+
244+
# Now the admin does two AVU-set operations. A successful test of these operations' success
245+
# includes that both ('x','y') has been added and ('a','b','c') has overwritten ('a','bb').
246+
247+
da = adm.data_objects.get(d.path)
248+
da.metadata.set('a','b','c',**{kw.ADMIN_KW:''})
249+
da.metadata(admin = True)['x'] = iRODSMeta('x','y')
250+
d = ses.data_objects.get(d.path) # assure metadata are not cached
251+
self.assertEqual(set([('x','y'), ('a','b','c')]),
252+
set(tuple(_) for _ in d.metadata.items()))
253+
finally:
254+
if d: d.unlink(force=True)
255+
if user: user.remove()
256+
257+
221258
def test_add_coll_meta(self):
222259
# add metadata to test collection
223260
self.sess.metadata.add(Collection, self.coll_path,

0 commit comments

Comments
 (0)