<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from __future__ import division <EOL> from __future__ import absolute_import <EOL> import unittest <EOL> import tempfile <EOL> import os <EOL> from pyexperiment import conf <EOL> class TestConf ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . reset_instance ( ) <EOL> def test_empty_conf ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . reset_instance ( ) <EOL> self . assertRaises ( KeyError , lambda : conf [ '<STR_LIT:a>' ] ) <EOL> self . assertRaises ( KeyError , conf . __getitem__ , '<STR_LIT:a>' ) <EOL> def test_empty_initialized_conf ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . reset_instance ( ) <EOL> conf . initialize ( ) <EOL> self . assertRaises ( KeyError , lambda : conf [ '<STR_LIT:a>' ] ) <EOL> def test_save_no_config ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT:a>' ] = <NUM_LIT:12> <EOL> with tempfile . NamedTemporaryFile ( ) as temp : <EOL> conf . save ( temp . name ) <EOL> conf . reset_instance ( ) <EOL> self . assertNotIn ( '<STR_LIT:a>' , conf ) <EOL> conf . load ( temp . name ) <EOL> self . assertIn ( '<STR_LIT:a>' , conf ) <EOL> self . assertEqual ( int ( conf [ '<STR_LIT:a>' ] ) , <NUM_LIT:12> ) <EOL> def test_in ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . initialize ( ) <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT:12> <EOL> self . assertTrue ( '<STR_LIT>' in conf ) <EOL> self . assertFalse ( '<STR_LIT>' in conf ) <EOL> def test_in_uninitialized ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT:12> <EOL> self . assertTrue ( '<STR_LIT>' in conf ) <EOL> self . assertFalse ( '<STR_LIT>' in conf ) <EOL> def test_get_default ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( KeyError , lambda : conf [ '<STR_LIT:a>' ] ) <EOL> self . assertRaises ( KeyError , conf . get , '<STR_LIT:a>' ) <EOL> self . assertEqual ( conf . get ( '<STR_LIT:a>' , default = <NUM_LIT:12> ) , <NUM_LIT:12> ) <EOL> self . assertRaises ( KeyError , conf . get , '<STR_LIT:a>' ) <EOL> def test_initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT:a>' ] = <NUM_LIT:12> <EOL> conf . initialize ( ) <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , <NUM_LIT:12> ) <EOL> def test_init_float ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT:a>' ] = <NUM_LIT> <EOL> conf . initialize ( ) <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , <NUM_LIT> ) <EOL> def test_init_string ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT:a>' ] = '<STR_LIT:foo>' <EOL> conf . initialize ( ) <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , '<STR_LIT:foo>' ) <EOL> def test_init_boolean ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT:a>' ] = False <EOL> conf . initialize ( ) <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , False ) <EOL> def test_init_list ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT:a>' ] = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> conf . initialize ( ) <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> def test_init_tuple ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT:a>' ] = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> conf . initialize ( ) <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> class TestConfFile ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> TEST_CONFIG = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . filename = tempfile . mkstemp ( ) [ <NUM_LIT:1> ] <EOL> with open ( self . filename , '<STR_LIT:w>' ) as outfile : <EOL> outfile . write ( self . TEST_CONFIG ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . reset_instance ( ) <EOL> os . remove ( self . filename ) <EOL> def test_initialize_with_file ( self ) : <EOL> """<STR_LIT>""" <EOL> conf [ '<STR_LIT:a>' ] = <NUM_LIT:12> <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , <NUM_LIT:12> ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> conf . initialize ( self . filename ) <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , <NUM_LIT:12> ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:12> ) <EOL> def test_load_conf ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> a_conf = conf [ '<STR_LIT>' ] <EOL> self . assertEqual ( a_conf , '<STR_LIT>' ) <EOL> b_conf = conf [ '<STR_LIT>' ] <EOL> self . assertEqual ( b_conf , '<STR_LIT>' ) <EOL> c_conf = conf [ '<STR_LIT>' ] <EOL> self . assertEqual ( c_conf , '<STR_LIT:True>' ) <EOL> def test_save_load_config ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> filename = tempfile . mkstemp ( ) [ <NUM_LIT:1> ] <EOL> conf . save ( filename ) <EOL> conf . reset_instance ( ) <EOL> self . assertRaises ( KeyError , <EOL> lambda : conf [ '<STR_LIT>' ] ) <EOL> conf . load ( filename ) <EOL> a_conf = conf [ '<STR_LIT>' ] <EOL> self . assertEqual ( a_conf , '<STR_LIT>' ) <EOL> b_conf = conf [ '<STR_LIT>' ] <EOL> self . assertEqual ( b_conf , '<STR_LIT>' ) <EOL> c_conf = conf [ '<STR_LIT>' ] <EOL> self . assertEqual ( c_conf , '<STR_LIT:True>' ) <EOL> self . assertTrue ( os . path . isfile ( filename ) ) <EOL> os . remove ( filename ) <EOL> def test_len ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> self . assertEqual ( len ( conf ) , <NUM_LIT:3> ) <EOL> def test_set_value ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> self . assertFalse ( '<STR_LIT:a>' in conf ) <EOL> conf [ '<STR_LIT:a>' ] = <NUM_LIT:2> <EOL> self . assertTrue ( '<STR_LIT:a>' in conf ) <EOL> self . assertEqual ( conf [ '<STR_LIT:a>' ] , <NUM_LIT:2> ) <EOL> def test_set_value_section ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> self . assertFalse ( '<STR_LIT>' in conf ) <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT:2> <EOL> self . assertTrue ( '<STR_LIT>' in conf ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> def test_get_or_set ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> self . assertRaises ( KeyError , lambda : conf [ '<STR_LIT:a>' ] ) <EOL> self . assertRaises ( KeyError , conf . get , '<STR_LIT:a>' ) <EOL> self . assertFalse ( '<STR_LIT:a>' in conf ) <EOL> self . assertEqual ( conf . get_or_set ( '<STR_LIT:a>' , <NUM_LIT:12> ) , <NUM_LIT:12> ) <EOL> self . assertEqual ( conf . get ( '<STR_LIT:a>' ) , <NUM_LIT:12> ) <EOL> self . assertTrue ( '<STR_LIT:a>' in conf ) <EOL> def test_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( list ( conf . keys ( ) ) , [ ] ) <EOL> conf . load ( self . filename ) <EOL> self . assertEqual ( len ( conf . keys ( ) ) , <NUM_LIT:3> ) <EOL> def test_override_with_args ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> self . assertTrue ( '<STR_LIT>' in conf ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> conf . override_with_args ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_override_with_args_level ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> conf [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> conf . override_with_args ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_override_with_args_level2 ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . load ( self . filename ) <EOL> conf . override_with_args ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_override_with_args_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conf . load ( self . filename , <EOL> spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> conf . override_with_args ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> conf . validate_config ( conf . base ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> def test_override_with_args_wrong ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> expected_error = ( <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" ) <EOL> conf . load ( self . filename , <EOL> spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> conf . override_with_args ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertRaisesRegexp ( <EOL> ValueError , <EOL> expected_error , <EOL> conf . validate_config , <EOL> conf . base ) <EOL> class TestConfSpec ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> TEST_CONFIG = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . filename = tempfile . mkstemp ( ) [ <NUM_LIT:1> ] <EOL> with open ( self . filename , '<STR_LIT:w>' ) as outfile : <EOL> outfile . write ( self . TEST_CONFIG ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> conf . reset_instance ( ) <EOL> os . remove ( self . filename ) <EOL> def test_load_with_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conf . load ( self . filename , <EOL> spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> self . assertTrue ( '<STR_LIT>' in conf ) <EOL> self . assertIsInstance ( conf [ '<STR_LIT>' ] , int ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:12> ) <EOL> def test_load_with_nested_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conf . load ( self . filename , <EOL> spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> self . assertTrue ( '<STR_LIT>' in conf ) <EOL> self . assertIsInstance ( conf [ '<STR_LIT>' ] , int ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:5> ) <EOL> self . assertTrue ( '<STR_LIT>' in conf ) <EOL> self . assertIsInstance ( conf [ '<STR_LIT>' ] , str ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_load_with_wrong_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> expected_error = ( <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" ) <EOL> self . assertRaisesRegexp ( <EOL> ValueError , <EOL> expected_error , <EOL> conf . load , self . filename , spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> def test_initialize_with_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> conf . initialize ( self . filename , spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:5> ) <EOL> def test_initialize_with_new_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> conf . initialize ( self . filename , spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:5> ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> def test_initialize_with_level_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT:12> <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:12> ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> conf . initialize ( self . filename , spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:5> ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:12> ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> def test_initialize_with_file_spec ( self ) : <EOL> """<STR_LIT>""" <EOL> spec = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conf [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> conf . initialize ( self . filename , spec = ( <EOL> [ option . encode ( ) for option in spec . split ( '<STR_LIT:\n>' ) ] ) ) <EOL> self . assertEqual ( conf [ '<STR_LIT>' ] , <NUM_LIT:12> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
