Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginnings of better logging tests. #68

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion lib/iris/experimental/ugrid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ def face_dimension(self, name):
"Not setting face_dimension (inappropriate for "
f"topology_dimension={self.topology_dimension} ."
)
logger.debug(message)
logger.debug(message, extra=dict(cls=None))
elif not name or not isinstance(name, str):
face_dimension = f"Mesh{self.topology_dimension}d_face"
else:
Expand Down
27 changes: 26 additions & 1 deletion lib/iris/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import functools
import gzip
import inspect
import json
import io
import json
import math
import os
import os.path
Expand Down Expand Up @@ -607,6 +607,31 @@ def assertWarnsRegexp(self, expected_regexp=""):
msg = msg.format(expected_regexp)
self.assertTrue(matches, msg)

@contextlib.contextmanager
def assertLogs(self, logger=None, level=None):
"""
An extended version of the usual :meth:`unittest.TestCase.assertLogs`,
which also exercises the logger's message formatting.

The inherited version of this method temporarily *replaces* the logger
in order to capture log records generated within the context.
However, in doing so it prevents any messages from being formatted
by the original logger.
This version first calls the original method, but then *also* exercises
the message formatters of all the logger's handlers, just to check that
there are no formatting errors.

"""
# Invoke the standard assertLogs behaviour.
assertlogging_context = super().assertLogs(logger, level)
with assertlogging_context as watcher:
# Run the caller context, as per original method.
yield watcher
# Check for any formatting errors by running all the formatters.
for record in watcher.records:
for handler in assertlogging_context.logger.handlers:
handler.format(record)

@contextlib.contextmanager
def assertNoWarningsRegexp(self, expected_regexp=""):
# Check that no warning matching the given expression is raised.
Expand Down
4 changes: 4 additions & 0 deletions lib/iris/tests/unit/experimental/ugrid/test_Mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,8 @@ def test_dimension_names(self):
default = ugrid.Mesh1DNames("Mesh1d_node", "Mesh1d_edge")
self.assertEqual(default, self.mesh.dimension_names())

ugrid.logger.setLevel("DEBUG")
self.mesh.dimension_names("foo", "bar", "baz")
with self.assertLogs(ugrid.logger, level="DEBUG") as log:
self.mesh.dimension_names("foo", "bar", "baz")
self.assertIn("Not setting face_dimension", log.output[0])
Expand All @@ -746,6 +748,8 @@ def test_edge_dimension_set(self):
self.assertEqual("foo", self.mesh.edge_dimension)

def test_face_dimension_set(self):
# ugrid.logger.setLevel('DEBUG')
# self.mesh.face_dimension = "foo"
with self.assertLogs(ugrid.logger, level="DEBUG") as log:
self.mesh.face_dimension = "foo"
self.assertIn("Not setting face_dimension", log.output[0])
Expand Down