Update tests/test_utils_ipy.py
Run CI Tests / test (push) Successful in 54s

This commit is contained in:
2025-08-03 22:52:06 +02:00
parent d41c7c0fe2
commit c968940144
+40 -49
View File
@@ -4,95 +4,86 @@ from slic.utils.ipy import devices
from slic.core.device.device import Device
import types
# Fixture to simulate an IPython env
@pytest.fixture(scope="module")
def fake_ipython():
@pytest.fixture(scope="module", autouse=True)
def simulate_ipython(monkeypatch):
class FakeIPython:
def __init__(self):
self.user_ns = {}
return FakeIPython()
@pytest.fixture(autouse=True)
def simulate_ipython(fake_ipython, monkeypatch):
monkeypatch.setattr("slic.utils.ipy.get_ipython", lambda: fake_ipython)
return fake_ipython
fake_ipy = FakeIPython()
monkeypatch.setattr("slic.utils.ipy.get_ipython", lambda: fake_ipy)
return fake_ipy
def test_devices_repr_fallback_and_ignore(simulate_ipython):
ipy = simulate_ipython
assert ipy is not None, "This test must be run in IPython (Jupyter or ipython shell)"
assert ipy is not None
# Clean up previous test variables
# Clean namespace
for k in list(ipy.user_ns):
if k.startswith("test_") or k.startswith("_test_"):
if k.startswith(("test_", "_test_")):
del ipy.user_ns[k]
# Valid BaseDevice subclasses
# Test device classes
class WithDescription(Device):
def __init__(self, ID):
super().__init__(ID)
self.ID = ID
self.description = "desc ok"
self.name = None # Explicit None
class WithDoc(Device):
"""doc ok"""
def __init__(self, ID):
super().__init__(ID)
self.ID = ID
self.description = None
self.name = None
class WithName(Device):
def __init__(self, ID):
super().__init__(ID)
self.ID = ID
self.name = "name ok"
# remove description to force fallback
if hasattr(self, 'description'):
del self.description
self.description = None
class WithOnlyID(Device):
def __init__(self, ID):
super().__init__(ID)
self.ID = ID
# remove name and description to force fallback to ID
if hasattr(self, 'description'):
del self.description
if hasattr(self, 'name'):
del self.name
self.name = None
self.description = None
# Invalid or ignored objects
# Setup test environment
ipy.user_ns.update({
# Valid devices
"test_desc": WithDescription("D1"),
"test_doc": WithDoc("D2"),
"test_name": WithName("D3"),
"test_id": WithOnlyID("D4"),
# Invalid objects
"test_str": "string",
"_hidden": WithDescription("HIDDEN")
})
ipy.user_ns["test_str"] = "i am a string"
ipy.user_ns["test_int"] = 42
ipy.user_ns["test_list"] = [1, 2, 3]
ipy.user_ns["test_func"] = lambda x: x
ipy.user_ns["test_module"] = types
ipy.user_ns["test_fake_device"] = type("FakeDevice", (), {"ID": "123"})()
ipy.user_ns["_test_hidden_device"] = WithDescription("HIDDEN")
# Inject valid devices into IPython
ipy.user_ns["test_desc"] = WithDescription("D1") # has description
ipy.user_ns["test_doc"] = WithDoc("D2") # has __doc__
ipy.user_ns["test_name"] = WithName("D3") # has name
ipy.user_ns["test_id"] = WithOnlyID("D4") # fallback to ID
# Execute devices()
out = repr(devices)
# Assert that valid devices appear and correct fallback values are present
# Core assertions (unchanged)
assert "test_desc" in out
assert "desc ok" in out
assert "test_doc" in out
assert "doc ok" in out
assert "test_name" in out
assert "name ok" in out
assert "test_id" in out
assert "D4" in out
assert "test_str" not in out
assert "_hidden" not in out
# Assert that all ignored values are not shown
for bad in [
"test_str", "test_int", "test_list", "test_func",
"test_module", "test_fake_device", "_test_hidden_device"
]:
assert bad not in out
# New None checks (what you asked for)
assert getattr(ipy.user_ns["test_desc"], "name") is None
assert getattr(ipy.user_ns["test_doc"], "name") is None
assert getattr(ipy.user_ns["test_doc"], "description") is None
assert getattr(ipy.user_ns["test_id"], "name") is None
assert getattr(ipy.user_ns["test_id"], "description") is None