Skip to content

Commit c6193e9

Browse files
matthew-bretteffigies
authored andcommitted
RF+DOC: refactor log_chk into own function
* Refactor log_chk testing method into function; * Add more helpful docstring to log_chk; * Expand docstring for check_fix method of wrapstruct.
1 parent a042e27 commit c6193e9

File tree

2 files changed

+73
-35
lines changed

2 files changed

+73
-35
lines changed

nibabel/tests/test_wrapstruct.py

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,69 @@
4343

4444
INTEGER_TYPES = np.sctypes['int'] + np.sctypes['uint']
4545

46+
def log_chk(hdr, level):
47+
""" Utility method to check header checking / logging
48+
49+
Asserts that log entry appears during ``hdr.check_fix`` for logging level
50+
below `level`.
51+
52+
Parameters
53+
----------
54+
hdr : instance
55+
Instance of header class, with methods ``copy`` and check_fix``. The
56+
header has some minor error (defect) which can be detected with
57+
``check_fix``.
58+
level : int
59+
Level (severity) of defect present in `hdr`. When logging threshold is
60+
at or below `level`, a message appears in the default log (we test that
61+
happens).
62+
63+
Returns
64+
-------
65+
hdrc : instance
66+
Header, with defect corrected.
67+
message : str
68+
Message generated in log when defect was detected.
69+
raiser : tuple
70+
Tuple of error type, callable, arguments that will raise an exception
71+
when then defect is detected. Can be empty. Check with ``if raiser !=
72+
(): assert_raises(*raiser)``.
73+
"""
74+
str_io = StringIO()
75+
logger = logging.getLogger('test.logger')
76+
handler = logging.StreamHandler(str_io)
77+
logger.addHandler(handler)
78+
str_io.truncate(0)
79+
hdrc = hdr.copy()
80+
if level == 0: # Should never log or raise error
81+
logger.setLevel(0)
82+
hdrc.check_fix(logger=logger, error_level=0)
83+
assert_equal(str_io.getvalue(), '')
84+
logger.removeHandler(handler)
85+
return hdrc, '', ()
86+
# Non zero defect level, test above and below threshold.
87+
# Set error level above defect level to prevent exception when defect
88+
# detected.
89+
e_lev = level + 1
90+
# Logging level above threshold, no log.
91+
logger.setLevel(level + 1)
92+
hdrc.check_fix(logger=logger, error_level=e_lev)
93+
assert_equal(str_io.getvalue(), '')
94+
# Logging level below threshold, log appears, store logged message
95+
logger.setLevel(level - 1)
96+
hdrc = hdr.copy()
97+
hdrc.check_fix(logger=logger, error_level=e_lev)
98+
assert_true(str_io.getvalue() != '')
99+
message = str_io.getvalue().strip()
100+
logger.removeHandler(handler)
101+
# When error level == level, check_fix should raise an error
102+
hdrc2 = hdr.copy()
103+
raiser = (HeaderDataError,
104+
hdrc2.check_fix,
105+
logger,
106+
level)
107+
return hdrc, message, raiser
108+
46109

47110
class _TestWrapStructBase(TestCase):
48111
''' Class implements base tests for binary headers
@@ -161,40 +224,7 @@ def test_structarr(self):
161224
assert_raises(AttributeError, hdr.__setattr__, 'structarr', 0)
162225

163226
def log_chk(self, hdr, level):
164-
# utility method to check header checking / logging
165-
# If level == 0, this header should always be OK
166-
str_io = StringIO()
167-
logger = logging.getLogger('test.logger')
168-
handler = logging.StreamHandler(str_io)
169-
logger.addHandler(handler)
170-
str_io.truncate(0)
171-
hdrc = hdr.copy()
172-
if level == 0: # Should never log or raise error
173-
logger.setLevel(0)
174-
hdrc.check_fix(logger=logger, error_level=0)
175-
assert_equal(str_io.getvalue(), '')
176-
logger.removeHandler(handler)
177-
return hdrc, '', ()
178-
# Non zero level, test above and below threshold
179-
# Logging level above threshold, no log
180-
logger.setLevel(level + 1)
181-
e_lev = level + 1
182-
hdrc.check_fix(logger=logger, error_level=e_lev)
183-
assert_equal(str_io.getvalue(), '')
184-
# Logging level below threshold, log appears
185-
logger.setLevel(level + 1)
186-
logger.setLevel(level - 1)
187-
hdrc = hdr.copy()
188-
hdrc.check_fix(logger=logger, error_level=e_lev)
189-
assert_true(str_io.getvalue() != '')
190-
message = str_io.getvalue().strip()
191-
logger.removeHandler(handler)
192-
hdrc2 = hdr.copy()
193-
raiser = (HeaderDataError,
194-
hdrc2.check_fix,
195-
logger,
196-
level)
197-
return hdrc, message, raiser
227+
return log_chk(hdr, level)
198228

199229
def assert_no_log_err(self, hdr):
200230
""" Assert that no logging or errors result from this `hdr`

nibabel/wrapstruct.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,15 @@ def get(self, k, d=None):
346346
return (k in self.keys()) and self._structarr[k] or d
347347

348348
def check_fix(self, logger=None, error_level=None):
349-
''' Check structured data with checks '''
349+
''' Check structured data with checks
350+
351+
Parameters
352+
----------
353+
logger : None or logging.Logger
354+
error_level : None or int
355+
Level of error severity at which to raise error. Any error of
356+
severity >= `error_level` will cause an exception.
357+
'''
350358
if logger is None:
351359
logger = imageglobals.logger
352360
if error_level is None:

0 commit comments

Comments
 (0)