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


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 WorkData.Zabbix.hstgrp import DataHstGrp

import allure


@allure.feature("测试模块：hostgroup.create")
class TestHostGroupCreate(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("连接数据库：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.step("断言返回错误结果")
    def check_status(self, response, status):
        _status = UtilsResponse().get_status(response=response)
        assert _status == status

    @allure.title("hostgroup.create：name=字符串")
    @allure.story("创建主机组：name=字符串")
    @allure.severity("blocker")
    def test_case_01(self):
        name = self.base_name + "01"
        flags = 0
        internal = 0
        response = self.hostgroup_create(name=name)
        self.check_code(response=response, code=0)
        groupid = UtilsResponse().get_result(response=response)["groupids"][0]
        self.check_select(groupid=groupid, name=name, flags=flags, internal=internal)

    @allure.title("hostgroup.create：重复创建")
    @allure.story("创建主机组：重复创建")
    def test_case_02(self):
        name = self.base_name + "02"
        flags = 0
        internal = 0
        response = self.hostgroup_create(name=name)
        self.check_code(response=response, code=0)
        groupid = UtilsResponse().get_result(response=response)["groupids"][0]
        self.check_select(groupid=groupid, name=name, flags=flags, internal=internal)

        response = self.hostgroup_create(name=name, flags=flags, internal=internal)
        self.check_code(response=response, code=100)

    @allure.title("hostgroup.create：无参数调用")
    @allure.story("创建主机组：无参数调用")
    def test_case_03(self):
        response = self.hostgroup_create()
        self.check_status(response=response, status=400)

    @allure.title("hostgroup.create：name=空字符串")
    @allure.story("创建主机组：name=空字符串")
    def test_case_04(self):
        response = self.hostgroup_create(name="")
        self.check_status(response=response, status=400)


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 = TestHostGroupCreate()
    # a.setup_class()
    # a.test_case_01()
