<s> from __future__ import division , print_function , unicode_literals <EOL> """<STR_LIT>""" <EOL> from mock import patch <EOL> import pytest <EOL> import sys <EOL> from sacred . experiment import Experiment <EOL> @ pytest . fixture <EOL> def ex ( ) : <EOL> return Experiment ( '<STR_LIT>' ) <EOL> def test_main ( ex ) : <EOL> @ ex . main <EOL> def foo ( ) : <EOL> pass <EOL> assert '<STR_LIT:foo>' in ex . commands <EOL> assert ex . commands [ '<STR_LIT:foo>' ] == foo <EOL> assert ex . default_command == '<STR_LIT:foo>' <EOL> def test_automain_imported ( ex ) : <EOL> main_called = [ False ] <EOL> with patch . object ( sys , '<STR_LIT>' , [ '<STR_LIT>' ] ) : <EOL> @ ex . automain <EOL> def foo ( ) : <EOL> main_called [ <NUM_LIT:0> ] = True <EOL> assert '<STR_LIT:foo>' in ex . commands <EOL> assert ex . commands [ '<STR_LIT:foo>' ] == foo <EOL> assert ex . default_command == '<STR_LIT:foo>' <EOL> assert main_called [ <NUM_LIT:0> ] is False <EOL> def test_automain_script_runs_main ( ex ) : <EOL> global __name__ <EOL> oldname = __name__ <EOL> main_called = [ False ] <EOL> try : <EOL> __name__ = '<STR_LIT:__main__>' <EOL> with patch . object ( sys , '<STR_LIT>' , [ '<STR_LIT>' ] ) : <EOL> @ ex . automain <EOL> def foo ( ) : <EOL> main_called [ <NUM_LIT:0> ] = True <EOL> assert '<STR_LIT:foo>' in ex . commands <EOL> assert ex . commands [ '<STR_LIT:foo>' ] == foo <EOL> assert ex . default_command == '<STR_LIT:foo>' <EOL> assert main_called [ <NUM_LIT:0> ] is True <EOL> finally : <EOL> __name__ = oldname <EOL> def test_fails_on_unused_config_updates ( ex ) : <EOL> @ ex . config <EOL> def cfg ( ) : <EOL> a = <NUM_LIT:1> <EOL> c = <NUM_LIT:3> <EOL> @ ex . main <EOL> def foo ( a , b = <NUM_LIT:2> ) : <EOL> return a + b <EOL> assert ex . run ( config_updates = { '<STR_LIT:a>' : <NUM_LIT:3> } ) . result == <NUM_LIT:5> <EOL> assert ex . run ( config_updates = { '<STR_LIT:b>' : <NUM_LIT:8> } ) . result == <NUM_LIT:9> <EOL> assert ex . run ( config_updates = { '<STR_LIT:c>' : <NUM_LIT:9> } ) . result == <NUM_LIT:3> <EOL> with pytest . raises ( KeyError ) : <EOL> ex . run ( config_updates = { '<STR_LIT:d>' : <NUM_LIT:3> } ) <EOL> def test_fails_on_nested_unused_config_updates ( ex ) : <EOL> @ ex . config <EOL> def cfg ( ) : <EOL> a = { '<STR_LIT:b>' : <NUM_LIT:1> } <EOL> d = { '<STR_LIT:e>' : <NUM_LIT:3> } <EOL> @ ex . main <EOL> def foo ( a ) : <EOL> return a [ '<STR_LIT:b>' ] <EOL> assert ex . run ( config_updates = { '<STR_LIT:a>' : { '<STR_LIT:b>' : <NUM_LIT:2> } } ) . result == <NUM_LIT:2> <EOL> assert ex . run ( config_updates = { '<STR_LIT:a>' : { '<STR_LIT:c>' : <NUM_LIT:5> } } ) . result == <NUM_LIT:1> <EOL> assert ex . run ( config_updates = { '<STR_LIT:d>' : { '<STR_LIT:e>' : <NUM_LIT:7> } } ) . result == <NUM_LIT:1> <EOL> with pytest . raises ( KeyError ) : <EOL> ex . run ( config_updates = { '<STR_LIT:d>' : { '<STR_LIT:f>' : <NUM_LIT:3> } } ) <EOL> def test_considers_captured_functions_for_fail_on_unused_config ( ex ) : <EOL> @ ex . config <EOL> def cfg ( ) : <EOL> a = <NUM_LIT:1> <EOL> @ ex . capture <EOL> def transmogrify ( a , b = <NUM_LIT:0> ) : <EOL> return a + b <EOL> @ ex . main <EOL> def foo ( ) : <EOL> return transmogrify ( ) <EOL> assert ex . run ( config_updates = { '<STR_LIT:a>' : <NUM_LIT:7> } ) . result == <NUM_LIT:7> <EOL> assert ex . run ( config_updates = { '<STR_LIT:b>' : <NUM_LIT:3> } ) . result == <NUM_LIT:4> <EOL> with pytest . raises ( KeyError ) : <EOL> ex . run ( config_updates = { '<STR_LIT:c>' : <NUM_LIT:3> } ) <EOL> def test_used_prefix_for_fail_on_unused_config ( ex ) : <EOL> @ ex . config <EOL> def cfg ( ) : <EOL> a = { '<STR_LIT:b>' : <NUM_LIT:1> } <EOL> @ ex . capture ( prefix = '<STR_LIT:a>' ) <EOL> def transmogrify ( b ) : <EOL> return b <EOL> @ ex . main <EOL> def foo ( ) : <EOL> return transmogrify ( ) <EOL> assert ex . run ( config_updates = { '<STR_LIT:a>' : { '<STR_LIT:b>' : <NUM_LIT:3> } } ) . result == <NUM_LIT:3> <EOL> with pytest . raises ( KeyError ) : <EOL> ex . run ( config_updates = { '<STR_LIT:b>' : <NUM_LIT:5> } ) <EOL> with pytest . raises ( KeyError ) : <EOL> ex . run ( config_updates = { '<STR_LIT:a>' : { '<STR_LIT:c>' : <NUM_LIT:5> } } ) <EOL> def test_using_a_named_config ( ex ) : <EOL> @ ex . config <EOL> def cfg ( ) : <EOL> a = <NUM_LIT:1> <EOL> @ ex . named_config <EOL> def ncfg ( ) : <EOL> a = <NUM_LIT:10> <EOL> @ ex . main <EOL> def run ( a ) : <EOL> return a <EOL> assert ex . run ( ) . result == <NUM_LIT:1> <EOL> assert ex . run ( named_configs = [ '<STR_LIT>' ] ) . result == <NUM_LIT:10> </s>
