|
| 1 | +# ruff: noqa: TC004, F401 |
| 2 | +import importlib |
| 3 | +import sys |
| 4 | +from importlib.util import cache_from_source |
| 5 | +from pathlib import Path |
| 6 | +from typing import Union |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +def purge_module(module_names: "list[str]", path: "Union[str, Path]") -> None: |
| 12 | + for name in module_names: |
| 13 | + if name in sys.modules: |
| 14 | + del sys.modules[name] |
| 15 | + Path(cache_from_source(str(path))).unlink(missing_ok=True) |
| 16 | + |
| 17 | + |
| 18 | +def test_deprecated_opentelemetry_imports() -> None: |
| 19 | + purge_module(["litestar.contrib.opentelemetry"], __file__) |
| 20 | + with pytest.warns( |
| 21 | + DeprecationWarning, |
| 22 | + match="importing OpenTelemetryConfig from 'litestar.contrib.opentelemetry' is deprecated", |
| 23 | + ): |
| 24 | + from litestar.contrib.opentelemetry import OpenTelemetryConfig |
| 25 | + |
| 26 | + purge_module(["litestar.contrib.opentelemetry"], __file__) |
| 27 | + with pytest.warns( |
| 28 | + DeprecationWarning, |
| 29 | + match="importing OpenTelemetryInstrumentationMiddleware from 'litestar.contrib.opentelemetry' is deprecated", |
| 30 | + ): |
| 31 | + from litestar.contrib.opentelemetry import OpenTelemetryInstrumentationMiddleware |
| 32 | + |
| 33 | + purge_module(["litestar.contrib.opentelemetry"], __file__) |
| 34 | + with pytest.warns( |
| 35 | + DeprecationWarning, |
| 36 | + match="importing OpenTelemetryPlugin from 'litestar.contrib.opentelemetry' is deprecated", |
| 37 | + ): |
| 38 | + from litestar.contrib.opentelemetry import OpenTelemetryPlugin |
| 39 | + |
| 40 | + |
| 41 | +def test_deprecated_opentelemetry_config_imports() -> None: |
| 42 | + purge_module(["litestar.contrib.opentelemetry.config"], __file__) |
| 43 | + with pytest.warns( |
| 44 | + DeprecationWarning, |
| 45 | + match="importing OpenTelemetryConfig from 'litestar.contrib.opentelemetry.config' is deprecated", |
| 46 | + ): |
| 47 | + from litestar.contrib.opentelemetry.config import OpenTelemetryConfig |
| 48 | + |
| 49 | + |
| 50 | +def test_deprecated_opentelemetry_middleware_imports() -> None: |
| 51 | + purge_module(["litestar.contrib.opentelemetry.middleware"], __file__) |
| 52 | + with pytest.warns( |
| 53 | + DeprecationWarning, |
| 54 | + match="importing OpenTelemetryInstrumentationMiddleware from 'litestar.contrib.opentelemetry.middleware' is deprecated", |
| 55 | + ): |
| 56 | + from litestar.contrib.opentelemetry.middleware import OpenTelemetryInstrumentationMiddleware |
| 57 | + |
| 58 | + |
| 59 | +def test_deprecated_opentelemetry_plugin_imports() -> None: |
| 60 | + purge_module(["litestar.contrib.opentelemetry.plugin"], __file__) |
| 61 | + with pytest.warns( |
| 62 | + DeprecationWarning, |
| 63 | + match="importing OpenTelemetryPlugin from 'litestar.contrib.opentelemetry.plugin' is deprecated", |
| 64 | + ): |
| 65 | + from litestar.contrib.opentelemetry.plugin import OpenTelemetryPlugin |
| 66 | + |
| 67 | + |
| 68 | +def test_contrib_imports_resolve_to_plugin_objects() -> None: |
| 69 | + """Sanity check: deprecated symbols are the same object as the plugin location.""" |
| 70 | + purge_module(["litestar.contrib.opentelemetry"], __file__) |
| 71 | + with pytest.warns(DeprecationWarning): |
| 72 | + from litestar.contrib.opentelemetry import ( |
| 73 | + OpenTelemetryConfig as ContribConfig, |
| 74 | + OpenTelemetryInstrumentationMiddleware as ContribMiddleware, |
| 75 | + OpenTelemetryPlugin as ContribPlugin, |
| 76 | + ) |
| 77 | + |
| 78 | + from litestar.plugins.opentelemetry import ( |
| 79 | + OpenTelemetryConfig, |
| 80 | + OpenTelemetryInstrumentationMiddleware, |
| 81 | + OpenTelemetryPlugin, |
| 82 | + ) |
| 83 | + |
| 84 | + assert ContribConfig is OpenTelemetryConfig |
| 85 | + assert ContribMiddleware is OpenTelemetryInstrumentationMiddleware |
| 86 | + assert ContribPlugin is OpenTelemetryPlugin |
0 commit comments