Skip to content

Commit

Permalink
test_state: add test_mutable_copy for entire state and mutable vars
Browse files Browse the repository at this point in the history
Test case for #1841
  • Loading branch information
masenf committed Sep 20, 2023
1 parent 3113aec commit 8befd7d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
import datetime
import functools
import sys
Expand Down Expand Up @@ -1585,6 +1586,56 @@ def assert_custom_dirty():
assert_custom_dirty()


@pytest.mark.parametrize(
("copy_func",),
[
(copy.copy,),
(copy.deepcopy,),
],
)
def test_mutable_copy(mutable_state, copy_func):
"""Test that mutable types are copied correctly.
Args:
mutable_state: A test state.
copy_func: A copy function.
"""
ms_copy = copy_func(mutable_state)
assert ms_copy is not mutable_state
for attr in ("array", "hashmap", "test_set", "custom"):
assert getattr(ms_copy, attr) == getattr(mutable_state, attr)
assert getattr(ms_copy, attr) is not getattr(mutable_state, attr)
ms_copy.custom.array.append(42)
assert "custom" in ms_copy.dirty_vars
if copy_func is copy.copy:
assert "custom" in mutable_state.dirty_vars
else:
assert not mutable_state.dirty_vars


@pytest.mark.parametrize(
("copy_func",),
[
(copy.copy,),
(copy.deepcopy,),
],
)
def test_mutable_copy_vars(mutable_state, copy_func):
"""Test that mutable types are copied correctly.
Args:
mutable_state: A test state.
copy_func: A copy function.
"""
for attr in ("array", "hashmap", "test_set", "custom"):
var_orig = getattr(mutable_state, attr)
var_copy = copy_func(var_orig)
assert var_orig is not var_copy
assert var_orig == var_copy
# copied vars should never be proxies, as they by definition are no longer attached to the state.
assert not isinstance(var_copy, MutableProxy)


def test_duplicate_substate_class(duplicate_substate):
with pytest.raises(ValueError):
duplicate_substate()

0 comments on commit 8befd7d

Please sign in to comment.