Skip to content

Commit 198d903

Browse files
committed
TEST: Adjusted tests which make sure that Opener uses GzipFile/IndexedGzipFile
classes as appropriate
1 parent bc49c60 commit 198d903

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

nibabel/tests/test_arrayproxy.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from nibabel.testing import VIRAL_MEMMAP
3434

3535
from .test_fileslice import slicer_samples
36+
from .test_openers import patch_indexed_gzip
3637

3738

3839
class FunkyHeader(object):
@@ -412,21 +413,6 @@ def test_keep_file_open_true_false_invalid():
412413
ArrayProxy(fname, ((10, 10, 10), dtype), keep_file_open='cauto')
413414

414415

415-
@contextlib.contextmanager
416-
def patch_indexed_gzip(state):
417-
# Make it look like we do (state==True) or do not (state==False) have
418-
# the indexed gzip module.
419-
if state:
420-
values = (True, True, gzip.GzipFile)
421-
else:
422-
values = (False, False, None)
423-
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', values[0]), \
424-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', values[1]), \
425-
mock.patch('nibabel.openers.SafeIndexedGzipFile', values[2],
426-
create=True):
427-
yield
428-
429-
430416
@contextlib.contextmanager
431417
def patch_keep_file_open_default(value):
432418
# Patch arrayproxy.KEEP_FILE_OPEN_DEFAULT with the given value

nibabel/tests/test_openers.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
''' Test for openers module '''
1010
import os
11+
import contextlib
1112
from gzip import GzipFile
1213
from bz2 import BZ2File
1314
from io import BytesIO, UnsupportedOperation
@@ -96,38 +97,55 @@ def test_BinOpener():
9697
BinOpener, 'test.txt', 'r')
9798

9899

100+
class MockIndexedGzipFile(object):
101+
def __init__(self, *args, **kwargs):
102+
pass
103+
104+
105+
@contextlib.contextmanager
106+
def patch_indexed_gzip(state):
107+
# Make it look like we do (state==True) or do not (state==False) have
108+
# the indexed gzip module.
109+
if state:
110+
values = (True, True, MockIndexedGzipFile)
111+
else:
112+
values = (False, False, GzipFile)
113+
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', values[0]), \
114+
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', values[1]), \
115+
mock.patch('nibabel.openers.SafeIndexedGzipFile', values[2],
116+
create=True):
117+
yield
118+
119+
99120
def test_Opener_gzip_type():
100121
# Test that BufferedGzipFile or IndexedGzipFile are used as appropriate
101122

102123
data = 'this is some test data'
103124
fname = 'test.gz'
104-
mockmod = mock.MagicMock()
105-
106-
class MockIGZFile(object):
107-
def __init__(self, *args, **kwargs):
108-
pass
109125

110126
with InTemporaryDirectory():
111127

112128
# make some test data
113129
with GzipFile(fname, mode='wb') as f:
114130
f.write(data.encode())
115131

116-
# test with indexd_gzip not present
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):
121-
assert isinstance(Opener(fname, mode='rb').fobj, GzipFile)
122-
assert isinstance(Opener(fname, mode='wb').fobj, GzipFile)
123-
124-
# test with indexd_gzip present
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):
129-
assert isinstance(Opener(fname, mode='rb').fobj, MockIGZFile)
130-
assert isinstance(Opener(fname, mode='wb').fobj, GzipFile)
132+
# Each test is specified by a tuple containing:
133+
# (indexed_gzip present, Opener kwargs, expected file type)
134+
tests = [
135+
(False, {'mode' : 'rb', 'keep_open' : True}, GzipFile),
136+
(False, {'mode' : 'rb', 'keep_open' : False}, GzipFile),
137+
(False, {'mode' : 'wb', 'keep_open' : True}, GzipFile),
138+
(False, {'mode' : 'wb', 'keep_open' : False}, GzipFile),
139+
(True, {'mode' : 'rb', 'keep_open' : True}, MockIndexedGzipFile),
140+
(True, {'mode' : 'rb', 'keep_open' : False}, GzipFile),
141+
(True, {'mode' : 'wb', 'keep_open' : True}, GzipFile),
142+
(True, {'mode' : 'wb', 'keep_open' : False}, GzipFile),
143+
]
144+
145+
for test in tests:
146+
igzip_present, kwargs, expected = test
147+
with patch_indexed_gzip(igzip_present):
148+
assert isinstance(Opener(fname, **kwargs).fobj, expected)
131149

132150

133151
class TestImageOpener:

0 commit comments

Comments
 (0)