This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import patch, MagicMock
|
||||
from slic.utils.snapshot import snapshot
|
||||
from slic.core.adjustable import Adjustable
|
||||
|
||||
class FakeAdjustable(Adjustable):
|
||||
"""Mock sans repr problématique"""
|
||||
_instances = set()
|
||||
|
||||
"""Mock complètement isolé"""
|
||||
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
|
||||
@@ -21,26 +18,10 @@ class FakeAdjustable(Adjustable):
|
||||
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()
|
||||
|
||||
# Tous les cas de test
|
||||
test_cases = [
|
||||
# Exclusion des internes
|
||||
pytest.param(
|
||||
@@ -49,7 +30,6 @@ test_cases = [
|
||||
{},
|
||||
id="exclude_internals"
|
||||
),
|
||||
|
||||
# Inclusion des internes
|
||||
pytest.param(
|
||||
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
||||
@@ -57,7 +37,6 @@ test_cases = [
|
||||
{"include_internal": True},
|
||||
id="include_internals"
|
||||
),
|
||||
|
||||
# Cas vide
|
||||
pytest.param(
|
||||
[],
|
||||
@@ -65,7 +44,6 @@ test_cases = [
|
||||
{},
|
||||
id="empty_case"
|
||||
),
|
||||
|
||||
# Tri par string
|
||||
pytest.param(
|
||||
[("3", "C"), ("1", "A"), ("2", "B")],
|
||||
@@ -73,54 +51,34 @@ test_cases = [
|
||||
{"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)
|
||||
"""Tests avec isolation absolue"""
|
||||
# Création des objets frais pour CE TEST uniquement
|
||||
test_objects = [FakeAdjustable(*args) for args in test_input]
|
||||
|
||||
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)
|
||||
# Mock COMPLET de la registry
|
||||
mock_registry = MagicMock()
|
||||
mock_registry.return_value = test_objects
|
||||
|
||||
with patch('slic.utils.registry.instances', new=mock_registry):
|
||||
# Mock aussi le registry interne si nécessaire
|
||||
with patch('slic.core.adjustable.Adjustable._instances', new=test_objects):
|
||||
result = snapshot(**kwargs)
|
||||
|
||||
# Vérification
|
||||
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