Esempio n. 1
0
    def test_handleConfigValue_isListFalse(self):
        """
        Tests that the code works for a basic case of is_list = False
        """
        val_in = '0.5'
        item = 'varname_in_cfg_file'
        default = None
        is_list = False
        convert_to_type = float
        can_be_unset = False
        allowed_values = None

        val_out = _handle_config_value(var=val_in,
                                       default=default,
                                       item=item,
                                       is_list=is_list,
                                       convert_to_type=convert_to_type,
                                       can_be_unset=can_be_unset,
                                       allowed_values=allowed_values)

        self.assertEqual(val_out, float(val_in))
Esempio n. 2
0
    def test_handleConfigValue_convertToTypeFail(self):
        """
        Tests the handling of an incorrectly entered list from a .cfg file
        """
        val = '1 2 3 x 5 6 7'
        item = 'varname_in_cfg_file'
        default = None
        is_list = True
        convert_to_type = float
        can_be_unset = False
        allowed_values = None
        errmsg = 'Wrong type for .cfg file variable: {}'.format(item)

        with self.assertRaisesRegex(SystemExit, errmsg):
            val = _handle_config_value(var=val,
                                       default=default,
                                       item=item,
                                       is_list=is_list,
                                       convert_to_type=convert_to_type,
                                       can_be_unset=can_be_unset,
                                       allowed_values=allowed_values)
Esempio n. 3
0
    def test_handleConfigValue_UnsetCantBeUnset(self):
        """
        Tests the handling of UNSET variable read in from a .cfg file
        for which can_be_unset = False
        """
        val = 'UNSET'
        item = 'varname_in_cfg_file'
        default = None
        is_list = False
        convert_to_type = None
        can_be_unset = False
        allowed_values = None
        errmsg = 'Must set a value for .cfg file variable: {}'.format(item)

        with self.assertRaisesRegex(SystemExit, errmsg):
            val = _handle_config_value(var=val,
                                       default=default,
                                       item=item,
                                       is_list=is_list,
                                       convert_to_type=convert_to_type,
                                       can_be_unset=can_be_unset,
                                       allowed_values=allowed_values)
Esempio n. 4
0
    def test_handleConfigValue_isListFail(self):
        """
        Tests that the code aborts if we forget to set is_list = True
        """
        val = 'True False'
        item = 'varname_in_cfg_file'
        default = None
        is_list = False
        convert_to_type = bool
        can_be_unset = False
        allowed_values = None
        errmsg = 'More than 1 element found for .cfg file variable: {}'.format(
            item)

        with self.assertRaisesRegex(SystemExit, errmsg):
            val = _handle_config_value(var=val,
                                       default=default,
                                       item=item,
                                       is_list=is_list,
                                       convert_to_type=convert_to_type,
                                       can_be_unset=can_be_unset,
                                       allowed_values=allowed_values)
Esempio n. 5
0
    def test_handleConfigValue_convertToTypePass(self):
        """
        Tests the handling of non-boolean list from a .cfg file
        """
        val = '-9 0.001'
        item = 'varname_in_cfg_file'
        default = None
        is_list = True
        convert_to_type = float
        can_be_unset = False
        allowed_values = None

        val = _handle_config_value(var=val,
                                   default=default,
                                   item=item,
                                   is_list=is_list,
                                   convert_to_type=convert_to_type,
                                   can_be_unset=can_be_unset,
                                   allowed_values=allowed_values)

        self.assertEqual(val[0], -9)
        self.assertEqual(val[1], 0.001)
Esempio n. 6
0
    def test_handleConfigValue_UnsetCanBeUnset(self):
        """
        Tests the handling of UNSET variable read in from a .cfg file
        for which can_be_unset = True
        """
        val = 'UNSET'
        item = 'varname_in_cfg_file'
        default = [True, False, True]
        is_list = True
        convert_to_type = None
        can_be_unset = True
        allowed_values = None

        val = _handle_config_value(var=val,
                                   default=default,
                                   item=item,
                                   is_list=is_list,
                                   convert_to_type=convert_to_type,
                                   can_be_unset=can_be_unset,
                                   allowed_values=allowed_values)

        self.assertEqual(val, default)
Esempio n. 7
0
    def test_handleConfigValue_convertToBoolPass(self):
        """
        Tests the handling of boolean read in from a .cfg file
        Also test whether the code can read a list of booleans
        """
        val = 'yes no'
        item = 'varname_in_cfg_file'
        default = None
        is_list = True
        convert_to_type = bool
        can_be_unset = False
        allowed_values = None

        val = _handle_config_value(var=val,
                                   default=default,
                                   item=item,
                                   is_list=is_list,
                                   convert_to_type=convert_to_type,
                                   can_be_unset=can_be_unset,
                                   allowed_values=allowed_values)

        self.assertTrue(val[0])
        self.assertFalse(val[1])
Esempio n. 8
0
    def test_handleConfigValue_convertToBoolFail(self):
        """
        Tests the handling of misspelled boolean read in from a .cfg file
        Also test whether the code can read a list of booleans
        """
        val = 'False Tree False'  # intentionally misspelled True
        item = 'varname_in_cfg_file'
        default = None
        is_list = True
        convert_to_type = bool
        can_be_unset = False
        allowed_values = None
        errmsg = 'Non-boolean value found for .cfg file variable: {}'.format(
            item)

        with self.assertRaisesRegex(SystemExit, errmsg):
            val = _handle_config_value(var=val,
                                       default=default,
                                       item=item,
                                       is_list=is_list,
                                       convert_to_type=convert_to_type,
                                       can_be_unset=can_be_unset,
                                       allowed_values=allowed_values)
Esempio n. 9
0
    def test_handleConfigValue_allowedValsFail(self):
        """
        Tests that the code aborts if val does not include all allowed_values
        """
        val = '1 2 3 4.5 6 7'
        item = 'varname_in_cfg_file'
        default = None
        is_list = True
        convert_to_type = float
        can_be_unset = False
        allowed_values = [1, 2, 3, 4, 5, 6, 7]
        v = 4.5  # v must equal the misstyped value in val
        errmsg = (
            '{} is not an allowed value for {} in .cfg file. Check allowed_values'
            .format(v, item))

        with self.assertRaisesRegex(SystemExit, errmsg):
            val = _handle_config_value(var=val,
                                       default=default,
                                       item=item,
                                       is_list=is_list,
                                       convert_to_type=convert_to_type,
                                       can_be_unset=can_be_unset,
                                       allowed_values=allowed_values)