|
| 1 | +import pytest |
| 2 | +from copy import copy, deepcopy |
| 3 | +from mockito import mock, when |
| 4 | + |
| 5 | + |
| 6 | +class TestDeepcopy: |
| 7 | + def test_dumb_mocks_are_copied_correctly(self): |
| 8 | + m = mock() |
| 9 | + m.foo = [1] |
| 10 | + n = deepcopy(m) |
| 11 | + assert m is not n |
| 12 | + assert n.foo == [1] |
| 13 | + |
| 14 | + m.foo.append(2) |
| 15 | + assert n.foo == [1] |
| 16 | + |
| 17 | + def test_strict_mocks_raise_on_unexpected_calls(self): |
| 18 | + m = mock(strict=True) |
| 19 | + with pytest.raises(RuntimeError) as exc: |
| 20 | + deepcopy(m) |
| 21 | + assert str(exc.value) == ( |
| 22 | + "'Dummy' has no attribute '__deepcopy__' configured" |
| 23 | + ) |
| 24 | + |
| 25 | + def test_configured_strict_mock_answers_correctly(self): |
| 26 | + m = mock(strict=True) |
| 27 | + when(m).__deepcopy__(...).thenReturn(42) |
| 28 | + assert deepcopy(m) == 42 |
| 29 | + |
| 30 | + def test_setting_none_enables_the_standard_implementation(self): |
| 31 | + m = mock({"__deepcopy__": None}, strict=True) |
| 32 | + m.foo = [1] |
| 33 | + |
| 34 | + n = deepcopy(m) |
| 35 | + assert m is not n |
| 36 | + assert n.foo == [1] |
| 37 | + |
| 38 | + m.foo.append(2) |
| 39 | + assert n.foo == [1] |
| 40 | + |
| 41 | + |
| 42 | + @pytest.mark.xfail(reason=( |
| 43 | + "the configuration is set on the mock's class, not the instance, " |
| 44 | + "which deepcopy does not copy" |
| 45 | + )) |
| 46 | + def test_deepcopy_of_a_configured_mock_is_a_new_mock(self): |
| 47 | + m = mock({"foo": [1]}, strict=True) |
| 48 | + n = deepcopy(m) |
| 49 | + |
| 50 | + m.foo.append(2) |
| 51 | + assert n.foo == [1] |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +class TestCopy: |
| 56 | + def test_dumb_mocks_are_copied_correctly(self): |
| 57 | + m = mock() |
| 58 | + m.foo = [1] |
| 59 | + n = copy(m) |
| 60 | + assert m is not n |
| 61 | + assert n.foo == [1] |
| 62 | + |
| 63 | + m.foo.append(2) |
| 64 | + assert n.foo == [1, 2] |
| 65 | + |
| 66 | + @pytest.mark.xfail(reason=( |
| 67 | + "not working for `copy` because __copy__ is accessed on the class, " |
| 68 | + "not the instance" |
| 69 | + )) |
| 70 | + def test_strict_mocks_raise_on_unexpected_calls(self): |
| 71 | + m = mock(strict=True) |
| 72 | + with pytest.raises(RuntimeError) as exc: |
| 73 | + copy(m) |
| 74 | + assert str(exc.value) == ( |
| 75 | + "'Dummy' has no attribute '__copy__' configured" |
| 76 | + ) |
0 commit comments