コード例 #1
0
def test_int_numpy():
    from config import Int
    np = pytest.importorskip("numpy")

    item = Int()
    value = item.validate(np.int32(1))
    assert isinstance(value, int)
    assert value == 1
コード例 #2
0
def test_defaults():
    from config.items.lookup import LookupDatabase
    from config import Int

    lookup = LookupDatabase(Int(), ('foo', 'bar'))
    assert lookup[1, 2] is None

    lookup = LookupDatabase(Int(5), ('foo', 'bar'))
    assert lookup[1, 2] == 5
コード例 #3
0
def test_lookup_invalid():
    from config.items.lookup import LookupDatabase
    from config import Int

    # invalid key in configuration
    with pytest.raises(ValueError):
        LookupDatabase(
            item=Int(1),
            hierarchy=("type", "id"),
            lookups=[
                ("type", "LST", 2),
                ("type", "MST", 3),
                ("invalid", 5, 4),
            ],
        )

    # invalid key in configuration
    with pytest.raises(ValueError):
        LookupDatabase(
            item=Int(1),
            hierarchy=("type", "id"),
            lookups=[
                ("type", "MST", 3),
                ("type", 2),
            ],
        )

    # invalid value in configuration
    with pytest.raises(ValueError):
        LookupDatabase(
            item=Int(1),
            hierarchy=("type", "id"),
            lookups=[
                ("type", "MST", 3),
                ("type", "LST", "foo"),
            ],
        )

    lookup = LookupDatabase(
        item=Int(1),
        hierarchy=("type", "id"),
        lookups=[
            ("type", "MST", 3),
            ("type", "LST", 2),
            ("id", 1, 2),
        ],
    )
    with pytest.raises(IndexError):
        # to few indices
        lookup[1]

    with pytest.raises(IndexError):
        # too many indices
        lookup[1, 2, 3]
コード例 #4
0
def test_two_keys():
    from config.items.lookup import LookupDatabase
    from config import Int

    lookup = LookupDatabase(
        item=Int(1),
        hierarchy=("type", "id"),
        lookups=[
            ("type", "LST", 2),
            ("type", "MST", 3),
            ("id", 5, 4),
            ("id", 30, 5),
        ],
    )

    assert lookup["LST", 1] == 2
    assert lookup["MST", 2] == 3

    # id more important then type
    assert lookup["LST", 5] == 4

    # unknown type, but matches id
    assert lookup["SST", 30] == 5

    # not matching anything -> default
    assert lookup["SST", 3] == 1
コード例 #5
0
 class Main(Configurable):
     val = Int()
     foo = ConfigurableInstance(
         Foo,
         default_config={
             'cls': 'Sub',
             'val1': 3,
             'val2': 4
         },
     )
コード例 #6
0
def test_int_get_default_config():
    from config import Int

    item = Int()
    assert item.get_default_config() is None

    item = Int(default=1)
    assert item.get_default_config() == 1
コード例 #7
0
def test_single_key():
    from config import Int
    from config.items.lookup import LookupDatabase

    lookup = LookupDatabase(
        item=Int(1),
        hierarchy=("type", ),
        lookups=[
            ("type", "LST", 2),
            ("type", "MST", 3),
        ],
    )

    assert lookup["LST"] == 2
    assert lookup["MST"] == 3

    # not matching anything -> default
    assert lookup["SST"] == 1
コード例 #8
0
def test_invalid():
    from config import Configurable, Float, Int
    from config.items.lookup import Lookup, LookupDatabase
    from config.exceptions import ConfigError

    class Cleaning(Configurable):
        level = Lookup(item=Float(5.0, allow_none=False),
                       hierarchy=('type', 'id'))

    with pytest.raises(ConfigError):
        Cleaning(level=None)

    cleaning = Cleaning()

    wrong_item = LookupDatabase(item=Int(5), hierarchy=('type', 'id'))
    with pytest.raises(ConfigError):
        cleaning.level = wrong_item

    wrong_hierarchy = LookupDatabase(item=Float(5.0), hierarchy=('foo', 'id'))
    with pytest.raises(ConfigError):
        cleaning.level = wrong_hierarchy
コード例 #9
0
 class Foo(Configurable):
     val = Int()
コード例 #10
0
 class SubNode2(Node):
     sub_val2 = Int(default=3)
コード例 #11
0
 class SubNode1(Node):
     sub_val1 = Int(default=2)
コード例 #12
0
 class Node(Configurable):
     val = Int(default=1)
コード例 #13
0
 class Bar(Configurable):
     val = Int(default=2)
     foo = ConfigurableInstance(Foo)
コード例 #14
0
 class Main(Configurable):
     val = Int()
     foo = ConfigurableInstance(Foo, default_config=dict(val=10))
コード例 #15
0
 class Main(Configurable):
     val = Int()
     sub = ConfigurableInstance(Sub, default_config=dict(val=10))
コード例 #16
0
 class Sub(Configurable):
     val = Int()
コード例 #17
0
 class Test(Configurable):
     val = Int()
コード例 #18
0
 class Bar(Configurable):
     val = Int(default=2)
     foo = ConfigurableInstance(Foo, default_config={'val': 3})
コード例 #19
0
 class Sub(Foo):
     val2 = Int(2)
コード例 #20
0
 class Foo(Configurable):
     val1 = Int(1)
コード例 #21
0
def test_int_validate():
    from config import Int

    item = Int()
    assert item.validate(5) == 5
    assert item.validate(None) is None

    # float with is_integer() == True is allowed
    assert isinstance(item.validate(5.0), int)

    with pytest.raises(ConfigError):
        item.validate(5.1)

    with pytest.raises(ConfigError):
        item.validate('5.0')

    item = Int(default=1, allow_none=False)
    with pytest.raises(ConfigError):
        item.validate(None)
コード例 #22
0
 class Foo(Configurable):
     val = Int(default=1)
コード例 #23
0
 class Test(Configurable):
     val = Int(default=1)