This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
from unittest.mock import patch
|
||||
from slic.utils.snapshot import snapshot
|
||||
from slic.core.adjustable import Adjustable
|
||||
|
||||
class FakeAdjustable(Adjustable):
|
||||
"""Test double that doesn't register in real registry"""
|
||||
"""Mock implémentant toutes les méthodes abstraites"""
|
||||
def __init__(self, ID, name=None, internal=False):
|
||||
super().__init__(ID=ID, name=name or ID, internal=internal)
|
||||
self._value = 0
|
||||
@@ -12,39 +12,37 @@ class FakeAdjustable(Adjustable):
|
||||
def get_current_value(self):
|
||||
return self._value
|
||||
|
||||
def is_moving(self):
|
||||
return False
|
||||
|
||||
def set_target_value(self, value, *args, **kwargs):
|
||||
return value
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Adjustable {self.name}>"
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def test_id(val):
|
||||
"""Generate stable test IDs for all cases"""
|
||||
# val is the entire parameter tuple (test_input, expected, kwargs)
|
||||
if not val[1]: # Empty expected list
|
||||
return "empty"
|
||||
if isinstance(val[1][0], str):
|
||||
if val[2].get('include_internal'):
|
||||
return "include_internals"
|
||||
if 'sort_key' in val[2]:
|
||||
return f"sort_{val[2]['sort_key'].__name__}"
|
||||
return val[1][0].lower()
|
||||
return str(val[1][0])
|
||||
|
||||
# Tous les cas de test avec IDs explicites
|
||||
test_cases = [
|
||||
# Basic functionality
|
||||
# Exclusion des internes par défaut
|
||||
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(
|
||||
[],
|
||||
[],
|
||||
@@ -52,22 +50,22 @@ test_cases = [
|
||||
id="empty_case"
|
||||
),
|
||||
|
||||
# Sorting variants
|
||||
# Tous les types de tri
|
||||
pytest.param(
|
||||
[("3", "C"), ("1", "A"), ("2", "B")],
|
||||
["A", "B", "C"],
|
||||
{"sort_key": repr},
|
||||
id="sort_repr"
|
||||
id="sort_by_repr"
|
||||
),
|
||||
pytest.param(
|
||||
[("3", "C"), ("1", "A"), ("2", "B")],
|
||||
["A", "B", "C"],
|
||||
{"sort_key": str},
|
||||
id="sort_str"
|
||||
id="sort_by_str"
|
||||
),
|
||||
pytest.param(
|
||||
[("3", "C"), ("1", "A"), ("2", "B")],
|
||||
["A", "B", "C"],
|
||||
[("3", "Z"), ("1", "A"), ("2", "M")],
|
||||
["A", "M", "Z"],
|
||||
{"sort_key": lambda a: a.ID},
|
||||
id="sort_by_id"
|
||||
),
|
||||
@@ -82,30 +80,29 @@ test_cases = [
|
||||
["A", "BB", "CCC"],
|
||||
{"sort_key": lambda a: len(a.name)},
|
||||
id="sort_by_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_by_internal_then_name"
|
||||
)
|
||||
]
|
||||
|
||||
@pytest.mark.parametrize("test_input,expected,kwargs", test_cases)
|
||||
def test_snapshot(test_input, expected, kwargs):
|
||||
"""Complete test with proper instance mocking"""
|
||||
# Create test objects
|
||||
test_objects = [FakeAdjustable(*args) for args in test_input]
|
||||
"""Test complet avec isolation et tous les types de tri"""
|
||||
# Création des objets
|
||||
test_objs = [FakeAdjustable(*x) for x in test_input]
|
||||
|
||||
# Mock the registry system
|
||||
with patch('slic.utils.registry.instances') as mock_instances, \
|
||||
patch('slic.core.adjustable.Adjustable._instances', new_callable=MagicMock) as mock_adj:
|
||||
|
||||
# Configure mocks
|
||||
mock_instances.return_value = test_objects
|
||||
mock_adj.return_value = test_objects
|
||||
|
||||
# Execute
|
||||
# Mocking
|
||||
with patch('slic.utils.registry.instances', return_value=test_objs):
|
||||
result = snapshot(**kwargs)
|
||||
|
||||
# Verify
|
||||
if "sort_key" in kwargs:
|
||||
# For sorting tests
|
||||
# Vérification adaptée au type de test
|
||||
if kwargs.get('include_internal'):
|
||||
assert len(result) == len(expected)
|
||||
if 'sort_key' in kwargs:
|
||||
assert [x.name for x in result] == expected
|
||||
else:
|
||||
# For filtering tests
|
||||
assert {x.name for x in result} == set(expected)
|
||||
Reference in New Issue
Block a user