|
43 | 43 |
|
44 | 44 | INTEGER_TYPES = np.sctypes['int'] + np.sctypes['uint']
|
45 | 45 |
|
| 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 | + |
46 | 109 |
|
47 | 110 | class _TestWrapStructBase(TestCase):
|
48 | 111 | ''' Class implements base tests for binary headers
|
@@ -161,40 +224,7 @@ def test_structarr(self):
|
161 | 224 | assert_raises(AttributeError, hdr.__setattr__, 'structarr', 0)
|
162 | 225 |
|
163 | 226 | 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) |
198 | 228 |
|
199 | 229 | def assert_no_log_err(self, hdr):
|
200 | 230 | """ Assert that no logging or errors result from this `hdr`
|
|
0 commit comments