<s> import os <EOL> import tempfile <EOL> from twisted . python import filepath <EOL> from twisted . trial import unittest <EOL> from piped import conf , exceptions <EOL> class ConfigurationManagerTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . cm = conf . ConfigurationManager ( ) <EOL> def test_get_with_path ( self ) : <EOL> config = dict ( foo = <NUM_LIT> , bar = dict ( baz = '<STR_LIT:value>' ) ) <EOL> self . cm . _config = config <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT:foo>' ) , <NUM_LIT> ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT:bar>' ) , dict ( baz = '<STR_LIT:value>' ) ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , '<STR_LIT:value>' ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEquals ( self . cm . get ( ( '<STR_LIT:bar>' , '<STR_LIT>' ) , '<STR_LIT>' ) , '<STR_LIT:value>' ) <EOL> def test_set_with_path ( self ) : <EOL> config = dict ( foo = <NUM_LIT> , bar = dict ( baz = '<STR_LIT:value>' ) ) <EOL> self . cm . _config = config <EOL> self . cm . set ( '<STR_LIT:foo>' , <NUM_LIT> ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT:foo>' ) , <NUM_LIT> ) <EOL> self . cm . set ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT:bar>' ) , dict ( baz = '<STR_LIT>' ) ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . cm . set ( '<STR_LIT>' , '<STR_LIT:c>' ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT:a>' ) , dict ( b = '<STR_LIT:c>' ) ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , '<STR_LIT:c>' ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , dict ( foo = <NUM_LIT> , bar = dict ( baz = '<STR_LIT>' ) , <EOL> a = dict ( b = '<STR_LIT:c>' ) ) ) <EOL> self . assertEquals ( self . cm . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:c>' ) <EOL> self . assertEquals ( self . cm . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_loading_nonexistent ( self ) : <EOL> nonexistent_file = filepath . FilePath ( __file__ ) . sibling ( '<STR_LIT>' ) <EOL> self . assertRaises ( exceptions . ConfigurationError , self . cm . load_from_file , nonexistent_file ) <EOL> def test_loading ( self ) : <EOL> test_config_file = filepath . FilePath ( __file__ ) . parent ( ) . preauthChild ( os . path . join ( '<STR_LIT:data>' , '<STR_LIT>' ) ) <EOL> self . cm . load_from_file ( test_config_file ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , { '<STR_LIT>' : { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:abc>' , '<STR_LIT>' : <NUM_LIT> } } } ) <EOL> def test_loading_invalid ( self ) : <EOL> test_invalid_config_file = filepath . FilePath ( __file__ ) . parent ( ) . preauthChild ( os . path . join ( '<STR_LIT:data>' , '<STR_LIT>' ) ) <EOL> self . assertRaises ( exceptions . ConfigurationError , self . cm . load_from_file , test_invalid_config_file ) <EOL> def test_loading_includes ( self ) : <EOL> test_includes_file = filepath . FilePath ( __file__ ) . parent ( ) . preauthChild ( os . path . join ( '<STR_LIT:data>' , '<STR_LIT>' ) ) <EOL> self . cm . load_from_file ( test_includes_file ) <EOL> expected_config = dict ( <EOL> added = <NUM_LIT> , <EOL> nested = dict ( <EOL> leaf = <NUM_LIT> , <EOL> another_leaf = '<STR_LIT>' <EOL> ) <EOL> ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , expected_config ) <EOL> def test_loading_nested_includes ( self ) : <EOL> test_includes_file = filepath . FilePath ( __file__ ) . parent ( ) . preauthChild ( os . path . join ( '<STR_LIT:data>' , '<STR_LIT>' ) ) <EOL> self . cm . load_from_file ( test_includes_file ) <EOL> expected_config = dict ( <EOL> bar = dict ( <EOL> root_level = dict ( <EOL> added = <NUM_LIT> , <EOL> nested = dict ( <EOL> leaf = <NUM_LIT> , <EOL> another_leaf = '<STR_LIT>' <EOL> ) <EOL> ) <EOL> ) , <EOL> baz = <NUM_LIT> <EOL> ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT:foo>' ) , expected_config ) <EOL> def test_loading_nonexistant_include_fails ( self ) : <EOL> fp = filepath . FilePath ( __file__ ) . child ( '<STR_LIT>' ) <EOL> self . assertRaises ( exceptions . ConfigurationError , self . cm . _load_includes , dict ( ) , [ fp ] , list ( ) ) <EOL> def test_empty_config ( self ) : <EOL> self . assertEquals ( self . cm . _load_config ( None , [ '<STR_LIT>' ] ) , dict ( ) ) <EOL> def test_non_dict_config ( self ) : <EOL> self . assertRaises ( exceptions . ConfigurationError , self . cm . _load_config , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ '<STR_LIT>' ] ) <EOL> class TestAliases ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . cm = conf . ConfigurationManager ( ) <EOL> def load_config_from_string ( self , string ) : <EOL> tmp = tempfile . NamedTemporaryFile ( suffix = '<STR_LIT>' ) <EOL> tmp . file . write ( string ) <EOL> tmp . file . flush ( ) <EOL> self . cm . load_from_file ( tmp . name ) <EOL> tmp . file . close ( ) <EOL> def test_simple_alias ( self ) : <EOL> config_string = """<STR_LIT>""" <EOL> self . load_config_from_string ( config_string ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT:foo>' ) , self . cm . get ( '<STR_LIT>' ) ) <EOL> def test_partial_alias ( self ) : <EOL> config_string = """<STR_LIT>""" <EOL> self . load_config_from_string ( config_string ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , dict ( bar = <NUM_LIT> , default = <NUM_LIT> ) ) <EOL> def test_nested_alises ( self ) : <EOL> config_string = """<STR_LIT>""" <EOL> self . load_config_from_string ( config_string ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT:foo>' ) , self . cm . get ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , <NUM_LIT> ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , <NUM_LIT> ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , <NUM_LIT> ) <EOL> def test_recursiveness ( self ) : <EOL> config_string = """<STR_LIT>""" <EOL> self . load_config_from_string ( config_string ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , self . cm . get ( '<STR_LIT:foo>' ) ) <EOL> self . assertEquals ( self . cm . get ( '<STR_LIT>' ) , <NUM_LIT> ) </s>
