This commit is contained in:
@@ -1,16 +1,13 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch, MagicMock
|
||||||
from slic.utils.snapshot import snapshot
|
from slic.utils.snapshot import snapshot
|
||||||
from slic.core.adjustable import Adjustable
|
from slic.core.adjustable import Adjustable
|
||||||
|
|
||||||
class FakeAdjustable(Adjustable):
|
class FakeAdjustable(Adjustable):
|
||||||
"""Mock sans repr problématique"""
|
"""Mock complètement isolé"""
|
||||||
_instances = set()
|
|
||||||
|
|
||||||
def __init__(self, ID, name=None, internal=False):
|
def __init__(self, ID, name=None, internal=False):
|
||||||
super().__init__(ID=ID, name=name or ID, internal=internal)
|
super().__init__(ID=ID, name=name or ID, internal=internal)
|
||||||
self._value = 0
|
self._value = 0
|
||||||
self.__class__._instances.add(self)
|
|
||||||
|
|
||||||
def get_current_value(self):
|
def get_current_value(self):
|
||||||
return self._value
|
return self._value
|
||||||
@@ -21,26 +18,10 @@ class FakeAdjustable(Adjustable):
|
|||||||
def set_target_value(self, value, *args, **kwargs):
|
def set_target_value(self, value, *args, **kwargs):
|
||||||
return value
|
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):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
# Tous les cas de test
|
||||||
def clean_registry():
|
|
||||||
"""Reset avant chaque test"""
|
|
||||||
FakeAdjustable.clear()
|
|
||||||
yield
|
|
||||||
FakeAdjustable.clear()
|
|
||||||
|
|
||||||
test_cases = [
|
test_cases = [
|
||||||
# Exclusion des internes
|
# Exclusion des internes
|
||||||
pytest.param(
|
pytest.param(
|
||||||
@@ -49,7 +30,6 @@ test_cases = [
|
|||||||
{},
|
{},
|
||||||
id="exclude_internals"
|
id="exclude_internals"
|
||||||
),
|
),
|
||||||
|
|
||||||
# Inclusion des internes
|
# Inclusion des internes
|
||||||
pytest.param(
|
pytest.param(
|
||||||
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
||||||
@@ -57,7 +37,6 @@ test_cases = [
|
|||||||
{"include_internal": True},
|
{"include_internal": True},
|
||||||
id="include_internals"
|
id="include_internals"
|
||||||
),
|
),
|
||||||
|
|
||||||
# Cas vide
|
# Cas vide
|
||||||
pytest.param(
|
pytest.param(
|
||||||
[],
|
[],
|
||||||
@@ -65,7 +44,6 @@ test_cases = [
|
|||||||
{},
|
{},
|
||||||
id="empty_case"
|
id="empty_case"
|
||||||
),
|
),
|
||||||
|
|
||||||
# Tri par string
|
# Tri par string
|
||||||
pytest.param(
|
pytest.param(
|
||||||
[("3", "C"), ("1", "A"), ("2", "B")],
|
[("3", "C"), ("1", "A"), ("2", "B")],
|
||||||
@@ -73,54 +51,34 @@ test_cases = [
|
|||||||
{"sort_key": str},
|
{"sort_key": str},
|
||||||
id="sort_by_str"
|
id="sort_by_str"
|
||||||
),
|
),
|
||||||
|
|
||||||
# Tri par ID
|
# Tri par ID
|
||||||
pytest.param(
|
pytest.param(
|
||||||
[("3", "Z"), ("1", "A"), ("2", "M")],
|
[("3", "Z"), ("1", "A"), ("2", "M")],
|
||||||
["A", "M", "Z"],
|
["A", "M", "Z"],
|
||||||
{"sort_key": lambda a: a.ID},
|
{"sort_key": lambda a: a.ID},
|
||||||
id="sort_by_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)
|
@pytest.mark.parametrize("test_input,expected,kwargs", test_cases)
|
||||||
def test_snapshot(test_input, expected, kwargs):
|
def test_snapshot(test_input, expected, kwargs):
|
||||||
"""Tests complets sans utiliser repr"""
|
"""Tests avec isolation absolue"""
|
||||||
for args in test_input:
|
# Création des objets frais pour CE TEST uniquement
|
||||||
FakeAdjustable(*args)
|
test_objects = [FakeAdjustable(*args) for args in test_input]
|
||||||
|
|
||||||
with patch('slic.utils.registry.instances',
|
# Mock COMPLET de la registry
|
||||||
return_value=list(FakeAdjustable._instances)):
|
mock_registry = MagicMock()
|
||||||
result = snapshot(**kwargs)
|
mock_registry.return_value = test_objects
|
||||||
|
|
||||||
if not expected:
|
with patch('slic.utils.registry.instances', new=mock_registry):
|
||||||
assert result == []
|
# Mock aussi le registry interne si nécessaire
|
||||||
elif 'sort_key' in kwargs:
|
with patch('slic.core.adjustable.Adjustable._instances', new=test_objects):
|
||||||
assert [x.name for x in result] == expected, (
|
result = snapshot(**kwargs)
|
||||||
f"Erreur de tri. Obtenu: {[x.name for x in result]}")
|
|
||||||
else:
|
# Vérification
|
||||||
assert {x.name for x in result} == set(expected)
|
if not expected:
|
||||||
|
assert result == []
|
||||||
|
elif 'sort_key' in kwargs:
|
||||||
|
assert [x.name for x in result] == expected
|
||||||
|
else:
|
||||||
|
assert {x.name for x in result} == set(expected)
|
||||||
Reference in New Issue
Block a user