Update tests/test_utils_snapshot.py
Run CI Tests / test (push) Successful in 32s

This commit is contained in:
2025-07-30 01:36:29 +02:00
parent d5c7a86be1
commit 7f7af828e8
+87 -63
View File
@@ -3,78 +3,102 @@ from unittest.mock import patch
from slic.utils.snapshot import snapshot
class FakeAdjustable:
"""Mock adjustable with isolated registry"""
_registry = []
def __init__(self, ID, name=None, internal=False):
self.ID = ID
self.name = name or ID
self.internal = internal
self.__class__._registry.append(self)
def __repr__(self):
return f"<Adjustable {self.name}>"
def __str__(self):
return self.name
@classmethod
def clear(cls):
cls._registry = []
@pytest.mark.parametrize("test_input,expected,test_kwargs", [
# Default behavior - excludes internal adjustables
(
[FakeAdjustable("v1", "Visible", False),
FakeAdjustable("h1", "Hidden", True)],
["Visible"],
{}
),
@pytest.fixture
def clean_slate():
"""Complete test isolation"""
FakeAdjustable.clear()
yield
FakeAdjustable.clear()
# All test cases with proper isolation
@pytest.mark.parametrize("case", [
# Basic functionality
{
"id": "exclude_internal",
"input": [("v1", "Visible", False), ("h1", "Hidden", True)],
"expected": ["Visible"],
"kwargs": {}
},
{
"id": "include_internal",
"input": [("v1", "Visible", False), ("h1", "Hidden", True)],
"expected": ["Visible", "Hidden"],
"kwargs": {"include_internal": True}
},
{
"id": "empty_case",
"input": [],
"expected": [],
"kwargs": {}
},
# Include internal when requested
(
[FakeAdjustable("v1", "Visible", False),
FakeAdjustable("h1", "Hidden", True)],
["Visible", "Hidden"],
{"include_internal": True}
),
# All sorting variants
{
"id": "sort_repr",
"input": [("3", "C"), ("1", "A"), ("2", "B")],
"expected": ["A", "B", "C"],
"kwargs": {"sort_key": repr}
},
{
"id": "sort_str",
"input": [("3", "C"), ("1", "A"), ("2", "B")],
"expected": ["A", "B", "C"],
"kwargs": {"sort_key": str}
},
{
"id": "sort_id",
"input": [("3", "C"), ("1", "A"), ("2", "B")],
"expected": ["A", "B", "C"], # Sorted by ID
"kwargs": {"sort_key": lambda a: a.ID}
},
{
"id": "sort_lower",
"input": [("3", "Charlie"), ("1", "alpha"), ("2", "Beta")],
"expected": ["alpha", "Beta", "Charlie"],
"kwargs": {"sort_key": lambda a: a.name.lower()}
},
{
"id": "sort_length",
"input": [("1", "A"), ("2", "BB"), ("3", "CCC")],
"expected": ["A", "BB", "CCC"],
"kwargs": {"sort_key": lambda a: len(a.name)}
}
], ids=lambda case: case["id"])
def test_snapshot(case, clean_slate):
"""Parametrized test covering all cases with proper isolation"""
# Setup
for args in case["input"]:
FakeAdjustable(*args)
# Empty result case
(
[],
[],
{}
),
# Sorting by representation
(
[FakeAdjustable("3", "C"),
FakeAdjustable("1", "A"),
FakeAdjustable("2", "B")],
["A", "B", "C"],
{"sort_key": repr}
),
# Sorting by string conversion
(
[FakeAdjustable("3", "C"),
FakeAdjustable("1", "A"),
FakeAdjustable("2", "B")],
["A", "B", "C"],
{"sort_key": str}
),
# Sorting by ID
(
[FakeAdjustable("3", "C"),
FakeAdjustable("1", "A"),
FakeAdjustable("2", "B")],
["A", "B", "C"], # Names sorted by ID
{"sort_key": lambda a: a.ID}
),
# Case-insensitive name sorting
(
[FakeAdjustable("3", "Charlie"),
FakeAdjustable("1", "alpha"),
FakeAdjustable("2", "Beta")],
["alpha", "Beta", "Charlie"],
{"sort_key": lambda a: a.name.lower()}
)
])
def test_snapshot_all_cases(test_input, expected, test_kwargs):
with patch('slic.utils.registry.instances', return_value=test_input):
result = snapshot(**test_kwargs)
assert [x.name for x in result] == expected
# Mock the real registry to use our test doubles
with patch('slic.utils.registry.instances',
return_value=list(FakeAdjustable._registry)):
# Execute
result = snapshot(**case["kwargs"])
# Verify
if case["id"].startswith("sort_"):
# For sorting tests, verify the order
assert [x.name for x in result] == case["expected"]
else:
# For other tests, verify content
assert set(x.name for x in result) == set(case["expected"])