|
| 1 | +# The MIT License (MIT) |
| 2 | +# Copyright (c) 2025 by the xcube development team and contributors |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | +# copy of this software and associated documentation files (the "Software"), |
| 6 | +# to deal in the Software without restriction, including without limitation |
| 7 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | +# and/or sell copies of the Software, and to permit persons to whom the |
| 9 | +# Software is furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in |
| 12 | +# all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | +# DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +import unittest |
| 23 | + |
| 24 | +from xcube_resampling.gridmapping import assertions |
| 25 | + |
| 26 | + |
| 27 | +class Testassertions(unittest.TestCase): |
| 28 | + # --- assert_given --- |
| 29 | + def test_assert_given_ok(self): |
| 30 | + assertions.assert_given("hello") |
| 31 | + |
| 32 | + def test_assert_given_fail(self): |
| 33 | + with self.assertRaises(ValueError) as cm: |
| 34 | + assertions.assert_given("", name="arg") |
| 35 | + self.assertIn("arg must be given", str(cm.exception)) |
| 36 | + |
| 37 | + # --- assert_instance --- |
| 38 | + def test_assert_instance_ok(self): |
| 39 | + assertions.assert_instance(5, int) |
| 40 | + |
| 41 | + def test_assert_instance_fail(self): |
| 42 | + with self.assertRaises(TypeError) as cm: |
| 43 | + assertions.assert_instance("s", int, name="val") |
| 44 | + self.assertIn("val must be an instance of", str(cm.exception)) |
| 45 | + |
| 46 | + def test_assert_instance_tuple_dtype(self): |
| 47 | + assertions.assert_instance(5, (int, float)) # should pass |
| 48 | + |
| 49 | + # --- assert_in --- |
| 50 | + def test_assert_in_ok(self): |
| 51 | + assertions.assert_in(1, [1, 2, 3]) |
| 52 | + |
| 53 | + def test_assert_in_fail(self): |
| 54 | + with self.assertRaises(ValueError) as cm: |
| 55 | + assertions.assert_in("z", ["a", "b"], name="char") |
| 56 | + self.assertIn("char must be one of", str(cm.exception)) |
| 57 | + |
| 58 | + # --- assert_true --- |
| 59 | + def test_assert_true_ok(self): |
| 60 | + assertions.assert_true(True, "must be true") |
| 61 | + |
| 62 | + def test_assert_true_fail(self): |
| 63 | + with self.assertRaises(ValueError) as cm: |
| 64 | + assertions.assert_true(False, "bad value") |
| 65 | + self.assertEqual(str(cm.exception), "bad value") |
0 commit comments