Skip to content

Commit 02ab496

Browse files
committed
Return a deepcopy of the result in cachedResult
1 parent b53b0ac commit 02ab496

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/unittest_extensions/case.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ def cachedResult(self) -> Any:
8181
subject without executing it multiple times.
8282
8383
Raises `unittest_extensions.TestError` if subject has not been called.
84+
85+
The returned object is a copy of the result. Thus, the result cannot be
86+
mutated by mutating the returned object of this method.
8487
"""
8588
if not hasattr(self, "_subject_result"):
8689
raise TestError("Cannot call 'cachedResult' before calling 'result'")
87-
return self._subject_result
90+
# NOTE: deepcopy keeps a reference of the copied object. This can cause
91+
# issues with memory.
92+
return deepcopy(self._subject_result)
8893

8994
def assertResult(self, value):
9095
"""

src/unittest_extensions/tests/test_use_case.py

+11
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,14 @@ def subject(self, lst):
182182
def test_mutable_kwargs(self):
183183
self.subjectKwargs()["lst"].append(3)
184184
self.assertDictEqual(self._subjectKwargs, {"lst": [1, 2]})
185+
186+
187+
class TestCachedResult(TestCase):
188+
def subject(self, lst):
189+
return lst
190+
191+
@args({"lst": [1, 2]})
192+
def test_mutate_cached_result(self):
193+
self.assertResult([1, 2])
194+
self.cachedResult().append(3)
195+
self.assertListEqual(self.cachedResult(), [1, 2])

0 commit comments

Comments
 (0)