Example #1
0
def test_generate_deep_copier_with_only_init_attributes():
    @attr.s
    class StubBaseClass:
        recursor: int = attr.ib(init=True)
        _field: bool = attr.ib(init=True)
        foo: str = attr.ib(init=True)

    model = StubBaseClass(recursor=431, field=True, foo="blam")
    old_model_fields = stdlib_copy.copy(model)
    copied_recursor = object()
    copied_field = object()
    copied_foo = object()
    memo = {123: object()}

    with mock.patch.object(
            stdlib_copy,
            "deepcopy",
            side_effect=[copied_recursor, copied_field, copied_foo],
    ):
        attr_extensions.generate_deep_copier(StubBaseClass)(model, memo)

        stdlib_copy.deepcopy.assert_has_calls([
            mock.call(old_model_fields.recursor, memo),
            mock.call(old_model_fields._field, memo),
            mock.call(old_model_fields.foo, memo),
        ])

    assert model.recursor is copied_recursor
    assert model._field is copied_field
    assert model.foo is copied_foo
Example #2
0
def test_generate_deep_copier_with_only_non_init_attributes():
    @attr.s
    class StubBaseClass:
        end: str = attr.ib(init=False)
        _blam: bool = attr.ib(init=False)

    model = StubBaseClass()
    model.end = "the way"
    model._blam = "555555"
    old_model_fields = stdlib_copy.copy(model)
    copied_end = object()
    copied_blam = object()
    memo = {123: object()}

    with mock.patch.object(
            stdlib_copy,
            "deepcopy",
            side_effect=[copied_end, copied_blam],
    ):
        attr_extensions.generate_deep_copier(StubBaseClass)(model, memo)

        stdlib_copy.deepcopy.assert_has_calls([
            mock.call(old_model_fields.end, memo),
            mock.call(old_model_fields._blam, memo),
        ])

    assert model.end is copied_end
    assert model._blam is copied_blam
Example #3
0
def test_generate_deep_copier_with_no_attributes():
    @attr.s
    class StubBaseClass:
        ...

    model = StubBaseClass()
    memo = {123: object()}

    with mock.patch.object(
            stdlib_copy,
            "deepcopy",
            side_effect=NotImplementedError,
    ):
        attr_extensions.generate_deep_copier(StubBaseClass)(model, memo)

        stdlib_copy.deepcopy.assert_not_called()
Example #4
0
def test_generate_deep_copier():
    @attr.define
    class StubBaseClass:
        recursor: int = attr.field()
        _field: bool = attr.field()
        foo: str = attr.field()
        end: str = attr.field(init=False)
        _blam: bool = attr.field(init=False)

    model = StubBaseClass(recursor=431, field=True, foo="blam")
    model.end = "the way"
    model._blam = "555555"
    old_model_fields = stdlib_copy.copy(model)
    copied_recursor = object()
    copied_field = object()
    copied_foo = object()
    copied_end = object()
    copied_blam = object()
    memo = {123: object()}

    with mock.patch.object(
        stdlib_copy,
        "deepcopy",
        side_effect=[copied_recursor, copied_field, copied_foo, copied_end, copied_blam],
    ):
        attr_extensions.generate_deep_copier(StubBaseClass)(model, memo)

        stdlib_copy.deepcopy.assert_has_calls(
            [
                mock.call(old_model_fields.recursor, memo),
                mock.call(old_model_fields._field, memo),
                mock.call(old_model_fields.foo, memo),
                mock.call(old_model_fields.end, memo),
                mock.call(old_model_fields._blam, memo),
            ]
        )

    assert model.recursor is copied_recursor
    assert model._field is copied_field
    assert model.foo is copied_foo
    assert model.end is copied_end
    assert model._blam is copied_blam