Skip to content

Commit a7cd91f

Browse files
committed
TEST: Check old GiftiMetaData.data interface works
1 parent c90b75d commit a7cd91f

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

nibabel/gifti/tests/test_gifti.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,81 @@ def test_metadata():
245245
md.get_metadata()
246246

247247

248+
def test_metadata_list_interface():
249+
md = GiftiMetaData(key='value')
250+
with pytest.warns(FutureWarning):
251+
mdlist = md.data
252+
assert len(mdlist) == 1
253+
assert mdlist[0].name == 'key'
254+
assert mdlist[0].value == 'value'
255+
256+
# Modify elements in-place
257+
mdlist[0].name = 'foo'
258+
assert mdlist[0].name == 'foo'
259+
assert 'foo' in md
260+
assert 'key' not in md
261+
assert md['foo'] == 'value'
262+
mdlist[0].value = 'bar'
263+
assert mdlist[0].value == 'bar'
264+
assert md['foo'] == 'bar'
265+
266+
# Append new NVPair
267+
nvpair = GiftiNVPairs('key', 'value')
268+
mdlist.append(nvpair)
269+
assert len(mdlist) == 2
270+
assert mdlist[1].name == 'key'
271+
assert mdlist[1].value == 'value'
272+
assert len(md) == 2
273+
assert md == {'foo': 'bar', 'key': 'value'}
274+
275+
# Clearing empties both
276+
mdlist.clear()
277+
assert len(mdlist) == 0
278+
assert len(md) == 0
279+
280+
# Extension adds multiple keys
281+
foobar = GiftiNVPairs('foo', 'bar')
282+
mdlist.extend([nvpair, foobar])
283+
assert len(mdlist) == 2
284+
assert len(md) == 2
285+
assert md == {'key': 'value', 'foo': 'bar'}
286+
287+
# Insertion updates list order, though we don't attempt to preserve it in the dict
288+
lastone = GiftiNVPairs('last', 'one')
289+
mdlist.insert(1, lastone)
290+
assert len(mdlist) == 3
291+
assert len(md) == 3
292+
assert mdlist[1].name == 'last'
293+
assert mdlist[1].value == 'one'
294+
assert md == {'key': 'value', 'foo': 'bar', 'last': 'one'}
295+
296+
# Popping returns a pair
297+
mypair = mdlist.pop(0)
298+
assert isinstance(mypair, GiftiNVPairs)
299+
assert mypair.name == 'key'
300+
assert mypair.value == 'value'
301+
assert len(mdlist) == 2
302+
assert len(md) == 2
303+
assert 'key' not in md
304+
assert md == {'foo': 'bar', 'last': 'one'}
305+
# Modifying the pair now does not affect md
306+
mypair.name = 'completelynew'
307+
mypair.value = 'strings'
308+
assert 'completelynew' not in md
309+
assert md == {'foo': 'bar', 'last': 'one'}
310+
# Check popping from the end (lastone inserted before foobar)
311+
lastpair = mdlist.pop()
312+
assert len(mdlist) == 1
313+
assert len(md) == 1
314+
assert md == {'last': 'one'}
315+
316+
# And let's remove an old pair with a new object
317+
lastoneagain = GiftiNVPairs('last', 'one')
318+
mdlist.remove(lastoneagain)
319+
assert len(mdlist) == 0
320+
assert len(md) == 0
321+
322+
248323
def test_gifti_label_rgba():
249324
rgba = np.random.rand(4)
250325
kwargs = dict(zip(['red', 'green', 'blue', 'alpha'], rgba))

0 commit comments

Comments
 (0)