Skip to content

Commit c93132b

Browse files
authored
Merge pull request #2863 from djkirkham/assertDoesNotGiveWarning
Refactor assertGivesWarning into separate methods
2 parents 6487277 + 4f8b2d8 commit c93132b

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

lib/iris/tests/__init__.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -531,26 +531,37 @@ def assertRaisesRegexp(self, *args, **kwargs):
531531
*args, **kwargs)
532532

533533
@contextlib.contextmanager
534-
def assertGivesWarning(self, expected_regexp='', expect_warning=True):
535-
# Check that a warning is raised matching a given expression, or that
536-
# no warning matching the given expression is raised.
534+
def _recordWarningMatches(self, expected_regexp=''):
535+
# Record warnings raised matching a given expression.
536+
matches = []
537537
with warnings.catch_warnings(record=True) as w:
538538
warnings.simplefilter('always')
539-
yield
539+
yield matches
540540
messages = [str(warning.message) for warning in w]
541541
expr = re.compile(expected_regexp)
542-
matches = [message for message in messages if expr.search(message)]
543-
warning_raised = any(matches)
544-
if expect_warning:
545-
if not warning_raised:
546-
msg = "Warning matching '{}' not raised."
547-
msg = msg.format(expected_regexp)
548-
self.assertEqual(expect_warning, warning_raised, msg)
549-
else:
550-
if warning_raised:
551-
msg = "Unexpected warning(s) raised, matching '{}' : {!r}."
552-
msg = msg.format(expected_regexp, matches)
553-
self.assertEqual(expect_warning, warning_raised, msg)
542+
matches.extend(message for message in messages
543+
if expr.search(message))
544+
545+
@contextlib.contextmanager
546+
def assertWarnsRegexp(self, expected_regexp=''):
547+
# Check that a warning is raised matching a given expression.
548+
with self._recordWarningMatches(expected_regexp) as matches:
549+
yield
550+
551+
msg = "Warning matching '{}' not raised."
552+
msg = msg.format(expected_regexp)
553+
self.assertTrue(matches, msg)
554+
555+
556+
@contextlib.contextmanager
557+
def assertNoWarningsRegexp(self, expected_regexp=''):
558+
# Check that no warning matching the given expression is raised.
559+
with self._recordWarningMatches(expected_regexp) as matches:
560+
yield
561+
562+
msg = "Unexpected warning(s) raised, matching '{}' : {!r}."
563+
msg = msg.format(expected_regexp, matches)
564+
self.assertFalse(matches, msg)
554565

555566
def _assertMaskedArray(self, assertion, a, b, strict, **kwargs):
556567
# Define helper function to extract unmasked values as a 1d

