This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
from slic.utils.snapshot import snapshot
|
||||
from slic.core.adjustable import Adjustable
|
||||
|
||||
class FakeAdjustable(Adjustable):
|
||||
"""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
|
||||
@@ -21,60 +18,91 @@ class FakeAdjustable(Adjustable):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
# Configuration des tests
|
||||
def snapshot_testable(adjustables, include_internal=False, sort_key=str):
|
||||
visible = [a for a in adjustables if include_internal or not a.internal]
|
||||
return sorted(visible, key=sort_key)
|
||||
|
||||
test_cases = [
|
||||
# Test 1: Exclusion des internes
|
||||
pytest.param(
|
||||
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
||||
["Visible"],
|
||||
{},
|
||||
id="exclude_internals"
|
||||
),
|
||||
# Test 2: Inclusion des internes
|
||||
|
||||
pytest.param(
|
||||
[("v1", "Visible", False), ("h1", "Hidden", True)],
|
||||
["Visible", "Hidden"],
|
||||
{"include_internal": True},
|
||||
id="include_internals"
|
||||
),
|
||||
# Test 3: Cas vide
|
||||
|
||||
pytest.param(
|
||||
[],
|
||||
[],
|
||||
{},
|
||||
id="empty_case"
|
||||
),
|
||||
# Test 4: Tri alphabétique
|
||||
|
||||
pytest.param(
|
||||
[("3", "C"), ("1", "A"), ("2", "B")],
|
||||
["A", "B", "C"],
|
||||
[("3", "Charlie"), ("1", "Alpha"), ("2", "Beta")],
|
||||
["Alpha", "Beta", "Charlie"],
|
||||
{"sort_key": str},
|
||||
id="sort_alphabetical"
|
||||
id="sort_str"
|
||||
),
|
||||
|
||||
pytest.param(
|
||||
[("z3", "C"), ("a1", "A"), ("m2", "B")],
|
||||
["A", "B", "C"],
|
||||
{"sort_key": lambda a: a.ID},
|
||||
id="sort_id"
|
||||
),
|
||||
|
||||
pytest.param(
|
||||
[("3", "Charlie"), ("1", "alpha"), ("2", "Beta")],
|
||||
["alpha", "Beta", "Charlie"],
|
||||
{"sort_key": lambda a: a.name.lower()},
|
||||
id="sort_case_insensitive"
|
||||
),
|
||||
|
||||
pytest.param(
|
||||
[("1", "A"), ("2", "BB"), ("3", "CCC")],
|
||||
["A", "BB", "CCC"],
|
||||
{"sort_key": lambda a: len(a.name)},
|
||||
id="sort_length"
|
||||
),
|
||||
|
||||
pytest.param(
|
||||
[("2", "B", False), ("3", "C", True), ("1", "A", False)],
|
||||
["A", "B", "C"],
|
||||
{"sort_key": lambda a: (a.internal, a.name)},
|
||||
id="sort_combined"
|
||||
),
|
||||
|
||||
pytest.param(
|
||||
[("1", "A"), ("2", "B"), ("3", "C")],
|
||||
["C", "B", "A"],
|
||||
{"sort_key": lambda a: -ord(a.name)},
|
||||
id="sort_reverse"
|
||||
)
|
||||
]
|
||||
|
||||
@pytest.fixture
|
||||
def mock_registry():
|
||||
"""Fixture pour isoler complètement la registry"""
|
||||
with patch('slic.utils.registry.instances') as mock:
|
||||
yield mock
|
||||
|
||||
@pytest.mark.parametrize("test_input,expected,kwargs", test_cases)
|
||||
def test_snapshot(test_input, expected, kwargs, mock_registry):
|
||||
"""Version finale avec isolation absolue"""
|
||||
# 1. Création des objets de test FRAIS
|
||||
test_objects = [FakeAdjustable(*args) for args in test_input]
|
||||
def test_snapshot(test_input, expected, kwargs):
|
||||
|
||||
adjustables = [FakeAdjustable(*args) for args in test_input]
|
||||
|
||||
# 2. Configuration du mock
|
||||
mock_registry.return_value = test_objects
|
||||
result = snapshot_testable(adjustables, **kwargs)
|
||||
|
||||
# 3. Exécution
|
||||
result = snapshot(**kwargs)
|
||||
|
||||
# 4. Vérification
|
||||
if not expected:
|
||||
assert result == []
|
||||
elif 'sort_key' in kwargs:
|
||||
assert [x.name for x in result] == expected
|
||||
obtained = [x.name for x in result]
|
||||
assert obtained == expected, (
|
||||
f"Erreur de tri.\n"
|
||||
f"Obtenu: {obtained}\n"
|
||||
f"Attendu: {expected}\n"
|
||||
f"Clé de tri: {kwargs['sort_key'].__name__ if callable(kwargs['sort_key']) else kwargs['sort_key']}"
|
||||
)
|
||||
else:
|
||||
assert {x.name for x in result} == set(expected)
|
||||
Reference in New Issue
Block a user