def test_parsing_env_vars_in_toml(self): """Test that environment variables get parsed in the TOML file.""" # Some useful variables. DEFAULT_VAL, DESIRED_VAL = "Christopher", "Walken" DUMMY_DEFINITION = "<test definition>" # Create a dummy default option. config._create_option( "_test.tomlTest", description="This option tests the TOML parser.", default_val=DEFAULT_VAL, ) config.get_config_options(force_reparse=True) self.assertEqual(config.get_option("_test.tomlTest"), DEFAULT_VAL) self.assertEqual( config.get_where_defined("_test.tomlTest"), ConfigOption.DEFAULT_DEFINITION ) os.environ["TEST_ENV_VAR"] = DESIRED_VAL # Override it with some TOML NEW_TOML = """ [_test] tomlTest="env:TEST_ENV_VAR" """ config._update_config_with_toml(NEW_TOML, DUMMY_DEFINITION) self.assertEqual(config.get_option("_test.tomlTest"), DESIRED_VAL) self.assertEqual(config.get_where_defined("_test.tomlTest"), DUMMY_DEFINITION)
def test_get_set_and_complex_config_options(self): """Verify that changing one option changes another, dependent one. This also implicitly tests simple and complex ConfigOptions as well as get_option() and set_option(). """ # Some useful variables. DUMMY_VAL_1, DUMMY_VAL_2, DUMMY_VAL_3 = "Steven", "Vincent", "Buscemi" # Set up both options. config._create_option( "_test.independentOption", description="This option can change at will", default_val=DUMMY_VAL_1, ) @config._create_option("_test.dependentOption") def _test_dependent_option(): """Depend on the value of _test.independentOption.""" return config.get_option("_test.independentOption") config.get_config_options(force_reparse=True) # Check that the default values are good. self.assertEqual(config.get_option("_test.independentOption"), DUMMY_VAL_1) self.assertEqual(config.get_option("_test.dependentOption"), DUMMY_VAL_1) self.assertEqual( config.get_where_defined("_test.independentOption"), ConfigOption.DEFAULT_DEFINITION, ) self.assertEqual( config.get_where_defined("_test.dependentOption"), ConfigOption.DEFAULT_DEFINITION, ) # Override the independent option. Both update! config.set_option("_test.independentOption", DUMMY_VAL_2) self.assertEqual(config.get_option("_test.independentOption"), DUMMY_VAL_2) self.assertEqual(config.get_option("_test.dependentOption"), DUMMY_VAL_2) self.assertEqual(config.get_where_defined("_test.independentOption"), config._USER_DEFINED) self.assertEqual( config.get_where_defined("_test.dependentOption"), ConfigOption.DEFAULT_DEFINITION, ) # Override the dependent option. Only that updates! config.set_option("_test.dependentOption", DUMMY_VAL_3) self.assertEqual(config.get_option("_test.independentOption"), DUMMY_VAL_2) self.assertEqual(config.get_option("_test.dependentOption"), DUMMY_VAL_3) self.assertEqual(config.get_where_defined("_test.independentOption"), config._USER_DEFINED) self.assertEqual(config.get_where_defined("_test.dependentOption"), config._USER_DEFINED)
def test_parsing_toml(self): """Test config._update_config_with_toml().""" # Some useful variables. DUMMY_VAL_1, DUMMY_VAL_2 = "Christopher", "Walken" DUMMY_DEFINITION = "<test definition>" # Create a dummy default option. config._create_option( "_test.tomlTest", description="This option tests the TOML parser.", default_val=DUMMY_VAL_1, ) config.get_config_options(force_reparse=True) self.assertEqual(config.get_option("_test.tomlTest"), DUMMY_VAL_1) self.assertEqual( config.get_where_defined("_test.tomlTest"), ConfigOption.DEFAULT_DEFINITION ) # Override it with some TOML NEW_TOML = ( """ [_test] tomlTest="%s" """ % DUMMY_VAL_2 ) config._update_config_with_toml(NEW_TOML, DUMMY_DEFINITION) self.assertEqual(config.get_option("_test.tomlTest"), DUMMY_VAL_2) self.assertEqual(config.get_where_defined("_test.tomlTest"), DUMMY_DEFINITION)
def test_parsing_env_vars_in_toml(self): """Test that environment variables get parsed in the TOML file.""" # Some useful variables. DEFAULT_VAL, DESIRED_VAL = 'Christopher', 'Walken' DUMMY_DEFINITION = '<test definition>' # Create a dummy default option. config._create_option('_test.tomlTest', description='This option tests the TOML parser.', default_val=DEFAULT_VAL) self.assertEqual(config.get_option('_test.tomlTest'), DEFAULT_VAL) self.assertEqual(config.get_where_defined('_test.tomlTest'), ConfigOption.DEFAULT_DEFINITION) os.environ['TEST_ENV_VAR'] = DESIRED_VAL # Override it with some TOML NEW_TOML = """ [_test] tomlTest="env:TEST_ENV_VAR" """ config._update_config_with_toml(NEW_TOML, DUMMY_DEFINITION) self.assertEqual(config.get_option('_test.tomlTest'), DESIRED_VAL) self.assertEqual(config.get_where_defined('_test.tomlTest'), DUMMY_DEFINITION)
def test_param_names_are_camel_case(self): """Test that param names must be camelCase. Note the exception is the "_test" section which is used for unit testing. """ with self.assertRaises(AssertionError): config._create_option("_test.snake_case")
def test_delete_option(self): # Create a dummy default option. config._create_option( "_test.testDeleteOption", description="This option tests the _delete_option function.", default_val="delete me!", ) config.get_config_options(force_reparse=True) self.assertEqual(config.get_option("_test.testDeleteOption"), "delete me!") config._delete_option("_test.testDeleteOption") with pytest.raises(RuntimeError) as e: config.get_option("_test.testDeleteOption") self.assertEqual( str(e.value), 'Config key "_test.testDeleteOption" not defined.' ) config._delete_option("_test.testDeleteOption")
def test_parsing_toml(self): """Test config._update_config_with_toml().""" # Some useful variables. DUMMY_VAL_1, DUMMY_VAL_2 = 'Christopher', 'Walken' DUMMY_DEFINITION = '<test definition>' # Create a dummy default option. config._create_option('_test.tomlTest', description='This option tests the TOML parser.', default_val=DUMMY_VAL_1) self.assertEqual(config.get_option('_test.tomlTest'), DUMMY_VAL_1) self.assertEqual(config.get_where_defined('_test.tomlTest'), ConfigOption.DEFAULT_DEFINITION) # Override it with some TOML NEW_TOML = """ [_test] tomlTest="%s" """ % DUMMY_VAL_2 config._update_config_with_toml(NEW_TOML, DUMMY_DEFINITION) self.assertEqual(config.get_option('_test.tomlTest'), DUMMY_VAL_2) self.assertEqual(config.get_where_defined('_test.tomlTest'), DUMMY_DEFINITION)
def test_cannot_overwrite_config_key(self): """Test overwriting a config option using _create_option.""" with self.assertRaises(AssertionError): config._create_option("_test.overwriteKey") config._create_option("_test.overwriteKey")
def test_invalid_config_section(self): """Test setting an invalid config section.""" with self.assertRaises(AssertionError): config._create_option("mySection.myParam")