|
8 | 8 | ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
9 | 9 | ''' Test for openers module '''
|
10 | 10 | import os
|
| 11 | +import contextlib |
11 | 12 | from gzip import GzipFile
|
12 | 13 | from bz2 import BZ2File
|
13 | 14 | from io import BytesIO, UnsupportedOperation
|
@@ -96,38 +97,55 @@ def test_BinOpener():
|
96 | 97 | BinOpener, 'test.txt', 'r')
|
97 | 98 |
|
98 | 99 |
|
| 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 | + |
99 | 120 | def test_Opener_gzip_type():
|
100 | 121 | # Test that BufferedGzipFile or IndexedGzipFile are used as appropriate
|
101 | 122 |
|
102 | 123 | data = 'this is some test data'
|
103 | 124 | fname = 'test.gz'
|
104 |
| - mockmod = mock.MagicMock() |
105 |
| - |
106 |
| - class MockIGZFile(object): |
107 |
| - def __init__(self, *args, **kwargs): |
108 |
| - pass |
109 | 125 |
|
110 | 126 | with InTemporaryDirectory():
|
111 | 127 |
|
112 | 128 | # make some test data
|
113 | 129 | with GzipFile(fname, mode='wb') as f:
|
114 | 130 | f.write(data.encode())
|
115 | 131 |
|
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) |
131 | 149 |
|
132 | 150 |
|
133 | 151 | class TestImageOpener:
|
|
0 commit comments