|
7 | 7 | class TestLazyImports(unittest.TestCase): |
8 | 8 | """Test that PrometheusConnect can be imported without loading heavy dependencies.""" |
9 | 9 |
|
10 | | - def test_prometheus_connect_import_without_pandas_matplotlib_numpy(self): |
11 | | - """Test that importing PrometheusConnect doesn't load pandas, matplotlib, or numpy.""" |
12 | | - # Remove any previously loaded prometheus_api_client modules |
13 | | - modules_to_remove = [ |
14 | | - key for key in sys.modules.keys() |
15 | | - if key.startswith('prometheus_api_client') |
16 | | - ] |
17 | | - for module in modules_to_remove: |
18 | | - del sys.modules[module] |
| 10 | + @staticmethod |
| 11 | + def _remove_modules(module_names): |
| 12 | + """Remove specified modules and their submodules from sys.modules. |
19 | 13 | |
20 | | - # Also remove numpy, pandas, matplotlib if they were loaded |
21 | | - for heavy_module in ['numpy', 'pandas', 'matplotlib']: |
| 14 | + Args: |
| 15 | + module_names: List of module names to remove |
| 16 | + """ |
| 17 | + for module_name in module_names: |
22 | 18 | modules_to_remove = [ |
23 | 19 | key for key in sys.modules.keys() |
24 | | - if key == heavy_module or key.startswith(heavy_module + '.') |
| 20 | + if key == module_name or key.startswith(module_name + '.') |
25 | 21 | ] |
26 | 22 | for module in modules_to_remove: |
27 | 23 | del sys.modules[module] |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def _is_module_loaded(module_name): |
| 27 | + """Check if a module is loaded in sys.modules. |
| 28 | + |
| 29 | + Args: |
| 30 | + module_name: Name of the module to check |
| 31 | + |
| 32 | + Returns: |
| 33 | + bool: True if module is loaded, False otherwise |
| 34 | + """ |
| 35 | + return any(m == module_name or m.startswith(module_name + '.') for m in sys.modules.keys()) |
| 36 | + |
| 37 | + def test_prometheus_connect_import_without_pandas_matplotlib_numpy(self): |
| 38 | + """Test that importing PrometheusConnect doesn't load pandas, matplotlib, or numpy.""" |
| 39 | + # Remove any previously loaded modules |
| 40 | + self._remove_modules(['prometheus_api_client', 'numpy', 'pandas', 'matplotlib']) |
28 | 41 |
|
29 | 42 | # Import PrometheusConnect |
30 | 43 | from prometheus_api_client import PrometheusConnect |
31 | 44 |
|
32 | 45 | # Check that pandas, matplotlib, and numpy are not loaded |
33 | | - loaded_modules = sys.modules.keys() |
34 | | - pandas_loaded = any(m == 'pandas' or m.startswith('pandas.') for m in loaded_modules) |
35 | | - matplotlib_loaded = any(m == 'matplotlib' or m.startswith('matplotlib.') for m in loaded_modules) |
36 | | - numpy_loaded = any(m == 'numpy' or m.startswith('numpy.') for m in loaded_modules) |
37 | | - |
38 | | - self.assertFalse(pandas_loaded, "pandas should not be loaded when importing PrometheusConnect") |
39 | | - self.assertFalse(matplotlib_loaded, "matplotlib should not be loaded when importing PrometheusConnect") |
40 | | - self.assertFalse(numpy_loaded, "numpy should not be loaded when importing PrometheusConnect") |
| 46 | + self.assertFalse(self._is_module_loaded('pandas'), |
| 47 | + "pandas should not be loaded when importing PrometheusConnect") |
| 48 | + self.assertFalse(self._is_module_loaded('matplotlib'), |
| 49 | + "matplotlib should not be loaded when importing PrometheusConnect") |
| 50 | + self.assertFalse(self._is_module_loaded('numpy'), |
| 51 | + "numpy should not be loaded when importing PrometheusConnect") |
41 | 52 |
|
42 | 53 | def test_prometheus_connect_instantiation_without_numpy(self): |
43 | 54 | """Test that PrometheusConnect can be instantiated without loading numpy.""" |
44 | | - # Remove any previously loaded prometheus_api_client modules |
45 | | - modules_to_remove = [ |
46 | | - key for key in sys.modules.keys() |
47 | | - if key.startswith('prometheus_api_client') |
48 | | - ] |
49 | | - for module in modules_to_remove: |
50 | | - del sys.modules[module] |
51 | | - |
52 | | - # Also remove numpy if it was loaded |
53 | | - modules_to_remove = [ |
54 | | - key for key in sys.modules.keys() |
55 | | - if key == 'numpy' or key.startswith('numpy.') |
56 | | - ] |
57 | | - for module in modules_to_remove: |
58 | | - del sys.modules[module] |
| 55 | + # Remove any previously loaded modules |
| 56 | + self._remove_modules(['prometheus_api_client', 'numpy']) |
59 | 57 |
|
60 | 58 | # Import and instantiate PrometheusConnect |
61 | 59 | from prometheus_api_client import PrometheusConnect |
62 | 60 | pc = PrometheusConnect(url='http://test.local:9090') |
63 | 61 |
|
64 | 62 | # Check that numpy is still not loaded after instantiation |
65 | | - loaded_modules = sys.modules.keys() |
66 | | - numpy_loaded = any(m == 'numpy' or m.startswith('numpy.') for m in loaded_modules) |
67 | | - |
68 | | - self.assertFalse(numpy_loaded, "numpy should not be loaded when instantiating PrometheusConnect") |
| 63 | + self.assertFalse(self._is_module_loaded('numpy'), |
| 64 | + "numpy should not be loaded when instantiating PrometheusConnect") |
69 | 65 | self.assertIsNotNone(pc, "PrometheusConnect should be instantiated successfully") |
70 | 66 |
|
71 | 67 | def test_metric_import_loads_pandas(self): |
72 | 68 | """Test that importing Metric does load pandas (expected behavior).""" |
73 | | - # Remove any previously loaded prometheus_api_client modules |
74 | | - modules_to_remove = [ |
75 | | - key for key in sys.modules.keys() |
76 | | - if key.startswith('prometheus_api_client') |
77 | | - ] |
78 | | - for module in modules_to_remove: |
79 | | - del sys.modules[module] |
80 | | - |
81 | | - # Also remove pandas if it was loaded |
82 | | - modules_to_remove = [ |
83 | | - key for key in sys.modules.keys() |
84 | | - if key == 'pandas' or key.startswith('pandas.') |
85 | | - ] |
86 | | - for module in modules_to_remove: |
87 | | - del sys.modules[module] |
| 69 | + # Remove any previously loaded modules |
| 70 | + self._remove_modules(['prometheus_api_client', 'pandas']) |
88 | 71 |
|
89 | 72 | # Import Metric |
90 | 73 | from prometheus_api_client import Metric |
91 | 74 |
|
92 | 75 | # Check that pandas is loaded (this is expected for Metric) |
93 | | - loaded_modules = sys.modules.keys() |
94 | | - pandas_loaded = any(m == 'pandas' or m.startswith('pandas.') for m in loaded_modules) |
95 | | - |
96 | | - self.assertTrue(pandas_loaded, "pandas should be loaded when importing Metric") |
| 76 | + self.assertTrue(self._is_module_loaded('pandas'), |
| 77 | + "pandas should be loaded when importing Metric") |
97 | 78 |
|
98 | 79 |
|
99 | 80 | if __name__ == '__main__': |
|
0 commit comments