Skip to content

Commit bd39417

Browse files
committed
TEST: Unit test to make sure that GzipFile/SafeIndexedGzipFile classes are
created appropriately
1 parent 172bb61 commit bd39417

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

nibabel/tests/test_openers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from nose.tools import (assert_true, assert_false, assert_equal,
2121
assert_not_equal, assert_raises)
2222
from ..testing import error_warnings
23+
import mock
2324

2425

2526
class Lunk(object):
@@ -95,6 +96,35 @@ def test_BinOpener():
9596
BinOpener, 'test.txt', 'r')
9697

9798

99+
def test_Opener_gzip_type():
100+
# Test that BufferedGzipFile or IndexedGzipFile are used as appropriate
101+
102+
data = 'this is some test data'
103+
fname = 'test.gz'
104+
mockmod = mock.MagicMock()
105+
106+
class MockClass(object):
107+
def __init__(self, *args, **kwargs):
108+
pass
109+
110+
with InTemporaryDirectory():
111+
112+
# make some test data
113+
with GzipFile(fname, mode='wb') as f:
114+
f.write(data.encode())
115+
116+
# test with indexd_gzip not present
117+
with mock.patch.dict('sys.modules', {'indexed_gzip' : None}):
118+
assert isinstance(Opener(fname, mode='rb').fobj, GzipFile)
119+
assert isinstance(Opener(fname, mode='wb').fobj, GzipFile)
120+
121+
# test with indexd_gzip present
122+
with mock.patch.dict('sys.modules', {'indexed_gzip' : mockmod}), \
123+
mock.patch('indexed_gzip.SafeIndexedGzipFile', MockClass):
124+
assert isinstance(Opener(fname, mode='rb').fobj, MockClass)
125+
assert isinstance(Opener(fname, mode='wb').fobj, GzipFile)
126+
127+
98128
class TestImageOpener:
99129

100130
def setUp(self):

0 commit comments

Comments
 (0)