Skip to content

Commit 934e948

Browse files
authored
test(data): refactor test data helper classes for reuse and clarity (#996)
test(data): refactor test data helper classes for reuse and clarity - Removed `test` prefix from helper data classes to avoid confusion with actual tests - Consolidated duplicated test data into a shared `data.py` module - Removed duplicate / unused helper classes - Cleaned up imports and formatting (isort & black) - Improved overall test data organization and maintainability #ref: 979
1 parent 6c5a3af commit 934e948

File tree

3 files changed

+145
-181
lines changed

3 files changed

+145
-181
lines changed

tests/base/data.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from pathlib import Path
2+
from typing import Dict, List
3+
from unittest.mock import MagicMock
4+
5+
from hydrolib.core.base.models import BaseModel, ParsableFileModel
6+
from hydrolib.core.base.parser import DummmyParser
7+
from hydrolib.core.base.serializer import DummySerializer
8+
9+
10+
# Common test model classes to reduce duplication
11+
class SimpleTestModel(BaseModel):
12+
"""A simple test model with basic properties."""
13+
14+
name: str
15+
value: int
16+
17+
18+
class ModelWithLinks(BaseModel):
19+
"""A test model that overrides link methods."""
20+
21+
name: str
22+
23+
def is_file_link(self) -> bool:
24+
return True
25+
26+
def is_intermediate_link(self) -> bool:
27+
return True
28+
29+
30+
class ChildTestModel(ModelWithLinks):
31+
"""A child test model for hierarchy testing."""
32+
33+
value: int
34+
35+
36+
class ParentTestModel(ModelWithLinks):
37+
"""A parent test model for hierarchy testing."""
38+
39+
child: ChildTestModel
40+
children: List[ChildTestModel] = []
41+
42+
43+
class BaseModelWithFunc(BaseModel):
44+
"""A test base model that can track function calls."""
45+
46+
def test_func(self):
47+
"""Test function that can be used to track calls."""
48+
pass
49+
50+
51+
class ChildModelWithFunc(BaseModelWithFunc, ChildTestModel):
52+
"""A child model that includes the test_func method."""
53+
54+
pass
55+
56+
57+
class ParentModelWithFunc(BaseModelWithFunc, ParentTestModel):
58+
"""A parent model that includes the test_func method."""
59+
60+
pass
61+
62+
63+
# Common test model classes for ParsableFileModel tests
64+
class ParsableModelBase(ParsableFileModel):
65+
"""Base class for parsable file model tests with common structure."""
66+
67+
name: str = "default"
68+
value: int = 0
69+
70+
@classmethod
71+
def _filename(cls) -> str:
72+
return "test"
73+
74+
@classmethod
75+
def _ext(cls) -> str:
76+
return ".test"
77+
78+
79+
class ParsableModelWithMocks(ParsableModelBase):
80+
"""ParsableFileModel test class using MagicMock for serializer and parser."""
81+
82+
@classmethod
83+
def _get_serializer(cls):
84+
return MagicMock()
85+
86+
@classmethod
87+
def _get_parser(cls):
88+
return MagicMock(return_value={"name": "parsed", "value": 42})
89+
90+
91+
class ParsableModelWithDummies(ParsableModelBase):
92+
"""ParsableFileModel test class using DummySerializer and DummyParser."""
93+
94+
@classmethod
95+
def _get_serializer(cls):
96+
return DummySerializer.serialize
97+
98+
@classmethod
99+
def _get_parser(cls):
100+
return DummmyParser.parse
101+
102+
103+
class SaveModelBase(ParsableModelWithMocks):
104+
"""Base class for testing save functionality."""
105+
106+
@property
107+
def _resolved_filepath(self):
108+
return Path(f"{self.__class__.__name__.lower()}.test")
109+
110+
def _load(self, filepath: Path) -> Dict:
111+
# Override _load to avoid file not found error
112+
return {"name": self.__class__.__name__.lower(), "value": 100}

tests/base/test_base_models.py

Lines changed: 18 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,27 @@
11
import unittest
22
from pathlib import Path
3-
from typing import Any, Callable, Dict, List, Optional, Type, TypeVar
3+
from typing import Any
44
from unittest.mock import MagicMock, patch
55

66
from hydrolib.core.base.file_manager import FileLoadContext
77
from hydrolib.core.base.models import (
88
BaseModel,
99
DiskOnlyFileModel,
10-
ParsableFileModel,
1110
SerializerConfig,
1211
_should_execute,
1312
_should_traverse,
1413
)
15-
16-
17-
# Common test model classes to reduce duplication
18-
class SimpleTestModel(BaseModel):
19-
"""A simple test model with basic properties."""
20-
21-
name: str
22-
value: int
23-
24-
25-
class TestModelWithLinks(BaseModel):
26-
"""A test model that overrides link methods."""
27-
28-
name: str
29-
30-
def is_file_link(self) -> bool:
31-
return True
32-
33-
def is_intermediate_link(self) -> bool:
34-
return True
35-
36-
37-
class ChildTestModel(TestModelWithLinks):
38-
"""A child test model for hierarchy testing."""
39-
40-
value: int
41-
42-
43-
class ParentTestModel(TestModelWithLinks):
44-
"""A parent test model for hierarchy testing."""
45-
46-
child: ChildTestModel
47-
children: List[ChildTestModel] = []
48-
49-
50-
class TestBaseModelWithFunc(BaseModel):
51-
"""A test base model that can track function calls."""
52-
53-
def test_func(self):
54-
"""Test function that can be used to track calls."""
55-
pass
56-
57-
58-
class ChildModelWithFunc(TestBaseModelWithFunc, ChildTestModel):
59-
"""A child model that includes the test_func method."""
60-
61-
pass
62-
63-
64-
class ParentModelWithFunc(TestBaseModelWithFunc, ParentTestModel):
65-
"""A parent model that includes the test_func method."""
66-
67-
pass
14+
from tests.base.data import (
15+
BaseModelWithFunc,
16+
ChildModelWithFunc,
17+
ChildTestModel,
18+
ModelWithLinks,
19+
ParentModelWithFunc,
20+
ParentTestModel,
21+
ParsableModelWithMocks,
22+
SaveModelBase,
23+
SimpleTestModel,
24+
)
6825

6926

7027
class TestBaseModel(unittest.TestCase):
@@ -88,7 +45,7 @@ def test_is_file_link(self):
8845
self.assertFalse(model.is_file_link())
8946

9047
# Test overridden implementation
91-
model_with_links = TestModelWithLinks(name="test")
48+
model_with_links = ModelWithLinks(name="test")
9249
self.assertTrue(model_with_links.is_file_link())
9350

9451
def test_is_intermediate_link(self):
@@ -98,7 +55,7 @@ def test_is_intermediate_link(self):
9855
self.assertFalse(model.is_intermediate_link())
9956

10057
# Test overridden implementation
101-
model_with_links = TestModelWithLinks(name="test")
58+
model_with_links = ModelWithLinks(name="test")
10259
self.assertTrue(model_with_links.is_intermediate_link())
10360

10461
def test_show_tree(self):
@@ -123,7 +80,7 @@ def test_func(self):
12380
called_models.append(self)
12481

12582
# Patch the test_func method
126-
with patch.object(TestBaseModelWithFunc, "test_func", test_func):
83+
with patch.object(BaseModelWithFunc, "test_func", test_func):
12784
child1 = ChildModelWithFunc(name="child1", value=1)
12885
child2 = ChildModelWithFunc(name="child2", value=2)
12986
child3 = ChildModelWithFunc(name="child3", value=3)
@@ -148,48 +105,12 @@ def test_get_identifier(self):
148105
self.assertIsNone(model._get_identifier({"name": "test", "value": 42}))
149106

150107

151-
# Common test model classes for ParsableFileModel tests
152-
class TestParsableModelBase(ParsableFileModel):
153-
"""Base class for parsable file model tests."""
154-
155-
name: str = "default"
156-
value: int = 0
157-
158-
@classmethod
159-
def _filename(cls) -> str:
160-
return "test"
161-
162-
@classmethod
163-
def _ext(cls) -> str:
164-
return ".test"
165-
166-
@classmethod
167-
def _get_serializer(cls):
168-
return MagicMock()
169-
170-
@classmethod
171-
def _get_parser(cls):
172-
return MagicMock(return_value={"name": "parsed", "value": 42})
173-
174-
175-
class TestSaveModelBase(TestParsableModelBase):
176-
"""Base class for testing save functionality."""
177-
178-
@property
179-
def _resolved_filepath(self):
180-
return Path(f"{self.__class__.__name__.lower()}.test")
181-
182-
def _load(self, filepath: Path) -> Dict:
183-
# Override _load to avoid file not found error
184-
return {"name": self.__class__.__name__.lower(), "value": 100}
185-
186-
187108
class TestParsableFileModel(unittest.TestCase):
188109
"""Test cases for the ParsableFileModel class."""
189110

190111
def setUp(self):
191112
"""Set up test fixtures."""
192-
self.TestParsableModel = TestParsableModelBase
113+
self.TestParsableModel = ParsableModelWithMocks
193114

194115
def test_load(self):
195116
"""Test _load method."""
@@ -220,7 +141,7 @@ def test_save(self):
220141
mock_serializer = MagicMock()
221142

222143
# Create a test model class that uses the mock serializer
223-
class TestSaveModel(TestSaveModelBase):
144+
class TestSaveModel(SaveModelBase):
224145
@classmethod
225146
def _get_serializer(cls):
226147
return mock_serializer
@@ -256,7 +177,7 @@ def test_serialize(self):
256177
mock_serializer = MagicMock()
257178

258179
# Create a test model class that uses the mock serializer
259-
class TestSerializeModel(TestSaveModelBase):
180+
class TestSerializeModel(SaveModelBase):
260181
@classmethod
261182
def _get_serializer(cls):
262183
return mock_serializer

0 commit comments

Comments
 (0)