<s> from __future__ import division , print_function , unicode_literals <EOL> import pytest <EOL> import sacred . optional as opt <EOL> from sacred . config import ConfigDict <EOL> from sacred . config . custom_containers import DogmaticDict , DogmaticList <EOL> @ pytest . fixture <EOL> def conf_dict ( ) : <EOL> cfg = ConfigDict ( { <EOL> "<STR_LIT:a>" : <NUM_LIT:1> , <EOL> "<STR_LIT:b>" : <NUM_LIT> , <EOL> "<STR_LIT:c>" : True , <EOL> "<STR_LIT:d>" : '<STR_LIT:string>' , <EOL> "<STR_LIT:e>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> "<STR_LIT:f>" : { '<STR_LIT:a>' : '<STR_LIT:b>' , '<STR_LIT:c>' : '<STR_LIT:d>' } , <EOL> } ) <EOL> return cfg <EOL> def test_config_dict_returns_dict ( conf_dict ) : <EOL> assert isinstance ( conf_dict ( ) , dict ) <EOL> def test_config_dict_result_contains_keys ( conf_dict ) : <EOL> cfg = conf_dict ( ) <EOL> assert set ( cfg . keys ( ) ) == { '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' , '<STR_LIT:f>' } <EOL> assert cfg [ '<STR_LIT:a>' ] == <NUM_LIT:1> <EOL> assert cfg [ '<STR_LIT:b>' ] == <NUM_LIT> <EOL> assert cfg [ '<STR_LIT:c>' ] <EOL> assert cfg [ '<STR_LIT:d>' ] == '<STR_LIT:string>' <EOL> assert cfg [ '<STR_LIT:e>' ] == [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> assert cfg [ '<STR_LIT:f>' ] == { '<STR_LIT:a>' : '<STR_LIT:b>' , '<STR_LIT:c>' : '<STR_LIT:d>' } <EOL> def test_fixing_values ( conf_dict ) : <EOL> assert conf_dict ( { '<STR_LIT:a>' : <NUM_LIT:100> } ) [ '<STR_LIT:a>' ] == <NUM_LIT:100> <EOL> @ pytest . mark . parametrize ( "<STR_LIT:key>" , [ "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:12> , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def test_config_dict_raises_on_invalid_keys ( key ) : <EOL> with pytest . raises ( KeyError ) : <EOL> ConfigDict ( { key : True } ) <EOL> @ pytest . mark . parametrize ( "<STR_LIT:value>" , [ lambda x : x , pytest , test_fixing_values ] ) <EOL> def test_config_dict_raises_on_invalid_values ( value ) : <EOL> with pytest . raises ( ValueError ) : <EOL> ConfigDict ( { "<STR_LIT>" : value } ) <EOL> def test_fixing_nested_dicts ( conf_dict ) : <EOL> cfg = conf_dict ( { '<STR_LIT:f>' : { '<STR_LIT:c>' : '<STR_LIT:t>' } } ) <EOL> assert cfg [ '<STR_LIT:f>' ] [ '<STR_LIT:a>' ] == '<STR_LIT:b>' <EOL> assert cfg [ '<STR_LIT:f>' ] [ '<STR_LIT:c>' ] == '<STR_LIT:t>' <EOL> def test_adding_values ( conf_dict ) : <EOL> cfg = conf_dict ( { '<STR_LIT:g>' : <NUM_LIT> , '<STR_LIT:h>' : { '<STR_LIT:i>' : <NUM_LIT:10> } } ) <EOL> assert cfg [ '<STR_LIT:g>' ] == <NUM_LIT> <EOL> assert cfg [ '<STR_LIT:h>' ] == { '<STR_LIT:i>' : <NUM_LIT:10> } <EOL> assert cfg . added == { '<STR_LIT:g>' , '<STR_LIT:h>' , '<STR_LIT>' } <EOL> def test_typechange ( conf_dict ) : <EOL> cfg = conf_dict ( { '<STR_LIT:a>' : '<STR_LIT:bar>' , '<STR_LIT:b>' : '<STR_LIT:foo>' , '<STR_LIT:c>' : <NUM_LIT:1> } ) <EOL> assert cfg . typechanged == { '<STR_LIT:a>' : ( int , type ( '<STR_LIT:bar>' ) ) , <EOL> '<STR_LIT:b>' : ( float , type ( '<STR_LIT:foo>' ) ) , <EOL> '<STR_LIT:c>' : ( bool , int ) } <EOL> def test_nested_typechange ( conf_dict ) : <EOL> cfg = conf_dict ( { '<STR_LIT:f>' : { '<STR_LIT:a>' : <NUM_LIT:10> } } ) <EOL> assert cfg . typechanged == { '<STR_LIT>' : ( type ( '<STR_LIT:a>' ) , int ) } <EOL> def is_dogmatic ( a ) : <EOL> if isinstance ( a , ( DogmaticDict , DogmaticList ) ) : <EOL> return True <EOL> elif isinstance ( a , dict ) : <EOL> return any ( is_dogmatic ( v ) for v in a . values ( ) ) <EOL> elif isinstance ( a , ( list , tuple ) ) : <EOL> return any ( is_dogmatic ( v ) for v in a ) <EOL> def test_result_of_conf_dict_is_not_dogmatic ( conf_dict ) : <EOL> cfg = conf_dict ( { '<STR_LIT:e>' : [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] } ) <EOL> assert not is_dogmatic ( cfg ) <EOL> @ pytest . mark . skipif ( not opt . has_numpy , reason = "<STR_LIT>" ) <EOL> def test_conf_scope_handles_numpy_bools ( ) : <EOL> cfg = ConfigDict ( { <EOL> "<STR_LIT:a>" : opt . np . bool_ ( <NUM_LIT:1> ) <EOL> } ) <EOL> assert '<STR_LIT:a>' in cfg ( ) <EOL> assert cfg ( ) [ '<STR_LIT:a>' ] <EOL> def test_conf_scope_contains_presets ( ) : <EOL> conf_dict = ConfigDict ( { <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } ) <EOL> cfg = conf_dict ( preset = { '<STR_LIT:a>' : <NUM_LIT> , '<STR_LIT>' : True } ) <EOL> assert set ( cfg . keys ( ) ) == { '<STR_LIT:a>' , '<STR_LIT>' , '<STR_LIT>' } <EOL> assert cfg [ '<STR_LIT:a>' ] == <NUM_LIT> <EOL> assert cfg [ '<STR_LIT>' ] == <NUM_LIT> <EOL> assert cfg [ '<STR_LIT>' ] is True <EOL> def test_conf_scope_does_not_contain_fallback ( ) : <EOL> config_dict = ConfigDict ( { <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } ) <EOL> cfg = config_dict ( fallback = { '<STR_LIT:a>' : <NUM_LIT> , '<STR_LIT:b>' : <NUM_LIT:10> } ) <EOL> assert set ( cfg . keys ( ) ) == { '<STR_LIT>' } <EOL> def test_fixed_subentry_of_preset ( ) : <EOL> config_dict = ConfigDict ( { } ) <EOL> cfg = config_dict ( preset = { '<STR_LIT:d>' : { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } } , fixed = { '<STR_LIT:d>' : { '<STR_LIT:a>' : <NUM_LIT:10> } } ) <EOL> assert set ( cfg . keys ( ) ) == { '<STR_LIT:d>' } <EOL> assert set ( cfg [ '<STR_LIT:d>' ] . keys ( ) ) == { '<STR_LIT:a>' , '<STR_LIT:b>' } <EOL> assert cfg [ '<STR_LIT:d>' ] [ '<STR_LIT:a>' ] == <NUM_LIT:10> <EOL> assert cfg [ '<STR_LIT:d>' ] [ '<STR_LIT:b>' ] == <NUM_LIT:2> </s>
