From dae3a28b74776662a54c3e6f8a91ef7939e20f95 Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Wed, 15 Nov 2023 13:29:26 +0000 Subject: [PATCH] Test for an unexpected result from a wrapped metadata operation. --- lib/iris/common/_split_attribute_dicts.py | 6 +++ .../split_attribute_dicts/__init__.py | 6 +++ ...adjust_for_split_attribute_dictionaries.py | 37 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 lib/iris/tests/unit/common/metadata/split_attribute_dicts/__init__.py create mode 100644 lib/iris/tests/unit/common/metadata/split_attribute_dicts/test__adjust_for_split_attribute_dictionaries.py diff --git a/lib/iris/common/_split_attribute_dicts.py b/lib/iris/common/_split_attribute_dicts.py index 875ee3102f0..4aa19034ef9 100644 --- a/lib/iris/common/_split_attribute_dicts.py +++ b/lib/iris/common/_split_attribute_dicts.py @@ -117,6 +117,12 @@ def _inner_function(*args, **kwargs): _convert_pairedkeys_dict_to_splitattrs(right), ) result = result.__class__([left, right]) + else: + message = ( + f"Common-metadata operation {operation!r} returned a result which is " + f"neither a Mapping nor a pair of Mappings : {result!r}." + ) + raise ValueError(message) return result diff --git a/lib/iris/tests/unit/common/metadata/split_attribute_dicts/__init__.py b/lib/iris/tests/unit/common/metadata/split_attribute_dicts/__init__.py new file mode 100644 index 00000000000..959cdaf06a5 --- /dev/null +++ b/lib/iris/tests/unit/common/metadata/split_attribute_dicts/__init__.py @@ -0,0 +1,6 @@ +# Copyright Iris contributors +# +# This file is part of Iris and is released under the LGPL license. +# See COPYING and COPYING.LESSER in the root of the repository for full +# licensing details. +"""Unit tests for the :mod:`iris.common.metadata._split_attribute_dicts` package.""" diff --git a/lib/iris/tests/unit/common/metadata/split_attribute_dicts/test__adjust_for_split_attribute_dictionaries.py b/lib/iris/tests/unit/common/metadata/split_attribute_dicts/test__adjust_for_split_attribute_dictionaries.py new file mode 100644 index 00000000000..07b8b083ad1 --- /dev/null +++ b/lib/iris/tests/unit/common/metadata/split_attribute_dicts/test__adjust_for_split_attribute_dictionaries.py @@ -0,0 +1,37 @@ +# Copyright Iris contributors +# +# This file is part of Iris and is released under the LGPL license. +# See COPYING and COPYING.LESSER in the root of the repository for full +# licensing details. +""" +Unit tests for the :class:`iris.common._split_attribute_dicts.adjust_for_split_attribute_dictionaries`. + +""" +import pytest + +from iris.common._split_attribute_dicts import ( + adjust_for_split_attribute_dictionaries, +) + + +class Test: + """ + Test the decorator function + behaviour of decorated functions. + + In this module, only exercise the return-value error check, + since **all other behaviour is tested by integration**. + See : :mod:`iris.tests.unit.common.metadata.test_CubeMetadata`. + """ + + def test_bad_return_value__fails(self): + @adjust_for_split_attribute_dictionaries + def sample_metadata_operation(*dictionary_args): + return [1, 2, 3] + + message = ( + "Common-metadata operation .*sample_metadata_operation .* " + "returned a result which is neither a Mapping nor a pair of Mappings " + r": \[1, 2, 3\]." + ) + with pytest.raises(ValueError, match=message): + sample_metadata_operation({})