Skip to content

Commit eab18a3

Browse files
committed
Merge pull request #1 from matthew-brett/refactor-csa-test
MRG: A little refactoring
2 parents 0aba201 + 9441591 commit eab18a3

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

‎nibabel/nicom/csareader.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
'IS': int, # integer string
1919
}
2020

21+
MAX_CSA_ITEMS = 199
22+
2123

2224
class CSAError(Exception):
2325
pass
@@ -116,7 +118,9 @@ def read(csa_str):
116118
# CSA1 specific length modifier
117119
if tag_no == 1:
118120
tag0_n_items = n_items
119-
assert n_items < 200
121+
if n_items > MAX_CSA_ITEMS:
122+
raise CSAReadError('Expected <= {0} tags, got {1}'.format(
123+
MAX_CSA_ITEMS, n_items))
120124
items = []
121125
for item_no in range(n_items):
122126
x0,x1,x2,x3 = up_str.unpack('4i')

‎nibabel/nicom/tests/test_csareader.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,19 @@ def test_csa_len0():
6868

6969

7070
def test_csa_nitem():
71-
# testing csa.read's ability to raise an error when n_items >200
72-
assert_raises(AssertionError, csa.read, CSA_STR_200n_items)
73-
assert csa.read(CSA_STR_valid)
71+
# testing csa.read's ability to raise an error when n_items >= 200
72+
assert_raises(csa.CSAReadError, csa.read, CSA_STR_200n_items)
73+
# OK when < 200
74+
csa_info = csa.read(CSA_STR_valid)
75+
assert_equal(len(csa_info['tags']), 1)
76+
# OK after changing module global
77+
n_items_thresh = csa.MAX_CSA_ITEMS
78+
try:
79+
csa.MAX_CSA_ITEMS = 1000
80+
csa_info = csa.read(CSA_STR_200n_items)
81+
assert_equal(len(csa_info['tags']), 1)
82+
finally:
83+
csa.MAX_CSA_ITEMS = n_items_thresh
7484

7585

7686
def test_csa_params():

0 commit comments

Comments
 (0)