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
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
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]
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
class Main(Configurable): val = Int() foo = ConfigurableInstance( Foo, default_config={ 'cls': 'Sub', 'val1': 3, 'val2': 4 }, )
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
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
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
class Foo(Configurable): val = Int()
class SubNode2(Node): sub_val2 = Int(default=3)
class SubNode1(Node): sub_val1 = Int(default=2)
class Node(Configurable): val = Int(default=1)
class Bar(Configurable): val = Int(default=2) foo = ConfigurableInstance(Foo)
class Main(Configurable): val = Int() foo = ConfigurableInstance(Foo, default_config=dict(val=10))
class Main(Configurable): val = Int() sub = ConfigurableInstance(Sub, default_config=dict(val=10))
class Sub(Configurable): val = Int()
class Test(Configurable): val = Int()
class Bar(Configurable): val = Int(default=2) foo = ConfigurableInstance(Foo, default_config={'val': 3})
class Sub(Foo): val2 = Int(2)
class Foo(Configurable): val1 = Int(1)
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)
class Foo(Configurable): val = Int(default=1)
class Test(Configurable): val = Int(default=1)