lib/iris/tests/unit/fileformats/netcdf/test_Saver.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def test_contains_fill_value_passed(self):
410410
# Test that a warning is raised if the data contains the fill value.
411411
cube = self._make_cube('>f4')
412412
fill_value = 1
413-
with self.assertGivesWarning(
413+
with self.assertWarnsRegexp(
414414
'contains unmasked data points equal to the fill-value'):
415415
with self._netCDF_var(cube, fill_value=fill_value):
416416
pass
@@ -420,7 +420,7 @@ def test_contains_fill_value_byte(self):
420420
# when it is of a byte type.
421421
cube = self._make_cube('>i1')
422422
fill_value = 1
423-
with self.assertGivesWarning(
423+
with self.assertWarnsRegexp(
424424
'contains unmasked data points equal to the fill-value'):
425425
with self._netCDF_var(cube, fill_value=fill_value):
426426
pass
@@ -430,7 +430,7 @@ def test_contains_default_fill_value(self):
430430
# value if no fill_value argument is supplied.
431431
cube = self._make_cube('>f4')
432432
cube.data[0, 0] = nc.default_fillvals['f4']
433-
with self.assertGivesWarning(
433+
with self.assertWarnsRegexp(
434434
'contains unmasked data points equal to the fill-value'):
435435
with self._netCDF_var(cube):
436436
pass
@@ -440,7 +440,7 @@ def test_contains_default_fill_value_byte(self):
440440
# value if no fill_value argument is supplied when the data is of a
441441
# byte type.
442442
cube = self._make_cube('>i1')
443-
with self.assertGivesWarning(r'\(fill\|mask\)', expect_warning=False):
443+
with self.assertNoWarningsRegexp(r'\(fill\|mask\)'):
444444
with self._netCDF_var(cube):
445445
pass
446446

@@ -449,15 +449,15 @@ def test_contains_masked_fill_value(self):
449449
# a masked point.
450450
fill_value = 1
451451
cube = self._make_cube('>f4', masked_value=fill_value)
452-
with self.assertGivesWarning(r'\(fill\|mask\)', expect_warning=False):
452+
with self.assertNoWarningsRegexp(r'\(fill\|mask\)'):
453453
with self._netCDF_var(cube, fill_value=fill_value):
454454
pass
455455

456456
def test_masked_byte_default_fill_value(self):
457457
# Test that a warning is raised when saving masked byte data with no
458458
# fill value supplied.
459459
cube = self._make_cube('>i1', masked_value=1)
460-
with self.assertGivesWarning(r'\(fill\|mask\)', expect_warning=False):
460+
with self.assertNoWarningsRegexp(r'\(fill\|mask\)'):
461461
with self._netCDF_var(cube):
462462
pass
463463

@@ -466,7 +466,7 @@ def test_masked_byte_fill_value_passed(self):
466466
# fill value supplied if the the data does not contain the fill_value.
467467
fill_value = 100
468468
cube = self._make_cube('>i1', masked_value=2)
469-
with self.assertGivesWarning(r'\(fill\|mask\)', expect_warning=False):
469+
with self.assertNoWarningsRegexp(r'\(fill\|mask\)'):
470470
with self._netCDF_var(cube, fill_value=fill_value):
471471
pass
472472

lib/iris/tests/unit/fileformats/pp/test_PPField.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def field_checksum(data):
108108
data_64 = np.linspace(0, 1, num=10, endpoint=False).reshape(2, 5)
109109
checksum_32 = field_checksum(data_64.astype('>f4'))
110110
msg = 'Downcasting array precision from float64 to float32 for save.'
111-
with self.assertGivesWarning(msg):
111+
with self.assertWarnsRegexp(msg):
112112
checksum_64 = field_checksum(data_64.astype('>f8'))
113113
self.assertEqual(checksum_32, checksum_64)
114114

@@ -119,7 +119,7 @@ def test_masked_mdi_value_warning(self):
119119
# Make float32 data, as float64 default produces an extra warning.
120120
field.data = np.ma.masked_array([1., field.bmdi, 3.], dtype=np.float32)
121121
msg = 'PPField data contains unmasked points'
122-
with self.assertGivesWarning(msg):
122+
with self.assertWarnsRegexp(msg):
123123
with self.temp_filename('.pp') as temp_filename:
124124
with open(temp_filename, 'wb') as pp_file:
125125
field.save(pp_file)
@@ -131,7 +131,7 @@ def test_unmasked_mdi_value_warning(self):
131131
# Make float32 data, as float64 default produces an extra warning.
132132
field.data = np.array([1., field.bmdi, 3.], dtype=np.float32)
133133
msg = 'PPField data contains unmasked points'
134-
with self.assertGivesWarning(msg):
134+
with self.assertWarnsRegexp(msg):
135135
with self.temp_filename('.pp') as temp_filename:
136136
with open(temp_filename, 'wb') as pp_file:
137137
field.save(pp_file)
@@ -146,7 +146,7 @@ def test_mdi_masked_value_nowarning(self):
146146
# Set underlying data value at masked point to BMDI value.
147147
field.data.data[1] = field.bmdi
148148
self.assertArrayAllClose(field.data.data[1], field.bmdi)
149-
with self.assertGivesWarning(r'\(mask\|fill\)', expect_warning=False):
149+
with self.assertNoWarningsRegexp(r'\(mask\|fill\)'):
150150
with self.temp_filename('.pp') as temp_filename:
151151
with open(temp_filename, 'wb') as pp_file:
152152
field.save(pp_file)

lib/iris/tests/unit/test_Future.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_valid_clip_latitudes(self):
4040
future = Future()
4141
new_value = not future.clip_latitudes
4242
msg = "'Future' property 'clip_latitudes' is deprecated"
43-
with self.assertGivesWarning(msg):
43+
with self.assertWarnsRegexp(msg):
4444
future.clip_latitudes = new_value
4545
self.assertEqual(future.clip_latitudes, new_value)
4646

0 commit comments

Comments
 (0)