Skip to content

Commit c943e9d

Browse files
committed
TEST: Generate file when no examples available
1 parent b729779 commit c943e9d

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

nibabel/filebasedimages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def from_bytes(klass, bytestring):
567567
bstring : bytes
568568
Byte string containing the on-disk representation of an image
569569
"""
570-
bio = io.BytesIO(bstring)
570+
bio = io.BytesIO(bytestring)
571571
file_map = klass.make_file_map({'image': bio, 'header': bio})
572572
return klass.from_file_map(file_map)
573573

nibabel/tests/test_filebasedimages.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import numpy as np
77

8-
from ..filebasedimages import FileBasedHeader, FileBasedImage
8+
from ..filebasedimages import FileBasedHeader, FileBasedImage, SerializableImage
99

1010
from .test_image_api import GenericImageAPI, SerializeMixin
1111

@@ -50,8 +50,11 @@ def set_data_dtype(self, dtype):
5050
self.arr = self.arr.astype(dtype)
5151

5252

53-
class TestFBImageAPI(GenericImageAPI,
54-
SerializeMixin):
53+
class SerializableNumpyImage(FBNumpyImage, SerializableImage):
54+
pass
55+
56+
57+
class TestFBImageAPI(GenericImageAPI):
5558
""" Validation for FileBasedImage instances
5659
"""
5760
# A callable returning an image from ``image_maker(data, header)``
@@ -81,6 +84,10 @@ def obj_params(self):
8184
yield func, params
8285

8386

87+
class TestSerializableImageAPI(TestFBImageAPI, SerializeMixin):
88+
image_maker = SerializableNumpyImage
89+
90+
8491
def test_filebased_header():
8592
# Test stuff about the default FileBasedHeader
8693

nibabel/tests/test_image_api.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -505,24 +505,36 @@ def validate_to_bytes(self, imaker, params):
505505
assert serialized == file_contents
506506

507507
def validate_from_bytes(self, imaker, params):
508-
for img_params in self.example_images:
509-
img_a = self.klass.from_filename(img_params['fname'])
510-
with open(img_params['fname'], 'rb') as fobj:
511-
img_b = self.klass.from_bytes(fobj.read())
508+
img = imaker()
509+
with InTemporaryDirectory():
510+
fname = 'img' + self.standard_extension
511+
img.to_filename(fname)
512512

513-
assert img_a.header == img_b.header
514-
assert np.array_equal(img_a.get_data(), img_b.get_data())
513+
all_images = list(getattr(self, 'example_images', [])) + [{'fname': fname}]
514+
for img_params in all_images:
515+
img_a = self.klass.from_filename(img_params['fname'])
516+
with open(img_params['fname'], 'rb') as fobj:
517+
img_b = self.klass.from_bytes(fobj.read())
515518

516-
def validate_round_trip(self, imaker, params):
517-
for img_params in self.example_images:
518-
img_a = self.klass.from_filename(img_params['fname'])
519-
bytes_a = img_a.to_bytes()
519+
assert img_a.header == img_b.header
520+
assert np.array_equal(img_a.get_data(), img_b.get_data())
521+
522+
def validate_to_from_bytes(self, imaker, params):
523+
img = imaker()
524+
with InTemporaryDirectory():
525+
fname = 'img' + self.standard_extension
526+
img.to_filename(fname)
527+
528+
all_images = list(getattr(self, 'example_images', [])) + [{'fname': fname}]
529+
for img_params in all_images:
530+
img_a = self.klass.from_filename(img_params['fname'])
531+
bytes_a = img_a.to_bytes()
520532

521-
img_b = self.klass.from_bytes(bytes_a)
533+
img_b = self.klass.from_bytes(bytes_a)
522534

523-
assert img_b.to_bytes() == bytes_a
524-
assert img_a.header == img_b.header
525-
assert np.array_equal(img_a.get_data(), img_b.get_data())
535+
assert img_b.to_bytes() == bytes_a
536+
assert img_a.header == img_b.header
537+
assert np.array_equal(img_a.get_data(), img_b.get_data())
526538

527539

528540
class LoadImageAPI(GenericImageAPI,

0 commit comments

Comments
 (0)