From 7f7af828e8ce900556e8f5eccd89abfbd4151fdf Mon Sep 17 00:00:00 2001 From: tligui_y Date: Wed, 30 Jul 2025 01:36:29 +0200 Subject: [PATCH] Update tests/test_utils_snapshot.py --- tests/test_utils_snapshot.py | 150 ++++++++++++++++++++--------------- 1 file changed, 87 insertions(+), 63 deletions(-) diff --git a/tests/test_utils_snapshot.py b/tests/test_utils_snapshot.py index 4096b2c34..f8ae22cbc 100644 --- a/tests/test_utils_snapshot.py +++ b/tests/test_utils_snapshot.py @@ -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"" 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 \ No newline at end of file + # 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"]) \ No newline at end of file