Skip to content

Commit 2f2900d

Browse files
committed
TEST: Test content, text and json() access
1 parent efacb5a commit 2f2900d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

nibabel/tests/test_nifti1.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,32 @@ def test_ext_eq():
12241224
assert not ext == ext2
12251225

12261226

1227+
def test_extension_content_access():
1228+
ext = Nifti1Extension('comment', b'123')
1229+
# Unmangled content access
1230+
assert ext.get_content() == b'123'
1231+
1232+
# Raw, text and JSON access
1233+
assert ext.content == b'123'
1234+
assert ext.text == '123'
1235+
assert ext.json() == 123
1236+
1237+
# Encoding can be set
1238+
ext.encoding = 'ascii'
1239+
assert ext.text == '123'
1240+
1241+
# Test that encoding errors are caught
1242+
ascii_ext = Nifti1Extension('comment', 'hôpital'.encode('utf-8'))
1243+
ascii_ext.encoding = 'ascii'
1244+
with pytest.raises(UnicodeDecodeError):
1245+
ascii_ext.text
1246+
1247+
json_ext = Nifti1Extension('unknown', b'{"a": 1}')
1248+
assert json_ext.content == b'{"a": 1}'
1249+
assert json_ext.text == '{"a": 1}'
1250+
assert json_ext.json() == {'a': 1}
1251+
1252+
12271253
def test_extension_codes():
12281254
for k in extension_codes.keys():
12291255
Nifti1Extension(k, 'somevalue')

0 commit comments

Comments
 (0)