
# -*- 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_update import HostGroupUpdate

from WorkData.Zabbix.hstgrp import DataHstGrp

import allure

# allure.label("")


@allure.feature("测试模块：hostgroup.update")
class TestHostGroupUpdate(object):
    log = UtilsLog()
    env = CaseBase().environment
    host = env["host"]
    db_url = env["db_url"]
    db_port = env["db_port"]
    db_user = env["db_user"]
    db_pw = env["db_pw"]
    db_base = env["db_base"]
    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.update")
    def hostgroup_update(self, groupid=None, name=None, flags=None, internal=None):
        api = HostGroupUpdate(_host=self.host)
        api.groupid = groupid
        api.name = name
        api.flags = flags
        api.internal = internal
        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, name, internal, flags):
        sql = self.select_hstgrp(groupid=groupid)
        for x, y in enumerate(sql):
            assert y.groupid == groupid
            assert y.name == name
            assert y.flags == flags
            assert y.internal == internal

    @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.title("hostgroup.update：修改name")
    @allure.story("修改主机组：修改name")
    @allure.severity("blocker")
    def test_case_01(self):
        name = self.base_name + "01"
        new_name = self.base_name + "NEW01"
        groupid = self.case_create(name=name)
        response = self.hostgroup_update(groupid=groupid, name=new_name)
        self.check_code(response=response, code=0)
        self.check_select(groupid=groupid, name=new_name, internal=0, flags=0)


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 = TestHostGroupUpdate()
    # a.setup_class()
    # a.test_case_02()
