# -*- coding: utf-8 -*-
# 测试用例
# 作者: 陈磊
# 时间: 2019-11-07


from __future__ import division

from WorkCase import CaseBase
from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsDataBase import UtilsDataBase
from WorkUtils.UtilsResponse import UtilsResponse
from WorkApi.API.HostGroup.hostGroup_create import HostGroupCreate
from WorkApi.API.HostGroup.hostGroup_delete import HostGroupDelete

from WorkData.Zabbix.hstgrp import DataHstGrp

import allure


@allure.feature("测试模块：hostgroup.delete")
class TestHostGroupDelete(object):
    log = UtilsLog()
    env = CaseBase().environment_test
    host = env["host_zabbix"]
    db_url = env["db_url_zabbix"]
    db_port = env["db_port_zabbix"]
    db_user = env["db_user_zabbix"]
    db_pw = env["db_pw_zabbix"]
    db_base = env["db_base_zabbix"]
    base_name = "SS自动化测试主机组"

    @classmethod
    def setup_class(cls):
        cls.log.debug("开始执行测试套件.......")
        session = UtilsDataBase().conn_mysql(db_url=cls.db_url, db_port=cls.db_port, db_base=cls.db_base, db_user=cls.db_user, db_pw=cls.db_pw)
        DataHstGrp().delete_like_name(session=session, name=cls.base_name)

    @classmethod
    def teardown_class(cls):
        cls.log.debug("结束执行测试套件.......")

    def setup_method(self):
        self.log.debug("测试用例执行开始...")

    def teardown_method(self):
        self.log.debug("测试用例执行结束...")

    @allure.step("调用接口：hostgroup.create")
    def hostgroup_create(self, name=None, groupid=None, flags=None, internal=None):
        api = HostGroupCreate(_host=self.host)
        api.name = name
        api.groupid = groupid
        api.flags = flags
        api.internal = internal
        api.get_response()
        return api.response

    @allure.step("调用接口：hostgroup.delete")
    def hostgroup_delete(self, groupids=None):
        api = HostGroupDelete(_host=self.host)
        api.groupids = groupids
        api.get_response()
        return api.response

    @allure.step("创建测试数据")
    def case_create(self, name):
        response = self.hostgroup_create(name=name)
        self.check_code(response=response, code=0)
        groupid = UtilsResponse().get_result(response=response)["groupids"][0]
        return groupid

    @allure.step("连接数据库：zabbix")
    def db_session(self):
        session = UtilsDataBase().conn_mysql(db_url=self.db_url, db_port=self.db_port, db_base=self.db_base, db_user=self.db_user, db_pw=self.db_pw)
        return session

    @allure.step("查询表：hstgrp")
    def select_hstgrp(self, groupid=None, name=None, internal=None, flags=None):
        session = self.db_session()
        sql = DataHstGrp().select_all_from_allKeys(session=session, groupid=groupid, name=name, internal=internal, flags=flags)
        return sql

    @allure.step("校验查询结果")
    def check_select(self, groupid):
        sql = self.select_hstgrp(groupid=groupid)
        assert sql == []

    @allure.step("断言返回结果：校验排序")
    def check_sortfield(self, response, possible):
        result = UtilsResponse().get_result(response=response)
        UtilsResponse().check_sort(_list=result, key="groupid", possible=possible)

    @allure.step("断言返回结果：校验返回数据的数量")
    def check_num(self, response, num):
        result = UtilsResponse().get_result(response=response)
        assert len(result) == num

    @allure.step("断言返回结果")
    def check_code(self, response, code):
        _code = UtilsResponse().get_code(response=response)
        assert _code == code

    @allure.step("断言返回结果")
    def check_msg(self, response, msg):
        _msg = UtilsResponse().get_msg(response=response)
        assert _msg == msg

    @allure.title("hostgroup.delete：删除的ID存在")
    @allure.story("删除主机组：删除的ID存在")
    @allure.severity("blocker")
    def test_case_01(self):
        name = self.base_name + "01"
        groupid = self.case_create(name=name)
        response = self.hostgroup_delete(groupids=[groupid])
        self.check_code(response=response, code=0)
        self.check_select(groupid=groupid)

    @allure.title("hostgroup.delete：删除的ID不存在 100")
    @allure.story("删除主机组：删除的ID不存在 100")
    def test_case_02(self):
        name = self.base_name + "02"
        groupid = self.case_create(name=name)
        response = self.hostgroup_delete(groupids=[groupid])
        self.check_code(response=response, code=0)
        self.check_select(groupid=groupid)

        response = self.hostgroup_delete(groupids=[groupid])
        self.check_code(response=response, code=100)
        self.check_msg(response=response, msg="No permissions to referred object or it does not exist!")

    @allure.title("hostgroup.delete：删除多个ID")
    @allure.story("删除主机组：删除多个ID")
    def test_case_03(self):
        name_1 = self.base_name + "031"
        name_2 = self.base_name + "032"
        name_3 = self.base_name + "033"
        groupid_1 = self.case_create(name=name_1)
        groupid_2 = self.case_create(name=name_2)
        groupid_3 = self.case_create(name=name_3)
        response = self.hostgroup_delete(groupids=[groupid_1, groupid_2, groupid_3])
        self.check_code(response=response, code=0)
        self.check_select(groupid=groupid_1)
        self.check_select(groupid=groupid_2)
        self.check_select(groupid=groupid_3)


if __name__ == "__main__":
    from WorkUtils.UtilsPyTest import UtilsPyTest
    from WorkUtils.UtilsCmd import UtilsCmd

    import os

    # 执行自动化测试用例
    case_info = os.path.split(__file__)
    case = UtilsCmd().pytest_cmd()
    r = UtilsPyTest(case=case, case_info=case_info)
    r.run_main()

    # a = TestHostGroupDelete()
    # a.setup_class()
    # a.test_case_03()
