Skip to content

Commit 67405b0

Browse files
committed
TEST: Small re-arrangements and code clean-up
1 parent dc07879 commit 67405b0

File tree

2 files changed

+42
-46
lines changed

2 files changed

+42
-46
lines changed

nibabel/tests/test_arrayproxy.py

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import warnings
1414
import gzip
15+
import contextlib
1516

1617
import pickle
1718
from io import BytesIO
@@ -24,11 +25,12 @@
2425
from ..openers import ImageOpener
2526
from ..nifti1 import Nifti1Header
2627

28+
import mock
29+
2730
from numpy.testing import assert_array_equal, assert_array_almost_equal
2831
from nose.tools import (assert_true, assert_false, assert_equal,
2932
assert_not_equal, assert_raises)
3033
from nibabel.testing import VIRAL_MEMMAP
31-
import mock
3234

3335
from .test_fileslice import slicer_samples
3436

@@ -338,18 +340,18 @@ def check_mmap(hdr, offset, proxy_class,
338340
# created
339341
class CountingImageOpener(ImageOpener):
340342

341-
numOpeners = 0
343+
num_openers = 0
342344

343345
def __init__(self, *args, **kwargs):
344346

345347
super(CountingImageOpener, self).__init__(*args, **kwargs)
346-
CountingImageOpener.numOpeners += 1
348+
CountingImageOpener.num_openers += 1
347349

348350

349351
def test_keep_file_open_true_false_invalid():
350352
# Test the behaviour of the keep_file_open __init__ flag, when it is set to
351353
# True or False.
352-
CountingImageOpener.numOpeners = 0
354+
CountingImageOpener.num_openers = 0
353355
fname = 'testdata'
354356
dtype = np.float32
355357
data = np.arange(1000, dtype=dtype).reshape((10, 10, 10))
@@ -367,15 +369,15 @@ def test_keep_file_open_true_false_invalid():
367369
for i in range(voxels.shape[0]):
368370
x , y, z = [int(c) for c in voxels[i, :]]
369371
assert proxy_no_kfp[x, y, z] == x * 100 + y * 10 + z
370-
assert CountingImageOpener.numOpeners == i + 1
371-
CountingImageOpener.numOpeners = 0
372+
assert CountingImageOpener.num_openers == i + 1
373+
CountingImageOpener.num_openers = 0
372374
proxy_kfp = ArrayProxy(fname, ((10, 10, 10), dtype),
373375
keep_file_open=True)
374376
assert proxy_kfp._keep_file_open
375377
for i in range(voxels.shape[0]):
376378
x , y, z = [int(c) for c in voxels[i, :]]
377379
assert proxy_kfp[x, y, z] == x * 100 + y * 10 + z
378-
assert CountingImageOpener.numOpeners == 1
380+
assert CountingImageOpener.num_openers == 1
379381
# Test that the keep_file_open flag has no effect if an open file
380382
# handle is passed in
381383
with open(fname, 'rb') as fobj:
@@ -410,6 +412,28 @@ def test_keep_file_open_true_false_invalid():
410412
ArrayProxy(fname, ((10, 10, 10), dtype), keep_file_open='cauto')
411413

412414

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+
430+
@contextlib.contextmanager
431+
def patch_keep_file_open_default(value):
432+
# Patch arrayproxy.KEEP_FILE_OPEN_DEFAULT with the given value
433+
with mock.patch('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT', value):
434+
yield
435+
436+
413437
def test_keep_file_open_auto():
414438
# Test the behaviour of the keep_file_open __init__ flag, when it is set to
415439
# 'auto'
@@ -420,18 +444,12 @@ def test_keep_file_open_auto():
420444
with gzip.open(fname, 'wb') as fobj:
421445
fobj.write(data.tostring(order='F'))
422446
# If have_indexed_gzip, then keep_file_open should be True
423-
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', True), \
424-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', True), \
425-
mock.patch('nibabel.openers.SafeIndexedGzipFile', gzip.GzipFile,
426-
create=True):
447+
with patch_indexed_gzip(True):
427448
proxy = ArrayProxy(fname, ((10, 10, 10), dtype),
428449
keep_file_open='auto')
429450
assert proxy._keep_file_open
430451
# If no have_indexed_gzip, then keep_file_open should be False
431-
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', False), \
432-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', False), \
433-
mock.patch('nibabel.openers.SafeIndexedGzipFile', None,
434-
create=True):
452+
with patch_indexed_gzip(False):
435453
proxy = ArrayProxy(fname, ((10, 10, 10), dtype),
436454
keep_file_open='auto')
437455
assert not proxy._keep_file_open
@@ -450,56 +468,34 @@ def test_keep_file_open_default():
450468
# keep_file_open to be False, regardless of whether or not indexed_gzip
451469
# is present
452470
assert KEEP_FILE_OPEN_DEFAULT is False
453-
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', False), \
454-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', False), \
455-
mock.patch('nibabel.openers.SafeIndexedGzipFile', None,
456-
create=True):
471+
with patch_indexed_gzip(False):
457472
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
458473
assert not proxy._keep_file_open
459-
with mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', True), \
460-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', True), \
461-
mock.patch('nibabel.openers.SafeIndexedGzipFile', gzip.GzipFile,
462-
create=True):
474+
with patch_indexed_gzip(True):
463475
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
464476
assert not proxy._keep_file_open
465477
# KEEP_FILE_OPEN_DEFAULT=True should cause keep_file_open to be True,
466478
# regardless of whether or not indexed_gzip is present
467-
with mock.patch('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT', True), \
468-
mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', True), \
469-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', True), \
470-
mock.patch('nibabel.openers.SafeIndexedGzipFile', gzip.GzipFile,
471-
create=True):
479+
with patch_keep_file_open_default(True), patch_indexed_gzip(True):
472480
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
473481
assert proxy._keep_file_open
474-
with mock.patch('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT', True), \
475-
mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', False), \
476-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', False), \
477-
mock.patch('nibabel.openers.SafeIndexedGzipFile', None,
478-
create=True):
482+
with patch_keep_file_open_default(True), patch_indexed_gzip(False):
479483
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
480484
assert proxy._keep_file_open
481485
# KEEP_FILE_OPEN_DEFAULT=auto should cause keep_file_open to be True
482486
# or False, depending on whether indeed_gzip is present,
483-
with mock.patch('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT', 'auto'), \
484-
mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', True), \
485-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', True), \
486-
mock.patch('nibabel.openers.SafeIndexedGzipFile', gzip.GzipFile,
487-
create=True):
487+
with patch_keep_file_open_default('auto'), patch_indexed_gzip(True):
488488
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
489489
assert proxy._keep_file_open
490-
with mock.patch('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT', 'auto'), \
491-
mock.patch('nibabel.openers.HAVE_INDEXED_GZIP', False), \
492-
mock.patch('nibabel.arrayproxy.HAVE_INDEXED_GZIP', False), \
493-
mock.patch('nibabel.openers.SafeIndexedGzipFile', None,
494-
create=True):
490+
with patch_keep_file_open_default('auto'), patch_indexed_gzip(False):
495491
proxy = ArrayProxy(fname, ((10, 10, 10), dtype))
496492
assert not proxy._keep_file_open
497493
# KEEP_FILE_OPEN_DEFAULT=any other value should cuse an error to be
498494
# raised
499-
with mock.patch('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT', 'badval'):
495+
with patch_keep_file_open_default('badvalue'):
500496
assert_raises(ValueError, ArrayProxy, fname, ((10, 10, 10),
501497
dtype))
502-
with mock.patch('nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT', None):
498+
with patch_keep_file_open_default(None):
503499
assert_raises(ValueError, ArrayProxy, fname, ((10, 10, 10),
504500
dtype))
505501

nibabel/tests/test_openers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
from ..tmpdirs import InTemporaryDirectory
1818
from ..volumeutils import BinOpener
1919

20+
import mock
2021
from nose.tools import (assert_true, assert_false, assert_equal,
2122
assert_not_equal, assert_raises)
2223
from ..testing import error_warnings
23-
import mock
2424

2525

2626
class Lunk(object):

0 commit comments

Comments
 (0)