|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +from opentelemetry.sdk._configuration._exceptions import ( |
| 7 | + ConfigurationError, |
| 8 | + MissingDependencyError, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class TestMissingDependencyError(unittest.TestCase): |
| 13 | + def test_is_configuration_error_subclass(self): |
| 14 | + self.assertTrue(issubclass(MissingDependencyError, ConfigurationError)) |
| 15 | + |
| 16 | + def test_minimal_constructor(self): |
| 17 | + exc = MissingDependencyError(package="foo") |
| 18 | + self.assertEqual(exc.package, "foo") |
| 19 | + self.assertIsNone(exc.feature) |
| 20 | + self.assertEqual(exc.install_name, "foo") |
| 21 | + self.assertIsNone(exc.extras) |
| 22 | + self.assertIn("'foo'", str(exc)) |
| 23 | + self.assertIn("pip install foo", str(exc)) |
| 24 | + |
| 25 | + def test_with_feature(self): |
| 26 | + exc = MissingDependencyError(package="bar", feature="Baz exporter") |
| 27 | + self.assertEqual(exc.package, "bar") |
| 28 | + self.assertEqual(exc.feature, "Baz exporter") |
| 29 | + self.assertIn("Baz exporter requires 'bar'", str(exc)) |
| 30 | + self.assertIn("pip install bar", str(exc)) |
| 31 | + |
| 32 | + def test_with_custom_install_name(self): |
| 33 | + exc = MissingDependencyError( |
| 34 | + package="pyyaml", |
| 35 | + install_name="opentelemetry-sdk", |
| 36 | + extras="file-configuration", |
| 37 | + ) |
| 38 | + self.assertEqual(exc.install_name, "opentelemetry-sdk") |
| 39 | + self.assertEqual(exc.extras, "file-configuration") |
| 40 | + self.assertIn( |
| 41 | + 'pip install "opentelemetry-sdk[file-configuration]"', str(exc) |
| 42 | + ) |
| 43 | + |
| 44 | + def test_with_feature_and_extras(self): |
| 45 | + exc = MissingDependencyError( |
| 46 | + package="jsonschema", |
| 47 | + feature="File configuration", |
| 48 | + install_name="opentelemetry-sdk", |
| 49 | + extras="file-configuration", |
| 50 | + ) |
| 51 | + self.assertIn("File configuration requires 'jsonschema'", str(exc)) |
| 52 | + self.assertIn( |
| 53 | + 'pip install "opentelemetry-sdk[file-configuration]"', str(exc) |
| 54 | + ) |
| 55 | + |
| 56 | + def test_can_be_caught_as_configuration_error(self): |
| 57 | + with self.assertRaises(ConfigurationError): |
| 58 | + raise MissingDependencyError(package="test") |
| 59 | + |
| 60 | + def test_can_be_caught_as_exception(self): |
| 61 | + with self.assertRaises(Exception): |
| 62 | + raise MissingDependencyError(package="test") |
| 63 | + |
| 64 | + def test_can_be_caught_as_import_error(self): |
| 65 | + with self.assertRaises(ImportError): |
| 66 | + raise MissingDependencyError(package="test") |
| 67 | + |
| 68 | + def test_is_import_error_subclass(self): |
| 69 | + self.assertTrue(issubclass(MissingDependencyError, ImportError)) |
| 70 | + |
| 71 | + def test_issubclass_import_error(self): |
| 72 | + self.assertIsInstance( |
| 73 | + MissingDependencyError(package="test"), ImportError |
| 74 | + ) |
0 commit comments