1- from  pyconfigparser  import  configparser , ConfigError , ConfigFileNotFoundError 
1+ from  pyconfigparser  import  ConfigParser , ConfigError , ConfigFileNotFoundError 
22from  config .schemas  import  SIMPLE_SCHEMA_CONFIG , UNSUPPORTED_OBJECT_KEYS_SCHEMA 
33import  unittest 
44import  os 
99
1010class  ConfigTestCase (unittest .TestCase ):
1111 def  setUp (self ) ->  None :
12-  configparser .hold_an_instance  =  False 
1312 os .environ ['DATE_FORMAT_TEST' ] =  DT_FMT_TEST 
1413 os .environ ['LOG_LEVEL_TEST' ] =  VAR_LOG_LEVEL_INFO 
1514
1615 def  test_schema_checking (self ):
16+  configparser  =  ConfigParser ()
1717 self .assertRaises (ConfigError , configparser .get_config , 1 )
1818
1919 def  test_config_without_file (self ):
20+  configparser  =  ConfigParser ()
2021 self .assertRaises (ConfigFileNotFoundError , configparser .get_config , SIMPLE_SCHEMA_CONFIG ,
2122 'config' ,
2223 'some_non_exists_file.json' )
2324
2425 def  test_undefined_env_var (self ):
2526 try :
27+  configparser  =  ConfigParser ()
2628 configparser .get_config (file_name = 'config.yaml' )
2729 except  Exception  as  e :
2830 self .assertIn ('Environment' , str (e ))
2931
32+  configparser  =  ConfigParser ()
33+  configparser .ignore_unset_env_vars  =  True 
34+  configparser .get_config (file_name = 'config.yaml' )
35+ 
3036 def  test_to_access_attr_from_config (self ):
37+  configparser  =  ConfigParser ()
3138 config  =  configparser .get_config (SIMPLE_SCHEMA_CONFIG )
3239 self .assertEqual (VAR_LOG_LEVEL_INFO , config .core .logging .level )
3340 self .assertEqual (DT_FMT_TEST , config .core .logging .datefmt )
@@ -36,28 +43,55 @@ def test_to_access_attr_from_config(self):
3643 self .assertEqual ('Mike' , config .core .obj_list [0 ]['name' ]) # <- using subscriptable access 
3744
3845 def  test_access_fake_attr (self ):
46+  configparser  =  ConfigParser ()
3947 config  =  configparser .get_config (SIMPLE_SCHEMA_CONFIG )
4048 self .assertRaises (AttributeError , lambda : config .fake_attr )
4149
4250 def  test_unsupported_object_key (self ):
51+  configparser  =  ConfigParser ()
4352 self .assertRaises (ConfigError , configparser .get_config , UNSUPPORTED_OBJECT_KEYS_SCHEMA ,
4453 file_name = 'unsupported_object_key.json' )
4554
46-  def  test_set_hold_an_invalid_instance (self ):
47-  def  assign_a_bad_type ():
48-  configparser .hold_an_instance  =  []
49-  self .assertRaises (ValueError , assign_a_bad_type )
50- 
5155 def  test_config_with_wrong_json_model (self ):
56+  configparser  =  ConfigParser ()
5257 self .assertRaises (ConfigError , configparser .get_config , SIMPLE_SCHEMA_CONFIG , file_name = 'wrong_model.json' )
5358
5459 def  test_config_file_with_unsupported_extension (self ):
60+  configparser  =  ConfigParser ()
5561 self .assertRaises (ConfigError , configparser .get_config , SIMPLE_SCHEMA_CONFIG , file_name = 'config.bad_extension' )
5662
5763 def  test_bad_decoder_error (self ):
64+  configparser  =  ConfigParser ()
5865 self .assertRaises (ConfigError , configparser .get_config , SIMPLE_SCHEMA_CONFIG , file_name = 'bad_content.json' )
5966 self .assertRaises (ConfigError , configparser .get_config , SIMPLE_SCHEMA_CONFIG , file_name = 'bad_content.yaml' )
6067
68+  def  test_caching_instance (self ):
69+  configparser  =  ConfigParser ()
70+  config1  =  configparser .get_config ()
71+  config2  =  configparser .get_config ()
72+  self .assertIs (config1 , config2 )
73+  configparser .hold_an_instance  =  False 
74+ 
75+  config2  =  configparser .get_config ()
76+  self .assertIsNot (config1 , config2 )
77+ 
78+  def  test_configparser_config_switches (self ):
79+  configparser  =  ConfigParser ()
80+ 
81+  def  assign_a_bad_type_hold_an_instance ():
82+  configparser .hold_an_instance  =  []
83+ 
84+  def  assign_a_bad_type_ignore_unsetted_env_vars ():
85+  configparser .ignore_unset_env_vars  =  []
86+ 
87+  self .assertRaises (ValueError , assign_a_bad_type_hold_an_instance )
88+  self .assertRaises (ValueError , assign_a_bad_type_ignore_unsetted_env_vars )
89+  configparser .hold_an_instance  =  False 
90+  configparser .ignore_unset_env_vars  =  True 
91+  self .assertIs (configparser .hold_an_instance , False )
92+  self .assertIs (configparser .ignore_unset_env_vars , True )
93+  self .assertIsInstance (configparser .ignore_unset_env_vars , bool )
94+ 
6195
6296if  __name__  ==  '__main__' :
6397 unittest .main ()
0 commit comments