Skip to content

Commit 33e0289

Browse files
Copilot4n4nd
andcommitted
Refactor test code to reduce duplication
- Extract module removal logic into _remove_modules() helper method - Extract module checking logic into _is_module_loaded() helper method - Improve code maintainability and readability Co-authored-by: 4n4nd <22333506+4n4nd@users.noreply.github.com>
1 parent 74f8772 commit 33e0289

1 file changed

Lines changed: 39 additions & 58 deletions

File tree

tests/test_lazy_imports.py

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,93 +7,74 @@
77
class TestLazyImports(unittest.TestCase):
88
"""Test that PrometheusConnect can be imported without loading heavy dependencies."""
99

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.
1913
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:
2218
modules_to_remove = [
2319
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 + '.')
2521
]
2622
for module in modules_to_remove:
2723
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'])
2841

2942
# Import PrometheusConnect
3043
from prometheus_api_client import PrometheusConnect
3144

3245
# 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")
4152

4253
def test_prometheus_connect_instantiation_without_numpy(self):
4354
"""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'])
5957

6058
# Import and instantiate PrometheusConnect
6159
from prometheus_api_client import PrometheusConnect
6260
pc = PrometheusConnect(url='http://test.local:9090')
6361

6462
# 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")
6965
self.assertIsNotNone(pc, "PrometheusConnect should be instantiated successfully")
7066

7167
def test_metric_import_loads_pandas(self):
7268
"""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'])
8871

8972
# Import Metric
9073
from prometheus_api_client import Metric
9174

9275
# 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")
9778

9879

9980
if __name__ == '__main__':

0 commit comments

Comments
 (0)