92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
import pytest
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from slic.utils.snapshot import *
|
|
from slic.utils.registry import instances
|
|
from unittest.mock import patch
|
|
|
|
|
|
# --- Fake Adjustable class for testing ---
|
|
class FakeAdjustable(Adjustable):
|
|
def __init__(self, ID, name=None, internal=False):
|
|
super().__init__(ID=ID, name=name or ID, internal=internal)
|
|
self._value = 0 # dummy fixed value for all instances
|
|
|
|
def get_current_value(self):
|
|
return self._value
|
|
|
|
def is_moving(self):
|
|
return False
|
|
|
|
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
|
|
|
|
|
|
def test_snapshot_excludes_internal_by_default():
|
|
|
|
visible = FakeAdjustable("visible", internal=False)
|
|
hidden = FakeAdjustable("internal", internal=True)
|
|
|
|
with patch("slic.utils.typecast.instances", return_value=[visible, hidden]):
|
|
result = snapshot()
|
|
assert visible in result
|
|
assert hidden not in result
|
|
|
|
|
|
def test_snapshot_include_internal_flag():
|
|
|
|
visible = FakeAdjustable("visible", internal=False)
|
|
hidden = FakeAdjustable("internal", internal=True)
|
|
|
|
with patch("slic.utils.typecast.instances", return_value=[visible, hidden]):
|
|
result = snapshot(include_internal=True)
|
|
assert visible in result
|
|
assert hidden in result
|
|
assert len(result) == 2
|
|
|
|
|
|
def test_snapshot_empty_result():
|
|
|
|
with patch("slic.utils.typecast.instances", return_value=[]):
|
|
result = snapshot()
|
|
assert isinstance(result, list)
|
|
assert result == []
|
|
|
|
# Sort key tests
|
|
|
|
@pytest.mark.parametrize("sort_key, expected_order", [
|
|
# Sorted by __repr__ values: repr = <Adjustable B>, <Adjustable C>, <Adjustable A>
|
|
(repr, ["B", "C", "A"]),
|
|
|
|
# Sorted by __str__ values: str = "Beta", "Charlie", "Alpha"
|
|
(str, ["B", "C", "A"]),
|
|
|
|
# Sorted by ID: "id1", "id2", "id3"
|
|
(lambda a: a.ID, ["A", "C", "B"]),
|
|
|
|
# Sorted by lowercase name: "alpha", "beta", "charlie"
|
|
(lambda a: a.name.lower(), ["A", "B", "C"]),
|
|
])
|
|
def test_snapshot_sort_keys(sort_key, expected_order):
|
|
|
|
a = FakeAdjustable("id3", name="Alpha") # __str__ = "Alpha", __repr__ = <Adjustable Alpha>
|
|
b = FakeAdjustable("id1", name="Beta") # __str__ = "Beta", __repr__ = <Adjustable Beta>
|
|
c = FakeAdjustable("id2", name="Charlie") # __str__ = "Charlie", __repr__ = <Adjustable Charlie>
|
|
|
|
# Map from name to object so we can extract expected IDs later
|
|
name_to_obj = {adj.name: adj for adj in [a, b, c]}
|
|
unordered = [c, a, b] # intentionally unsorted
|
|
|
|
with patch("slic.utils.typecast.instances", return_value=unordered):
|
|
result = snapshot(sort_key=sort_key)
|
|
# We check that the objects returned match the expected names (using original mapping)
|
|
result_names = [adj.name for adj in result]
|
|
assert result_names == [name_to_obj[n].name for n in expected_order]
|