def test_pickle_get_root(): # Test that get_root() is reconstructed correctly for pickle loaded files. with tempfile.TemporaryFile() as fp: c1 = OmegaConf.from_dict(dict(a=dict( a1=1, a2=2, ), )) c2 = OmegaConf.from_dict( dict(b=dict( b1='???', b2=4, bb=dict( bb1=3, bb2=4, ), ), )) c3 = OmegaConf.merge(c1, c2) import pickle pickle.dump(c3, fp) fp.flush() fp.seek(0) loaded_c3 = pickle.load(fp) def test(conf): assert conf._get_root() == conf assert conf.a._get_root() == conf assert conf.b._get_root() == conf assert conf.b.bb._get_root() == conf assert c3 == loaded_c3 test(c3) test(loaded_c3)
def test_str_interpolation_1(): # Simplest str_interpolation c = OmegaConf.from_dict(dict( a='${referenced}', referenced='bar', )) assert c.referenced == 'bar' assert c.a == 'bar'
def test_env_interpolation1(): try: os.environ['foobar'] = '1234' c = OmegaConf.from_dict(dict(path='/test/${env:foobar}', )) assert c.path == '/test/1234' finally: del os.environ['foobar'] OmegaConf.clear_resolvers()
def test_str_interpolation_3(): # Test that str_interpolation works with complex strings c = OmegaConf.from_dict(dict( a='the year ${year}', year='of the cat', )) assert c.a == 'the year of the cat'
def test_str_interpolation_4(): # Test that a string with multiple str_interpolations works c = OmegaConf.from_dict( dict( a='${ha} ${ha} ${ha}, said Pennywise, ${ha} ${ha}... ${ha}!', ha='HA', )) assert c.a == 'HA HA HA, said Pennywise, HA HA... HA!'
def test_2_step_interpolation(): c = OmegaConf.from_dict( dict( src='bar', copy_src='${src}', copy_copy='${copy_src}', )) assert c.copy_src == 'bar' assert c.copy_copy == 'bar'
def test_iterate_keys(): # Test for k in conf loop returns all keys c = OmegaConf.from_dict(dict(a=1, b=2, c={})) keys = set(['a', 'b', 'c']) for k in c: assert k in keys keys.remove(k) assert len(keys) == 0
def test_env_interpolation_recursive2(): c = OmegaConf.from_dict( dict( path1='/test/${path2}', path2='/test/${path1}', )) with pytest.raises(RuntimeError): c.path1
def test_register_resolver_1(): try: OmegaConf.register_resolver("plus_10", lambda x: int(x) + 10) c = OmegaConf.from_dict(dict(k='${plus_10:990}', )) assert type(c.k) == int assert c.k == 1000 finally: OmegaConf.clear_resolvers()
def test_deep_str_interpolation_1(): # Test deep str_interpolation works c = OmegaConf.from_dict( dict( a='the answer to the universe and everything is ${nested.value}', nested=dict(value=42), )) assert c.a == 'the answer to the universe and everything is 42'
def test_get_root(): c = OmegaConf.from_dict(dict( a=123, b=dict( bb=456, cc=7, ), )) assert c._get_root() == c assert c.b._get_root() == c
def test_select(): c = OmegaConf.from_dict(dict( a=dict(v=1), b=dict(v=1), )) assert c.select('a') == {'v': 1} assert c.select('a.v') == 1 assert c.select('b.v') == 1 assert c.select('nope') is None
def test_deep_str_interpolation_2(): # Test that str_interpolation of a key that is nested works c = OmegaConf.from_dict( dict( out=42, deep=dict( inside='the answer to the universe and everything is ${out}', ), )) assert c.deep.inside == 'the answer to the universe and everything is 42'
def test_deepcopy(): c1 = OmegaConf.from_dict(dict( foo1='foo1', foo2='foo2', )) c2 = copy.deepcopy(c1) assert c2 == c1 c1.foo1 = "bar" assert c1.foo1 == 'bar' assert c2.foo1 == 'foo1'
def test_register_resolver_2(): # resolvers are always converted to stateless idempotent functions # subsequent calls to the same function with the same argument will always return the same value. # this is important to allow embedding of functions like time() without having the value change during # the program execution. try: OmegaConf.register_resolver("random", lambda _: random.randint(0, 10000000)) c = OmegaConf.from_dict(dict(k='${random:_}', )) assert c.k == c.k finally: OmegaConf.clear_resolvers()
def test_get_root_of_merged(): c1 = OmegaConf.from_dict(dict(a=dict( a1=1, a2=2, ), )) c2 = OmegaConf.from_dict( dict(b=dict( b1='???', b2=4, bb=dict( bb1=3, bb2=4, ), ), )) c3 = OmegaConf.merge(c1, c2) assert c3._get_root() == c3 assert c3.a._get_root() == c3 assert c3.b._get_root() == c3 assert c3.b.bb._get_root() == c3
def test_interpolation_fails_on_config(): # Test that a ValueError is thrown if an string interpolation used on config value c = OmegaConf.from_dict( dict( config_obj=dict( value1=42, value2=43, ), deep=dict(inside='${config_obj}', ), )) with pytest.raises(ValueError): c.deep.inside
def test_complex_str_interpolation_is_always_str_1(): c = OmegaConf.from_dict( dict( two=2, four=4, inter1='${four}${two}', inter2='4${two}', )) assert type(c.inter1) == str assert c.inter1 == '42' assert type(c.inter2) == str assert c.inter2 == '42'
def test_simple_str_interpolation_inherit_type(): # Test that str_interpolation of a key that is nested works c = OmegaConf.from_dict( dict( inter1='${answer1}', inter2='${answer2}', inter3='${answer3}', inter4='${answer4}', answer1=42, answer2=42.0, answer3=False, answer4='string', )) assert type(c.inter1) == int assert type(c.inter2) == float assert type(c.inter3) == bool assert type(c.inter4) == str
def test_create_from_dict__deprecated(): src = dict(a=1, b=2) c = OmegaConf.from_dict(src) assert c == src
def test_from_dict2(): d = dict(a=2, b=10) c = OmegaConf.from_dict(d) assert d == c
def test_from_nested_dict(): d = {'a': 2, 'b': {'c': {'f': 1}, 'd': {}}} c = OmegaConf.from_dict(d) assert d == c
def test_env_interpolation_not_found(): c = OmegaConf.from_dict(dict(path='/test/${env:foobar}', )) with pytest.raises(KeyError): c.path
def test_in(): c = OmegaConf.from_dict(dict(a=1, b=2, c={})) assert 'a' in c assert 'b' in c assert 'c' in c assert 'd' not in c
def test_from_dict1(): d = {'a': 2, 'b': 10} c = OmegaConf.from_dict(d) assert d == c
def test_str_interpolation_key_error_2(): # Test that a KeyError is thrown if an str_interpolation key is not available c = OmegaConf.from_dict(dict(a='${not.found}', )) with pytest.raises(KeyError): c.a