Esempio n. 1
0
    def test_random_normal(self):
        manager = _OptionManager(self._APP_NAME)

        test_case = self._gen_random_inputs(self.__TEST_COUNT, True)

        environs = {}
        configs = defaultdict(dict)
        args = []

        for category, values in test_case.items():
            for name, value in values.items():
                if value.is_env_choosable:
                    environs.update(
                        {manager.env_name(category, name): str(value.e_val)})

                if value.is_file_choosable:
                    configs[category].update({name: value.f_val})

                if value.is_user_choosable:
                    args.append('{}={}'.format(
                        manager.long_arg(category, name), value.u_val))

        del manager

        with utils.config_file(configs) as temp_file:
            manager = _OptionManager(self._APP_NAME, environ=environs)
            manager.load(temp_file)

            self.__check_all_values(manager, test_case, lambda v: v.expected,
                                    args)
Esempio n. 2
0
    def test_normal(self):
        test_options = self._gen_random_inputs(self.__TEST_COUNT)

        # This is the temporal manager to call the env_name().
        manager = _OptionManager(self._APP_NAME)

        env_dict = {}
        conf_dict = defaultdict(dict)

        for category, values in test_options.items():
            for name, value in values.items():
                if value.is_env_choosable:
                    env_dict.update(
                        {manager.env_name(category, name): str(value.e_val)})

                if value.is_file_choosable:
                    conf_dict[category].update({name: value.f_val})

        del manager

        # Run normal valid test
        with utils.config_file(conf_dict) as temp_file:
            manager = _OptionManager(self._APP_NAME, environ=env_dict)

            manager.load(temp_file)

            for category, values in test_options.items():
                for name, value in values.items():
                    default = manager.default_value(category,
                                                    name,
                                                    default=value.default,
                                                    type=value.type)

                    self.assertEqual(default, value.expected)
Esempio n. 3
0
    def test_invalid_initialization(self):
        # 1. Enter invalid type.
        with self.assertRaises(SGLException) as err:
            _OptionManager(self._APP_NAME, [self._APP_NAME])

        self.assertEqual(err.exception.code, SGL_PARSER_UNEXPECTED_ENV_TYPE)

        # 2. Enter dict converted os.environ as environ target
        _OptionManager(self._APP_NAME, dict(os.environ))
Esempio n. 4
0
    def test_default_value_action(self):
        manager = _OptionManager(self._APP_NAME)

        test_case = self._gen_random_inputs(self.__TEST_COUNT, False)

        self.__check_all_values(manager, test_case,
                                lambda value: value.default)
Esempio n. 5
0
    def test_env_value_action(self):
        manager = _OptionManager(self._APP_NAME)

        test_case = self._gen_random_inputs(self.__TEST_COUNT, False)

        environs = {
            manager.env_name(category, name): str(value.e_val)
            for category, values in test_case.items()
            for name, value in values.items()
        }

        del manager

        manager = _OptionManager(self._APP_NAME, environ=environs)

        self.__check_all_values(manager, test_case, lambda value: value.e_val)
Esempio n. 6
0
    def test_invalid_loading(self):
        manager = _OptionManager(self._APP_NAME)

        with self.assertRaises(SGLException) as err:
            manager.load(utils.get_temp_file('invalid_path'))

        self.assertEqual(err.exception.code, SGL_PARSER_CONFIG_NOT_EXIST)
Esempio n. 7
0
    def __test_invalid_naming(self, str_func, str_list):
        manager = _OptionManager(self._APP_NAME)
        name_t = namedtuple('name_t', ('name', 'sub'))

        for _ in range(self.__TEST_COUNT):
            invalid_strings = [str_func(c) for c in str_list]

            # 1. Try constructor's name format check
            for case in invalid_strings:
                with self.assertRaises(SGLException) as err:
                    _OptionManager(case)

                self.assertEqual(err.exception.code,
                                 SGL_PARSER_INVALID_NAME_FORMAT)

            # 2. Try each member function's name format check
            valid = self._gen_random_string()

            test_case = [(name_t(invalid, valid), name_t(valid, invalid))
                         for invalid in invalid_strings]

            for case in [v for sub in test_case for v in sub]:
                for func in (manager.dest_name, manager.env_name,
                             manager.long_arg):
                    with self.assertRaises(SGLException) as err:
                        func(case.name, case.sub)

                    self.assertEqual(err.exception.code,
                                     SGL_PARSER_INVALID_NAME_FORMAT)
