def test_read_config_file(self):
        a_configuration = configuration.Configuration()

        rule_handler_section = rule_handler.RuleHandlerConfigModel()
        a_configuration.add_section(rule_handler_section)

        a_rule_set_section_handler = rule_handler.RuleSetSectionHandler()
        a_configuration.register_section_handler(p_section_handler=a_rule_set_section_handler)

        filename = os.path.join(os.path.dirname(__file__), "resources/ruleset_handler.test.config")
        a_configuration.read_config_file(p_filename=filename)

        ruleset_configs = a_rule_set_section_handler.rule_set_configs

        self.assertIsNotNone(ruleset_configs)

        self.assertEqual(len(ruleset_configs), 2)

        self.assertIn("user1", ruleset_configs)
        self.assertIn("user2", ruleset_configs)

        ruleset_user1 = ruleset_configs["user1"]
        self.assertEqual(len(ruleset_user1), 1)
        ruleset_user2 = ruleset_configs["user2"]
        self.assertEqual(len(ruleset_user2), 4)

        self.assertIsNotNone(a_configuration)
    def test_load_configuration(self):

        config = configuration.Configuration()
        model = SomeTestConfigModel()
        config.add_section(p_section=model)
        config_filename = os.path.join(self.get_test_data_path(),
                                       "test.config")
        config.read_config_file(p_filename=config_filename)

        self.assertEqual(234, model.int)
        self.assertEqual("There!", model.string)
        self.assertFalse(model.bool)

        self.assertEqual(len(model.int_array), 3)
        self.assertEqual(model.int_array[0], 1)
        self.assertEqual(model.int_array[1], 2)
        self.assertEqual(model.int_array[2], 3)

        self.assertEqual(len(model.empty_int_array), 3)
        self.assertEqual(model.empty_int_array[0], 4)
        self.assertEqual(model.empty_int_array[1], 5)
        self.assertEqual(model.empty_int_array[2], 6)

        self.assertEqual(len(model.string_array), 4)
        self.assertEqual(model.string_array[0], "somebody")
        self.assertEqual(model.string_array[1], "in")
        self.assertEqual(model.string_array[2], "there")
        self.assertEqual(model.string_array[3], "?")

        self.assertEqual(len(model.empty_string_array), 3)
        self.assertEqual(model.empty_string_array[0], "one")
        self.assertEqual(model.empty_string_array[1], "two")
        self.assertEqual(model.empty_string_array[2], "three")
    def test_override_by_environment(self):

        config = configuration.Configuration()
        model = SomeTestConfigModel()
        config.add_section(p_section=model)

        self.assertEqual(model.int, INT_VALUE)

        environment = {
            "{section}__int".format(section=SECTION_NAME): str(NEW_INT_VALUE)
        }

        config.read_environment_parameters(p_environment_dict=environment)

        self.assertEqual(model.int, NEW_INT_VALUE)
    def test_override_by_command_line_options(self):

        config = configuration.Configuration()
        model = SomeTestConfigModel()
        config.add_section(p_section=model)

        self.assertEqual(model.int, INT_VALUE)

        parameters = [
            "{section}.int={value}".format(section=SECTION_NAME,
                                           value=NEW_INT_VALUE)
        ]

        config.read_command_line_parameters(parameters)

        self.assertEqual(model.int, NEW_INT_VALUE)
    def test_valid_boolean_values(self):

        model = SomeTestConfigModel()

        config = configuration.Configuration()
        config.add_section(p_section=model)

        for value in configuration.VALID_BOOLEAN_FALSE_VALUES:
            config.set_config_value(p_section_name=SECTION_NAME,
                                    p_option="bool",
                                    p_option_value=value)
            self.assertFalse(model.bool)

        for value in configuration.VALID_BOOLEAN_TRUE_VALUES:
            config.set_config_value(p_section_name=SECTION_NAME,
                                    p_option="bool",
                                    p_option_value=value)
            self.assertTrue(model.bool)
    def test_invalid_none_boolean(self):

        model = SomeTestConfigModel()

        config = configuration.Configuration()
        config.add_section(p_section=model)
        exception = None

        try:
            config.set_config_value(p_section_name=SECTION_NAME,
                                    p_option="none_bool",
                                    p_option_value="123")

        except Exception as e:
            exception = e

        self.assertIsInstance(exception, configuration.ConfigurationException)
        self.assertIn("Invalid Boolean value", str(exception))
    def test_invalid_int(self):

        model = SomeTestConfigModel()

        config = configuration.Configuration()
        config.add_section(p_section=model)
        exception = None

        try:
            config.set_config_value(p_section_name=SECTION_NAME,
                                    p_option="int",
                                    p_option_value="hallo")

        except Exception as e:
            exception = e

        self.assertIsInstance(exception, configuration.ConfigurationException)
        self.assertIn("Invalid numerical value", str(exception))
Esempio n. 8
0
 def configuration_factory(self):
     return configuration.Configuration()