|
15 | 15 | import unittest
|
16 | 16 | from http import HTTPStatus
|
17 | 17 |
|
| 18 | +from wrapt import ObjectProxy, wrap_function_wrapper |
| 19 | + |
18 | 20 | from opentelemetry.context import (
|
19 | 21 | _SUPPRESS_HTTP_INSTRUMENTATION_KEY,
|
20 | 22 | _SUPPRESS_INSTRUMENTATION_KEY,
|
|
29 | 31 | is_instrumentation_enabled,
|
30 | 32 | suppress_http_instrumentation,
|
31 | 33 | suppress_instrumentation,
|
| 34 | + unwrap, |
32 | 35 | )
|
33 | 36 | from opentelemetry.trace import StatusCode
|
34 | 37 |
|
35 | 38 |
|
| 39 | +class WrappedClass: |
| 40 | + def method(self): |
| 41 | + pass |
| 42 | + |
| 43 | + def wrapper_method(self): |
| 44 | + pass |
| 45 | + |
| 46 | + |
36 | 47 | class TestUtils(unittest.TestCase):
|
37 | 48 | # See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status
|
38 | 49 | def test_http_status_to_status_code(self):
|
@@ -240,3 +251,75 @@ def test_suppress_http_instrumentation_key(self):
|
240 | 251 | self.assertTrue(get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY))
|
241 | 252 |
|
242 | 253 | self.assertIsNone(get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY))
|
| 254 | + |
| 255 | + |
| 256 | +class UnwrapTestCase(unittest.TestCase): |
| 257 | + @staticmethod |
| 258 | + def _wrap_method(): |
| 259 | + return wrap_function_wrapper( |
| 260 | + WrappedClass, "method", WrappedClass.wrapper_method |
| 261 | + ) |
| 262 | + |
| 263 | + def test_can_unwrap_object_attribute(self): |
| 264 | + self._wrap_method() |
| 265 | + instance = WrappedClass() |
| 266 | + self.assertTrue(isinstance(instance.method, ObjectProxy)) |
| 267 | + |
| 268 | + unwrap(WrappedClass, "method") |
| 269 | + self.assertFalse(isinstance(instance.method, ObjectProxy)) |
| 270 | + |
| 271 | + def test_can_unwrap_object_attribute_as_string(self): |
| 272 | + self._wrap_method() |
| 273 | + instance = WrappedClass() |
| 274 | + self.assertTrue(isinstance(instance.method, ObjectProxy)) |
| 275 | + |
| 276 | + unwrap("tests.test_utils.WrappedClass", "method") |
| 277 | + self.assertFalse(isinstance(instance.method, ObjectProxy)) |
| 278 | + |
| 279 | + def test_raises_import_error_if_path_not_well_formed(self): |
| 280 | + self._wrap_method() |
| 281 | + instance = WrappedClass() |
| 282 | + self.assertTrue(isinstance(instance.method, ObjectProxy)) |
| 283 | + |
| 284 | + with self.assertRaisesRegex( |
| 285 | + ImportError, "Cannot parse '' as dotted import path" |
| 286 | + ): |
| 287 | + unwrap("", "method") |
| 288 | + |
| 289 | + unwrap(WrappedClass, "method") |
| 290 | + self.assertFalse(isinstance(instance.method, ObjectProxy)) |
| 291 | + |
| 292 | + def test_raises_import_error_if_cannot_find_module(self): |
| 293 | + self._wrap_method() |
| 294 | + instance = WrappedClass() |
| 295 | + self.assertTrue(isinstance(instance.method, ObjectProxy)) |
| 296 | + |
| 297 | + with self.assertRaisesRegex(ImportError, "No module named 'does'"): |
| 298 | + unwrap("does.not.exist.WrappedClass", "method") |
| 299 | + |
| 300 | + unwrap(WrappedClass, "method") |
| 301 | + self.assertFalse(isinstance(instance.method, ObjectProxy)) |
| 302 | + |
| 303 | + def test_raises_import_error_if_cannot_find_object(self): |
| 304 | + self._wrap_method() |
| 305 | + instance = WrappedClass() |
| 306 | + self.assertTrue(isinstance(instance.method, ObjectProxy)) |
| 307 | + |
| 308 | + with self.assertRaisesRegex( |
| 309 | + ImportError, "Cannot import 'NotWrappedClass' from" |
| 310 | + ): |
| 311 | + unwrap("tests.test_utils.NotWrappedClass", "method") |
| 312 | + |
| 313 | + unwrap(WrappedClass, "method") |
| 314 | + self.assertFalse(isinstance(instance.method, ObjectProxy)) |
| 315 | + |
| 316 | + # pylint: disable=no-self-use |
| 317 | + def test_does_nothing_if_cannot_find_attribute(self): |
| 318 | + instance = WrappedClass() |
| 319 | + unwrap(instance, "method_not_found") |
| 320 | + |
| 321 | + def test_does_nothing_if_attribute_is_not_from_wrapt(self): |
| 322 | + instance = WrappedClass() |
| 323 | + self.assertFalse(isinstance(instance.method, ObjectProxy)) |
| 324 | + unwrap(WrappedClass, "method") |
| 325 | + self.assertFalse(isinstance(instance.method, ObjectProxy)) |
0 commit comments