Skip to content

Commit 8da5d5c

Browse files
author
Ben Cipollini
committed
Add simple tests for adding a new file association.
1 parent 094f832 commit 8da5d5c

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

nibabel/tests/test_openers.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from ..openers import Opener, ImageOpener
2121

22+
from nose.case import Test
2223
from nose.tools import (assert_true, assert_false, assert_equal,
2324
assert_not_equal, assert_raises)
2425

@@ -84,13 +85,39 @@ def test_Opener_various():
8485
# Just check there is a fileno
8586
assert_not_equal(fobj.fileno(), 0)
8687

87-
def test_ImageOpener():
88-
# Test that ImageOpener does add '.mgz' as gzipped file type
89-
with InTemporaryDirectory():
90-
with ImageOpener('test.gz', 'w') as fobj:
91-
assert_true(hasattr(fobj.fobj, 'compress'))
92-
with ImageOpener('test.mgz', 'w') as fobj:
93-
assert_true(hasattr(fobj.fobj, 'compress'))
88+
89+
class TestImageOpener:
90+
def setUp(self):
91+
self.compress_ext_map = ImageOpener.compress_ext_map.copy()
92+
93+
def teardown(self):
94+
ImageOpener.compress_ext_map = self.compress_ext_map
95+
96+
def test_vanilla(self):
97+
# Test that ImageOpener does add '.mgz' as gzipped file type
98+
with InTemporaryDirectory():
99+
with ImageOpener('test.gz', 'w') as fobj:
100+
assert_true(hasattr(fobj.fobj, 'compress'))
101+
with ImageOpener('test.mgz', 'w') as fobj:
102+
assert_true(hasattr(fobj.fobj, 'compress'))
103+
104+
def test_new_association(self):
105+
def file_opener(fileish, mode):
106+
return open(fileish, mode)
107+
108+
# Add the association
109+
n_associations = len(ImageOpener.compress_ext_map)
110+
dec = ImageOpener.register_ext_from_image('.foo',
111+
(file_opener, ('mode',)))
112+
dec(self.__class__)
113+
assert_equal(n_associations + 1, len(ImageOpener.compress_ext_map))
114+
assert_true('.foo' in ImageOpener.compress_ext_map)
115+
116+
with InTemporaryDirectory():
117+
with ImageOpener('test.foo', 'w'):
118+
pass
119+
assert_true(os.path.exists('test.foo'))
120+
94121

95122
def test_file_like_wrapper():
96123
# Test wrapper using BytesIO (full API)

0 commit comments

Comments
 (0)