|
| 1 | +"""Test lazy imports to ensure pandas/matplotlib are not loaded unnecessarily.""" |
| 2 | +import unittest |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | + |
| 6 | + |
| 7 | +class TestLazyImports(unittest.TestCase): |
| 8 | + """Test that PrometheusConnect can be imported without loading heavy dependencies.""" |
| 9 | + |
| 10 | + def _run_in_subprocess(self, code, fail_map): |
| 11 | + """Run code in a subprocess and check exit codes against fail_map. |
| 12 | + |
| 13 | + Args: |
| 14 | + code: Python code to execute in subprocess |
| 15 | + fail_map: Dictionary mapping exit codes to error messages |
| 16 | + |
| 17 | + Raises: |
| 18 | + AssertionError: If subprocess exits with a code in fail_map or any non-zero code |
| 19 | + """ |
| 20 | + result = subprocess.run( |
| 21 | + [sys.executable, '-c', code], |
| 22 | + capture_output=True, |
| 23 | + text=True |
| 24 | + ) |
| 25 | + |
| 26 | + if result.returncode in fail_map: |
| 27 | + self.fail(fail_map[result.returncode]) |
| 28 | + elif result.returncode != 0: |
| 29 | + # Include both stdout and stderr for better debugging |
| 30 | + output = [] |
| 31 | + if result.stdout: |
| 32 | + output.append(f"stdout: {result.stdout}") |
| 33 | + if result.stderr: |
| 34 | + output.append(f"stderr: {result.stderr}") |
| 35 | + output_str = "\n".join(output) if output else "no output" |
| 36 | + self.fail(f"Subprocess failed with code {result.returncode}: {output_str}") |
| 37 | + |
| 38 | + def test_prometheus_connect_import_without_pandas_matplotlib_numpy(self): |
| 39 | + """Test that importing PrometheusConnect doesn't load pandas, matplotlib, or numpy.""" |
| 40 | + # Run in a subprocess to avoid affecting other tests |
| 41 | + code = """ |
| 42 | +import sys |
| 43 | +from prometheus_api_client import PrometheusConnect |
| 44 | +
|
| 45 | +# Check that pandas, matplotlib, and numpy are not loaded |
| 46 | +pandas_loaded = any(m == 'pandas' or m.startswith('pandas.') for m in sys.modules.keys()) |
| 47 | +matplotlib_loaded = any(m == 'matplotlib' or m.startswith('matplotlib.') for m in sys.modules.keys()) |
| 48 | +numpy_loaded = any(m == 'numpy' or m.startswith('numpy.') for m in sys.modules.keys()) |
| 49 | +
|
| 50 | +if pandas_loaded: |
| 51 | + sys.exit(1) |
| 52 | +if matplotlib_loaded: |
| 53 | + sys.exit(2) |
| 54 | +if numpy_loaded: |
| 55 | + sys.exit(3) |
| 56 | +sys.exit(0) |
| 57 | +""" |
| 58 | + fail_map = { |
| 59 | + 1: "pandas should not be loaded when importing PrometheusConnect", |
| 60 | + 2: "matplotlib should not be loaded when importing PrometheusConnect", |
| 61 | + 3: "numpy should not be loaded when importing PrometheusConnect", |
| 62 | + } |
| 63 | + self._run_in_subprocess(code, fail_map) |
| 64 | + |
| 65 | + def test_prometheus_connect_instantiation_without_numpy(self): |
| 66 | + """Test that PrometheusConnect can be instantiated without loading numpy.""" |
| 67 | + # Run in a subprocess to avoid affecting other tests |
| 68 | + code = """ |
| 69 | +import sys |
| 70 | +from prometheus_api_client import PrometheusConnect |
| 71 | +
|
| 72 | +pc = PrometheusConnect(url='http://test.local:9090') |
| 73 | +
|
| 74 | +# Check that numpy is still not loaded after instantiation |
| 75 | +numpy_loaded = any(m == 'numpy' or m.startswith('numpy.') for m in sys.modules.keys()) |
| 76 | +
|
| 77 | +if numpy_loaded: |
| 78 | + sys.exit(1) |
| 79 | +if pc is None: |
| 80 | + sys.exit(2) |
| 81 | +sys.exit(0) |
| 82 | +""" |
| 83 | + fail_map = { |
| 84 | + 1: "numpy should not be loaded when instantiating PrometheusConnect", |
| 85 | + 2: "PrometheusConnect should be instantiated successfully", |
| 86 | + } |
| 87 | + self._run_in_subprocess(code, fail_map) |
| 88 | + |
| 89 | + def test_metric_import_loads_pandas(self): |
| 90 | + """Test that importing Metric does load pandas (expected behavior).""" |
| 91 | + # This test doesn't remove modules, so it won't cause reload issues |
| 92 | + from prometheus_api_client import Metric |
| 93 | + |
| 94 | + # Check that pandas is loaded (this is expected for Metric) |
| 95 | + pandas_loaded = any(m == 'pandas' or m.startswith('pandas.') for m in sys.modules.keys()) |
| 96 | + self.assertTrue(pandas_loaded, "pandas should be loaded when importing Metric") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + unittest.main() |
0 commit comments