Пример #1
0
def test_attribute_descriptor1():
    assert not hasattr(attribute_descriptor('name', default=10), "check")

    class Foo:
        name = attribute_descriptor("name",
                                    default=10,
                                    content_type=int,
                                    nullable=False,
                                    constraint=lambda x: x % 10 == 0)

    x = Foo()
    assert x.name == 10

    x.name = 30
    assert x.name == 30

    with pytest.raises(ValueError):
        x.name = 3
    with pytest.raises(TypeError):
        x.name = "asdas"
    with pytest.raises(TypeError):
        x.name = 3.4
    with pytest.raises(ValueError):
        x.name = None
    del x.name
    assert x.name == 10  # back to the default value
Пример #2
0
def test_attribute_descriptor1():
    assert not hasattr(attribute_descriptor('name', default=10), "check")
    
    class Foo:
        name = attribute_descriptor("name", default=10, content_type=int, 
                                 nullable=False, constraint=lambda x: x%10==0)
        
    x = Foo()
    assert x.name == 10
    
    x.name = 30
    assert x.name == 30
    
    with pytest.raises(ValueError):
        x.name = 3
    with pytest.raises(TypeError):
        x.name = "asdas"
    with pytest.raises(TypeError):
        x.name = 3.4
    with pytest.raises(ValueError):
        x.name = None
    del x.name
    assert x.name==10  # back to the default value
Пример #3
0
 class Foo:
     a = attribute_descriptor('a', content_type=int, nullable=False)
Пример #4
0
 class Foo:
     x = attribute_descriptor("x",
                              default=10,
                              content_type=(int, float),
                              nullable=True)
Пример #5
0
 class Foo:
     a = attribute_descriptor("name",
                              default=10,
                              content_type=int,
                              nullable=True,
                              constraint=lambda x: x % 10 == 0)