Skip to content

Commit 39dea17

Browse files
committed
TEST: Update ArrayProxy/Opener tests to work with new HAVE_INDEXED_GZIP flag
1 parent d7c7cac commit 39dea17

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

nibabel/tests/test_arrayproxy.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,21 +402,23 @@ def test_keep_file_open_default():
402402
# its default value
403403
dtype = np.float32
404404
data = np.arange(1000, dtype=dtype).reshape((10, 10, 10))
405-
voxels = np.random.randint(0, 10, (10, 3))
406-
mockmod = mock.MagicMock()
407405
with InTemporaryDirectory():
408406
fname = 'testdata.gz'
409407
with gzip.open(fname, 'wb') as fobj:
410408
fobj.write(data.tostring(order='F'))
411409
# If have_indexed_gzip, then keep_file_open should be True
412-
with mock.patch.dict('sys.modules', {'indexed_gzip' : mockmod}), \
413-
mock.patch('indexed_gzip.SafeIndexedGzipFile', gzip.GzipFile):
410+
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', True), \
411+
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', True), \
412+
mock.patch('nibabel.openers.SafeIndexedGzipFile', gzip.GzipFile,
413+
create=True):
414414
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
415415
assert proxy._keep_file_open
416416
proxy = ArrayProxy(fname, ((10, 10, 10), dtype), keep_file_open='auto')
417417
assert proxy._keep_file_open
418418
# If no have_indexed_gzip, then keep_file_open should be False
419-
with mock.patch.dict('sys.modules', {'indexed_gzip' : None}):
419+
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', False), \
420+
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', False), \
421+
mock.patch('nibabel.openers.SafeIndexedGzipFile', None, create=True):
420422
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
421423
assert not proxy._keep_file_open
422424
proxy = ArrayProxy(fname, ((10, 10, 10), dtype), keep_file_open='auto')

nibabel/tests/test_openers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,18 @@ def __init__(self, *args, **kwargs):
114114
f.write(data.encode())
115115

116116
# test with indexd_gzip not present
117-
with mock.patch.dict('sys.modules', {'indexed_gzip' : None}):
117+
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', False), \
118+
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', False), \
119+
mock.patch('nibabel.openers.SafeIndexedGzipFile', None,
120+
create=True):
118121
assert isinstance(Opener(fname, mode='rb').fobj, GzipFile)
119122
assert isinstance(Opener(fname, mode='wb').fobj, GzipFile)
120123

121124
# test with indexd_gzip present
122-
with mock.patch.dict('sys.modules', {'indexed_gzip' : mockmod}), \
123-
mock.patch('indexed_gzip.SafeIndexedGzipFile', MockIGZFile):
125+
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', True), \
126+
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', True), \
127+
mock.patch('nibabel.openers.SafeIndexedGzipFile', MockIGZFile,
128+
create=True):
124129
assert isinstance(Opener(fname, mode='rb').fobj, MockIGZFile)
125130
assert isinstance(Opener(fname, mode='wb').fobj, GzipFile)
126131

0 commit comments

Comments
 (0)