Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    def test_get_where_defined(self):
        config._set_option("s3.bucket", "some.bucket", "test")
        self.assertEqual("test", config.get_where_defined("s3.bucket"))

        with pytest.raises(RuntimeError) as e:
            config.get_where_defined("doesnt.exist")
        self.assertEqual(str(e.value), 'Config key "doesnt.exist" not defined.')
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    def test_get_where_defined(self):
        config._set_option('s3.bucket', 'some.bucket', 'test')
        self.assertEqual('test', config.get_where_defined('s3.bucket'))

        with pytest.raises(RuntimeError) as e:
            config.get_where_defined('doesnt.exist')
        self.assertEqual(str(e.value), 'Config key "doesnt.exist" not defined.')
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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)