126 lines
3.2 KiB
Python
126 lines
3.2 KiB
Python
import pytest
|
|
from unittest.mock import patch
|
|
from slic.utils.snapshot import snapshot
|
|
from slic.core.adjustable import Adjustable
|
|
|
|
class FakeAdjustable(Adjustable):
|
|
"""Mock sans repr problématique"""
|
|
_instances = set()
|
|
|
|
def __init__(self, ID, name=None, internal=False):
|
|
super().__init__(ID=ID, name=name or ID, internal=internal)
|
|
self._value = 0
|
|
self.__class__._instances.add(self)
|
|
|
|
def get_current_value(self):
|
|
return self._value
|
|
|
|
def is_moving(self):
|
|
return False
|
|
|
|
def set_target_value(self, value, *args, **kwargs):
|
|
return value
|
|
|
|
@classmethod
|
|
def clear(cls):
|
|
cls._instances = set()
|
|
|
|
def __hash__(self):
|
|
return hash((self.ID, self.name))
|
|
|
|
def __eq__(self, other):
|
|
return (self.ID, self.name) == (other.ID, other.name)
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clean_registry():
|
|
"""Reset avant chaque test"""
|
|
FakeAdjustable.clear()
|
|
yield
|
|
FakeAdjustable.clear()
|
|
|
|
test_cases = [
|
|
# Exclusion des internes
|
|
pytest.param(
|
|
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
|
["Visible"],
|
|
{},
|
|
id="exclude_internals"
|
|
),
|
|
|
|
# Inclusion des internes
|
|
pytest.param(
|
|
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
|
["Visible", "Hidden"],
|
|
{"include_internal": True},
|
|
id="include_internals"
|
|
),
|
|
|
|
# Cas vide
|
|
pytest.param(
|
|
[],
|
|
[],
|
|
{},
|
|
id="empty_case"
|
|
),
|
|
|
|
# Tri par string
|
|
pytest.param(
|
|
[("3", "C"), ("1", "A"), ("2", "B")],
|
|
["A", "B", "C"],
|
|
{"sort_key": str},
|
|
id="sort_by_str"
|
|
),
|
|
|
|
# Tri par ID
|
|
pytest.param(
|
|
[("3", "Z"), ("1", "A"), ("2", "M")],
|
|
["A", "M", "Z"],
|
|
{"sort_key": lambda a: a.ID},
|
|
id="sort_by_id"
|
|
),
|
|
|
|
# Tri case-insensitive
|
|
pytest.param(
|
|
[("3", "Charlie"), ("1", "alpha"), ("2", "Beta")],
|
|
["alpha", "Beta", "Charlie"],
|
|
{"sort_key": lambda a: a.name.lower()},
|
|
id="sort_case_insensitive"
|
|
),
|
|
|
|
# Tri par longueur
|
|
pytest.param(
|
|
[("1", "A"), ("2", "BB"), ("3", "CCC")],
|
|
["A", "BB", "CCC"],
|
|
{"sort_key": lambda a: len(a.name)},
|
|
id="sort_by_length"
|
|
),
|
|
|
|
# Tri combiné
|
|
pytest.param(
|
|
[("2", "B", False), ("3", "C", True), ("1", "A", False)],
|
|
["A", "B", "C"],
|
|
{"sort_key": lambda a: (a.internal, a.name)},
|
|
id="sort_complex"
|
|
)
|
|
]
|
|
|
|
@pytest.mark.parametrize("test_input,expected,kwargs", test_cases)
|
|
def test_snapshot(test_input, expected, kwargs):
|
|
"""Tests complets sans utiliser repr"""
|
|
for args in test_input:
|
|
FakeAdjustable(*args)
|
|
|
|
with patch('slic.utils.registry.instances',
|
|
return_value=list(FakeAdjustable._instances)):
|
|
result = snapshot(**kwargs)
|
|
|
|
if not expected:
|
|
assert result == []
|
|
elif 'sort_key' in kwargs:
|
|
assert [x.name for x in result] == expected, (
|
|
f"Erreur de tri. Obtenu: {[x.name for x in result]}")
|
|
else:
|
|
assert {x.name for x in result} == set(expected) |