Skip to content

Commit aacf014

Browse files
committed
Increase test coverage
1 parent 4d93676 commit aacf014

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

nibabel/streamlines/tck.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ def _write_header(fileobj, header):
256256
msg = "Key-value pairs cannot contain '\\n':\n{}".format(out)
257257
raise HeaderError(msg)
258258

259-
if out.count(":") > len(lines): # : only one per line.
259+
if out.count(":") > len(lines) - 1:
260+
# : only one per line (except the last one which contains END).
260261
msg = "Key-value pairs cannot contain ':':\n{}".format(out)
261262
raise HeaderError(msg)
262263

@@ -428,7 +429,7 @@ def _read(cls, fileobj, header, buffer_size=4):
428429
n_streams += len(point_parts)
429430

430431
if not buffs[-1] == eof_marker:
431-
raise DataError('Expecting end-of-file marker ' 'inf inf inf')
432+
raise DataError("Expecting end-of-file marker 'inf inf inf'")
432433

433434
# In case the 'count' field was not provided.
434435
header[Field.NB_STREAMLINES] = n_streams

nibabel/streamlines/tests/test_tck.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
from ..array_sequence import ArraySequence
1010
from ..tractogram import Tractogram
11-
from ..tractogram_file import HeaderWarning, DataError
11+
from ..tractogram_file import HeaderWarning, HeaderError
12+
from ..tractogram_file import DataError
1213

1314
from .. import tck as tck_module
1415
from ..tck import TckFile
@@ -84,15 +85,20 @@ def test_load_simple_file_in_big_endian(self):
8485
def test_load_file_with_wrong_information(self):
8586
tck_file = open(DATA['simple_tck_fname'], 'rb').read()
8687

87-
# Simulate a TCK file where `datatype` was incorrectly provided.
88+
# Simulate a TCK file where `datatype` has not the right endianness.
8889
new_tck_file = tck_file.replace(asbytes("Float32LE"),
8990
asbytes("Float32BE"))
9091
assert_raises(DataError, TckFile.load, BytesIO(new_tck_file))
9192

93+
# Simulate a TCK file with unsupported `datatype`.
94+
new_tck_file = tck_file.replace(asbytes("Float32LE"),
95+
asbytes("int32"))
96+
assert_raises(HeaderError, TckFile.load, BytesIO(new_tck_file))
97+
9298
# Simulate a TCK file with no `datatype` field.
9399
new_tck_file = tck_file.replace(b"datatype: Float32LE\n", b"")
94-
# Adjust data offset
95-
new_tck_file = new_tck_file.replace(b"\nfile: . 67\n", b"\nfile: . 47\n")
100+
# Need to adjust data offset.
101+
new_tck_file = new_tck_file.replace(b"file: . 67\n", b"file: . 47\n")
96102
with clear_and_catch_warnings(record=True, modules=[tck_module]) as w:
97103
tck = TckFile.load(BytesIO(new_tck_file))
98104
assert_equal(len(w), 1)
@@ -101,7 +107,6 @@ def test_load_file_with_wrong_information(self):
101107
assert_array_equal(tck.header['datatype'], "Float32LE")
102108

103109
# Simulate a TCK file with no `file` field.
104-
# Adjust data offset
105110
new_tck_file = tck_file.replace(b"\nfile: . 67", b"")
106111
with clear_and_catch_warnings(record=True, modules=[tck_module]) as w:
107112
tck = TckFile.load(BytesIO(new_tck_file))
@@ -110,6 +115,26 @@ def test_load_file_with_wrong_information(self):
110115
assert_true("Missing 'file'" in str(w[0].message))
111116
assert_array_equal(tck.header['file'], ". 56")
112117

118+
# Simulate a TCK file with `file` field pointing to another file.
119+
new_tck_file = tck_file.replace(b"file: . 67\n",
120+
b"file: dummy.mat 75\n")
121+
assert_raises(HeaderError, TckFile.load, BytesIO(new_tck_file))
122+
123+
# Simulate a TCK file which is missing a streamline delimiter.
124+
eos = TckFile.FIBER_DELIMITER.tostring()
125+
eof = TckFile.EOF_DELIMITER.tostring()
126+
new_tck_file = tck_file[:-(len(eos) + len(eof))] + tck_file[-len(eof):]
127+
128+
# Force TCK loading to use buffering.
129+
buffer_size = 1. / 1024**2 # 1 bytes
130+
hdr = TckFile._read_header(BytesIO(new_tck_file))
131+
tck_reader = TckFile._read(BytesIO(new_tck_file), hdr, buffer_size)
132+
assert_raises(DataError, list, tck_reader)
133+
134+
# Simulate a TCK file which is missing the end-of-file delimiter.
135+
new_tck_file = tck_file[:-len(eof)]
136+
assert_raises(DataError, TckFile.load, BytesIO(new_tck_file))
137+
113138
def test_write_empty_file(self):
114139
tractogram = Tractogram(affine_to_rasmm=np.eye(4))
115140

@@ -147,6 +172,15 @@ def test_write_simple_file(self):
147172
assert_equal(tck_file.read(),
148173
open(DATA['simple_tck_fname'], 'rb').read())
149174

175+
# TCK file containing not well formatted entries in its header.
176+
tck_file = BytesIO()
177+
tck = TckFile(tractogram)
178+
tck.header['new_entry'] = 'value\n' # \n not allowed
179+
assert_raises(HeaderError, tck.save, tck_file)
180+
181+
tck.header['new_entry'] = 'val:ue' # : not allowed
182+
assert_raises(HeaderError, tck.save, tck_file)
183+
150184
def test_load_write_file(self):
151185
for fname in [DATA['empty_tck_fname'],
152186
DATA['simple_tck_fname']]:

0 commit comments

Comments
 (0)