def test_get_result_key_not_found(self):
        '''Test validations_read_ini when key is not found'''

        tmpfile = self.create_tmp_ini()
        tmp_name = os.path.relpath(tmpfile.name)
        tmpfile.write(valid_content.encode('utf-8'))
        tmpfile.seek(0)
        ret, msg, value = validation.get_result(tmp_name, 'section', 'key')
        tmpfile.close()

        self.assertEqual(validation.ReturnValue.KEY_NOT_FOUND, ret)
        self.assertEqual(("There is no key 'key' under the section 'section' "
                          "in file {}.").format(tmp_name), msg)
        self.assertIsNone(value)
    def test_get_result_invalid_format(self):
        '''Test validations_read_ini when file format is valid'''

        tmpfile = self.create_tmp_ini()
        tmp_name = os.path.relpath(tmpfile.name)
        tmpfile.write(invalid_content.encode('utf-8'))
        tmpfile.seek(0)
        ret, msg, value = validation.get_result(tmp_name, 'section', 'key')
        tmpfile.close()

        self.assertEqual(validation.ReturnValue.INVALID_FORMAT, ret)
        self.assertEqual(
            "The file '{}' is not in a valid INI format.".format(tmp_name),
            msg)
        self.assertIsNone(value)
    def test_get_result_ok(self):
        '''Test validations_read_ini when key is not found'''

        tmpfile = self.create_tmp_ini()
        tmp_name = os.path.relpath(tmpfile.name)
        tmpfile.write(valid_content.encode('utf-8'))
        tmpfile.seek(0)
        ret, msg, value = validation.get_result(tmp_name, 'secrets',
                                                'password')
        tmpfile.close()

        self.assertEqual(validation.ReturnValue.OK, ret)
        self.assertEqual(
            ("The key 'password' under the section 'secrets'"
             " in file {} has the value: '1234'").format(tmp_name), msg)
        self.assertEqual('1234', value)
    def test_get_result_key_not_found_with_default(self):
        '''Test validations_read_ini when key is not found but has a default'''

        tmpfile = self.create_tmp_ini()
        tmp_name = os.path.relpath(tmpfile.name)
        tmpfile.write(valid_content.encode('utf-8'))
        tmpfile.seek(0)
        ret, msg, value = validation.get_result(tmp_name, 'section', 'key',
                                                'foo')
        tmpfile.close()

        self.assertEqual(validation.ReturnValue.OK, ret)
        self.assertEqual(
            ("There is no key 'key' under section 'section' "
             "in file {}. Using default value '{}'").format(tmp_name,
                                                            'foo'), msg)
        self.assertEqual(value, 'foo')
Пример #5
0
    def test_get_result_invalid_format(self):
        '''Test validations_read_ini when file format is valid'''

        tmpfile = self.create_tmp_ini()
        tmp_name = os.path.relpath(tmpfile.name)
        tmpfile.write(invalid_content.encode('utf-8'))
        tmpfile.seek(0)
        ret, msg, value = validation.get_result(tmp_name, 'section', 'key')
        tmpfile.close()

        self.assertEqual(validation.ReturnValue.INVALID_FORMAT, ret)
        asserted = ("The file '{path}' is not in a valid INI format: File "
                    "contains no section headers.\nfile: '{path}', line: 2\n"
                    "'[DEFAULT#\\n\'").format(path=tmp_name)

        self.assertEqual(asserted, msg)
        self.assertIsNone(value)