Update tests/test_utils_snapshot.py
Run CI Tests / test (push) Successful in 32s

This commit is contained in:
2025-07-30 01:32:45 +02:00
parent 54234c13c7
commit d5c7a86be1
+72 -67
View File
@@ -1,75 +1,80 @@
import pytest
from unittest.mock import patch
from slic.utils.registry import instances
from slic.utils.snapshot import snapshot
from slic.core.adjustable import Adjustable
class FakeAdjustable(Adjustable):
class FakeAdjustable:
def __init__(self, ID, name=None, internal=False):
super().__init__(ID=ID, name=name or ID, internal=internal)
self._value = 0
def get_current_value(self):
return self._value
def is_moving(self):
return False
self.ID = ID
self.name = name or ID
self.internal = internal
def __repr__(self):
return f"<Adjustable {self.name}>"
def __str__(self):
return str(self.name)
def set_target_value(self, value, *args, **kwargs):
return value
@pytest.fixture(autouse=True)
def clean_registry(monkeypatch):
monkeypatch.setattr('slic.utils.registry.instances', lambda _: [])
def test_snapshot_excludes_internal_by_default():
visible = FakeAdjustable("vis_id", "visible", False)
hidden = FakeAdjustable("hid_id", "internal", True)
with patch('slic.utils.registry.instances', return_value=[visible, hidden]):
result = snapshot()
assert len(result) == 1
assert visible in result
assert hidden not in result
def test_snapshot_include_internal_flag():
visible = FakeAdjustable("vis_id", "visible", False)
hidden = FakeAdjustable("hid_id", "internal", True)
with patch('slic.utils.registry.instances', return_value=[visible, hidden]):
result = snapshot(include_internal=True)
assert len(result) == 2
assert visible in result
assert hidden in result
def test_snapshot_empty_result():
with patch('slic.utils.registry.instances', return_value=[]):
result = snapshot()
assert result == []
@pytest.mark.parametrize("sort_key,expected", [
(repr, ["Alpha", "Beta", "Charlie"]),
(str, ["Alpha", "Beta", "Charlie"]),
(lambda a: a.ID, ["id1", "id2", "id3"]),
(lambda a: a.name.lower(), ["alpha", "beta", "charlie"]),
])
def test_snapshot_sort_keys(sort_key, expected):
a = FakeAdjustable("id3", "Alpha")
b = FakeAdjustable("id1", "Beta")
c = FakeAdjustable("id2", "Charlie")
with patch('slic.utils.registry.instances', return_value=[c, a, b]):
result = snapshot(sort_key=sort_key)
if callable(sort_key) and 'ID' in sort_key.__code__.co_varnames:
assert [x.ID for x in result] == expected
elif callable(sort_key):
assert [x.name.lower() for x in result] == expected
else:
assert [x.name for x in result] == expected
def __str__(self):
return self.name
@pytest.mark.parametrize("test_input,expected,test_kwargs", [
# Default behavior - excludes internal adjustables
(
[FakeAdjustable("v1", "Visible", False),
FakeAdjustable("h1", "Hidden", True)],
["Visible"],
{}
),
# Include internal when requested
(
[FakeAdjustable("v1", "Visible", False),
FakeAdjustable("h1", "Hidden", True)],
["Visible", "Hidden"],
{"include_internal": True}
),
# Empty result case
(
[],
[],
{}
),
# Sorting by representation
(
[FakeAdjustable("3", "C"),
FakeAdjustable("1", "A"),
FakeAdjustable("2", "B")],
["A", "B", "C"],
{"sort_key": repr}
),
# Sorting by string conversion
(
[FakeAdjustable("3", "C"),
FakeAdjustable("1", "A"),
FakeAdjustable("2", "B")],
["A", "B", "C"],
{"sort_key": str}
),
# Sorting by ID
(
[FakeAdjustable("3", "C"),
FakeAdjustable("1", "A"),
FakeAdjustable("2", "B")],
["A", "B", "C"], # Names sorted by ID
{"sort_key": lambda a: a.ID}
),
# Case-insensitive name sorting
(
[FakeAdjustable("3", "Charlie"),
FakeAdjustable("1", "alpha"),
FakeAdjustable("2", "Beta")],
["alpha", "Beta", "Charlie"],
{"sort_key": lambda a: a.name.lower()}
)
])
def test_snapshot_all_cases(test_input, expected, test_kwargs):
with patch('slic.utils.registry.instances', return_value=test_input):
result = snapshot(**test_kwargs)
assert [x.name for x in result] == expected