Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions lib/iris/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,26 +539,37 @@ def assertRaisesRegexp(self, *args, **kwargs):
*args, **kwargs)

@contextlib.contextmanager
def assertGivesWarning(self, expected_regexp='', expect_warning=True):
# Check that a warning is raised matching a given expression, or that
# no warning matching the given expression is raised.
def _recordWarningMatches(self, expected_regexp=''):
# Record warnings raised matching a given expression.
matches = []
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
yield
yield matches
messages = [str(warning.message) for warning in w]
expr = re.compile(expected_regexp)
matches = [message for message in messages if expr.search(message)]
warning_raised = any(matches)
if expect_warning:
if not warning_raised:
msg = "Warning matching '{}' not raised."
msg = msg.format(expected_regexp)
self.assertEqual(expect_warning, warning_raised, msg)
else:
if warning_raised:
msg = "Unexpected warning(s) raised, matching '{}' : {!r}."
msg = msg.format(expected_regexp, matches)
self.assertEqual(expect_warning, warning_raised, msg)
matches.extend(message for message in messages
if expr.search(message))

@contextlib.contextmanager
def assertGivesWarning(self, expected_regexp=''):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename? assertWarningsMatching?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djkirkham suggests: assertGivesWarningRegexp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even assertGivesWarningMatchingRegexp

# Check that a warning is raised matching a given expression.
with self._recordWarningMatches(expected_regexp) as matches:
yield

msg = "Warning matching '{}' not raised."
msg = msg.format(expected_regexp)
self.assertTrue(matches, msg)


@contextlib.contextmanager
def assertDoesNotGiveWarning(self, expected_regexp=''):
# Check that no warning matching the given expression is raised.
with self._recordWarningMatches(expected_regexp) as matches:
yield

msg = "Unexpected warning(s) raised, matching '{}' : {!r}."
msg = msg.format(expected_regexp, matches)
self.assertFalse(matches, msg)

def _assertMaskedArray(self, assertion, a, b, strict, **kwargs):
# Define helper function to extract unmasked values as a 1d
Expand Down
8 changes: 4 additions & 4 deletions lib/iris/tests/unit/fileformats/netcdf/test_Saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def test_contains_default_fill_value_byte(self):
# value if no fill_value argument is supplied when the data is of a
# byte type.
cube = self._make_cube('>i1')
with self.assertGivesWarning(r'\(fill\|mask\)', expect_warning=False):
with self.assertDoesNotGiveWarning(r'\(fill\|mask\)'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename? assertNoWarningsMatching?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djkirkham suggests: assertDoesNotGiveWarningRegexp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertDoesNotGiveWarningMatchingRegexp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertNoWarningsMatchingRegexp

with self._netCDF_var(cube):
pass

Expand All @@ -449,15 +449,15 @@ def test_contains_masked_fill_value(self):
# a masked point.
fill_value = 1
cube = self._make_cube('>f4', masked_value=fill_value)
with self.assertGivesWarning(r'\(fill\|mask\)', expect_warning=False):
with self.assertDoesNotGiveWarning(r'\(fill\|mask\)'):
with self._netCDF_var(cube, fill_value=fill_value):
pass

def test_masked_byte_default_fill_value(self):
# Test that a warning is raised when saving masked byte data with no
# fill value supplied.
cube = self._make_cube('>i1', masked_value=1)
with self.assertGivesWarning(r'\(fill\|mask\)', expect_warning=False):
with self.assertDoesNotGiveWarning(r'\(fill\|mask\)'):
with self._netCDF_var(cube):
pass

Expand All @@ -466,7 +466,7 @@ def test_masked_byte_fill_value_passed(self):
# fill value supplied if the the data does not contain the fill_value.
fill_value = 100
cube = self._make_cube('>i1', masked_value=2)
with self.assertGivesWarning(r'\(fill\|mask\)', expect_warning=False):
with self.assertDoesNotGiveWarning(r'\(fill\|mask\)'):
with self._netCDF_var(cube, fill_value=fill_value):
pass

Expand Down
2 changes: 1 addition & 1 deletion lib/iris/tests/unit/fileformats/pp/test_PPField.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_mdi_masked_value_nowarning(self):
# Set underlying data value at masked point to BMDI value.
field.data.data[1] = field.bmdi
self.assertArrayAllClose(field.data.data[1], field.bmdi)
with self.assertGivesWarning(r'\(mask\|fill\)', expect_warning=False):
with self.assertDoesNotGiveWarning(r'\(mask\|fill\)'):
with self.temp_filename('.pp') as temp_filename:
with open(temp_filename, 'wb') as pp_file:
field.save(pp_file)
Expand Down