Esempio n. 8
0
    def __gen_test_inputs(self, test_case):
        manager = _OptionManager(self._APP_NAME)

        configs = defaultdict(dict)
        envs = []
        args = []

        for category, values in test_case.items():
            for name, value in values.items():
                # Environment value injection
                if value.is_env_choosable:
                    key = manager.env_name(category, name)
                    os.environ[key] = str(value.e_val)
                    envs.append(key)

                if value.is_file_choosable:
                    configs[category].update({name: value.f_val})

                if value.is_user_choosable:
                    args.append('{}={}'.format(
                        manager.long_arg(category, name), value.u_val))

        del manager

        return utils.config_file(configs), envs, args
Esempio n. 9
0
    def test_abnormal_action(self):
        manager = _OptionManager(self._APP_NAME)
        parser = argparse.ArgumentParser()

        category = self._gen_random_string()
        name = self._gen_random_string()

        # 1. Try invalid manager
        with self.assertRaises(SGLException) as err:
            parser.add_argument(self._gen_random_string(),
                                action=_FileEnvAction,
                                manager=None,
                                category=category,
                                name=name)

        self.assertEqual(err.exception.code, SGL_PARSER_UNEXPECTED_MANAGER)

        # 2. Use invalid nargs
        for nargs in ['*', '?', '+', argparse.REMAINDER, 2]:
            opt_strings = [
                self._gen_random_string(),
                self._to_arg_name(self._gen_random_string())
            ]

            for opt in opt_strings:
                with self.assertRaises(SGLException) as err:
                    parser.add_argument(opt,
                                        action=_FileEnvAction,
                                        manager=manager,
                                        category=category,
                                        name=name,
                                        nargs=nargs)

                self.assertEqual(err.exception.code,
                                 SGL_PARSER_INVALID_PARSING_ARG)

        # 3. Use const option
        for _, item in self._gen_random_inputs(1).popitem()[-1].items():
            opt_strings = [
                self._gen_random_string(),
                self._to_arg_name(self._gen_random_string())
            ]

            for opt in opt_strings:
                with self.assertRaises(SGLException) as err:
                    parser.add_argument(opt,
                                        action=_FileEnvAction,
                                        manager=manager,
                                        category=category,
                                        name=name,
                                        const=item.u_val)

                self.assertEqual(err.exception.code,
                                 SGL_PARSER_INVALID_PARSING_ARG)
Esempio n. 10
0
    def test_valid_naming(self):
        manager = _OptionManager(self._APP_NAME)
        test_case = {
            self._gen_random_string(): self._gen_random_string()
            for _ in range(self.__TEST_COUNT)
        }

        for k, v in test_case.items():
            self.assertEqual(manager.env_name(k, v), self._to_env_name(k, v))
            self.assertEqual(manager.long_arg(k, v), self._to_arg_name(k, v))
            self.assertEqual(manager.dest_name(k, v), self._to_dest_name(k, v))
Esempio n. 11
0
    def test_user_value_action(self):
        manager = _OptionManager(self._APP_NAME)

        test_case = self._gen_random_inputs(self.__TEST_COUNT, True)

        args = [
            '{}={}'.format(manager.long_arg(category, name), value.u_val)
            for category, values in test_case.items()
            for name, value in values.items()
        ]

        self.__check_all_values(manager, test_case, lambda value: value.u_val,
                                args)
Esempio n. 12
0
    def test_file_value_action(self):
        manager = _OptionManager(self._APP_NAME)

        test_case = self._gen_random_inputs(self.__TEST_COUNT, False)

        configs = {
            category: {name: value.f_val
                       for name, value in values.items()}
            for category, values in test_case.items()
        }

        with utils.config_file(configs) as temp_file:
            manager.load(temp_file)

            self.__check_all_values(manager, test_case,
                                    lambda value: value.f_val)