コード例 #1
0
def test_generate_shallow_copier_with_no_attributes():
    @attr.s(init=True)
    class StubModel:
        ...

    old_model = StubModel()

    copier = attr_extensions.generate_shallow_copier(StubModel)
    new_model = copier(old_model)

    assert new_model is not old_model
    assert isinstance(new_model, StubModel)
コード例 #2
0
def test_generate_shallow_copier_with_init_only_arguments():
    @attr.s(init=True)
    class StubModel:
        _gfd: int = attr.ib(init=True)
        baaaa: str = attr.ib(init=True)
        _blambat: bool = attr.ib(init=True)
        no: bytes = attr.ib(init=True)

    old_model = StubModel(gfd=42, baaaa="sheep", blambat=True, no=b"okokokok")

    copier = attr_extensions.generate_shallow_copier(StubModel)
    new_model = copier(old_model)

    assert new_model is not old_model
    assert new_model._gfd is old_model._gfd
    assert new_model.baaaa is old_model.baaaa
    assert new_model._blambat is old_model._blambat
    assert new_model.no is old_model.no
コード例 #3
0
def test_generate_shallow_copier():
    @attr.s(init=True)
    class StubModel:
        _foo: int = attr.ib(init=True)
        baaaa: str = attr.ib(init=True)
        _blam: bool = attr.ib(init=True)
        not_init: int = attr.ib(init=False)
        no: bytes = attr.ib(init=True)

    old_model = StubModel(foo=42, baaaa="sheep", blam=True, no=b"okokokok")
    old_model.not_init = 54234

    copier = attr_extensions.generate_shallow_copier(StubModel)
    new_model = copier(old_model)

    assert new_model is not old_model
    assert new_model._foo is old_model._foo
    assert new_model.baaaa is old_model.baaaa
    assert new_model._blam is old_model._blam
    assert new_model.not_init is old_model.not_init
    assert new_model.no is old_model.no
コード例 #4
0
def test_generate_shallow_copier_with_only_non_init_attrs():
    @attr.define()
    class StubModel:
        _gfd: int = attr.field(init=False)
        baaaa: str = attr.field(init=False)
        _blambat: bool = attr.field(init=False)
        no: bytes = attr.field(init=False)

    old_model = StubModel()
    old_model._gfd = 42
    old_model.baaaa = "sheep"
    old_model._blambat = True
    old_model.no = b"okokokok"

    copier = attr_extensions.generate_shallow_copier(StubModel)
    new_model = copier(old_model)

    assert new_model is not old_model
    assert new_model._gfd is old_model._gfd
    assert new_model.baaaa is old_model.baaaa
    assert new_model._blambat is old_model._blambat
    assert new_model.no is old_model.no