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


from __future__ import division
from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsDataBase import UtilsDataBase
from WorkUtils.UtilsResponse import UtilsResponse
from WorkApi.API.HostInterface.hostInterface_create import HostInterfaceCreate
from WorkApi.API.HostInterface.hostInterface_get import HostInterfaceGet

from WorkData.Zabbix.interface import DataInterface

from WorkCase import CaseBase

import allure

# allure.label("")


@allure.feature("测试模块：hostinterface.get")
class TestHostInterfaceGet(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测试主机组"
    base_ip = "10.0.0.28"
    base_hostid = 10266
    base_dns = "sanshi.zmops.cn"
    base_port = "127"

    @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)
        DataInterface().delete_hostid(session=session, hostid=cls.base_hostid)

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

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

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

    @allure.step("调用接口：hostinterface.create")
    def hostinterface_create(self, dns=None, hostid=None, ip=None, main=None, port=None, _type=None, useip=None, bulk=None):
        api = HostInterfaceCreate(_host=self.host)
        api.dns = dns
        api.hostid = hostid
        api.ip = ip
        api.main = main
        api.port = port
        api.type = _type
        api.useip = useip
        api.bulk = bulk
        api.get_response()
        return api.response

    @allure.step("创建测试数据")
    def case_create(self, ip, hostid, main, port, _type, useip, bulk, dns):
        response = self.hostinterface_create(ip=ip, hostid=hostid, main=main, port=port, _type=_type, useip=useip, bulk=bulk, dns=dns)
        self.check_code(response=response, code=0)
        interfaceid = UtilsResponse().get_result(response=response)["interfaceids"][0]
        return interfaceid

    @allure.step("调用接口：hostinterface.get")
    def hostinterface_get(self, hostids=None, interfaceids=None, itemids=None, triggerids=None, selectItems=None,
                          selectHosts=None, limitSelects=None, countOutput=None, editable=None, excludeSearch=None,
                          _filter=None, limit=None, output=None, preservekeys=None, search=None, searchByAny=None,
                          searchWildcardsEnabled=None, sortfield=None, sortorder=None, startSearch=None):
        api = HostInterfaceGet(_host=self.host)
        api.hostids = hostids
        api.interfaceids = interfaceids
        api.itemids = itemids
        api.triggerids = triggerids
        api.selectItems = selectItems
        api.selectHosts = selectHosts
        api.limitSelects = limitSelects
        api.countOutput = countOutput
        api.editable = editable
        api.excludeSearch = excludeSearch
        api.filter = _filter
        api.limit = limit
        api.output = output
        api.preservekeys = preservekeys
        api.search = search
        api.searchByAny = searchByAny
        api.searchWildcardsEnabled = searchWildcardsEnabled
        api.sortfield = sortfield
        api.sortorder = sortorder
        api.startSearch = startSearch
        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("查询表：interface")
    def select_interface(self, interfaceid=None):
        session = self.db_session()
        sql = DataInterface().select_all_from_allKeys(session=session, interfaceid=interfaceid)
        return sql

    @allure.step("断言返回结果：校验排序")
    def check_sortfield(self, response, possible):
        result = UtilsResponse().get_result(response=response)
        UtilsResponse().check_sort(_list=result, key="interfaceid", 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_sql(self, response, interfaceid):
        sql = self.select_interface(interfaceid=interfaceid)
        result = UtilsResponse().get_result(response=response)
        for x, y in enumerate(sql):
            assert y.interfaceid in UtilsResponse().get_values_from_key(_dict=result, key="interfaceid")
            assert y.type in UtilsResponse().get_values_from_key(_dict=result, key="type")
            assert y.ip in UtilsResponse().get_values_from_key(_dict=result, key="ip")
            assert y.port in UtilsResponse().get_values_from_key(_dict=result, key="port")
            assert y.main in UtilsResponse().get_values_from_key(_dict=result, key="main")
            assert y.useip in UtilsResponse().get_values_from_key(_dict=result, key="useip")
            assert y.bulk in UtilsResponse().get_values_from_key(_dict=result, key="bulk")
        for x, y in enumerate(result):
            assert y["interfaceid"] in UtilsResponse().get_values_from_sql(_sql=sql, key="interfaceid")
            assert y["type"] in UtilsResponse().get_values_from_sql(_sql=sql, key="type")
            assert y["ip"] in UtilsResponse().get_values_from_sql(_sql=sql, key="ip")
            assert y["port"] in UtilsResponse().get_values_from_sql(_sql=sql, key="port")
            assert y["main"] in UtilsResponse().get_values_from_sql(_sql=sql, key="main")
            assert y["useip"] in UtilsResponse().get_values_from_sql(_sql=sql, key="useip")
            assert y["bulk"] in UtilsResponse().get_values_from_sql(_sql=sql, key="bulk")

    @allure.title("hostinterface.get：无参数调用")
    @allure.story("查询主机接口：无参数调用")
    @allure.severity("blocker")
    def test_case_01(self):
        response = self.hostinterface_get()
        self.check_code(response=response, code=0)

    @allure.title("hostinterface.get：countOutput=true")
    @allure.story("查询主机接口：countOutput=true")
    def test_case_02(self):
        response = self.hostinterface_get(countOutput="true")
        self.check_code(response=response, code=0)

    @allure.title("hostinterface.get：hostids=特定IDs")
    @allure.story("查询主机接口：hostids=特定IDs")
    def test_case_03(self):
        response = self.hostinterface_get(hostids=[self.base_hostid])
        self.check_code(response=response, code=0)

    @allure.title("hostinterface.get：output=[ip]")
    @allure.story("查询主机接口：output=[ip]")
    def test_case_04(self):
        response = self.hostinterface_get(output=["ip"])
        self.check_code(response=response, code=0)

    @allure.title("hostinterface.get：interfaceids=特定IDs, output=[ip, port, type, main, useip, bulk]")
    @allure.story("查询主机接口：interfaceids=特定IDs, output=[ip, port, type, main, useip, bulk]")
    def test_case_05(self):
        interfaceid = self.case_create(ip=self.base_ip, dns="", hostid=self.base_hostid, main=1, port=self.base_port, _type=1, useip=1, bulk=1)

        response = self.hostinterface_get(interfaceids=[interfaceid], output=["ip", "port", "type", "main", "useip", "bulk"])
        self.check_code(response=response, code=0)
        self.check_sql(response=response, interfaceid=interfaceid)

    @allure.title("hostinterface.get：limit=1")
    @allure.story("查询主机接口：limit=1")
    def test_case_06(self):
        self.case_create(ip=self.base_ip, dns="", hostid=self.base_hostid, main=1, port=self.base_port, _type=2, useip=1, bulk=1)

        response = self.hostinterface_get(limit=1)
        self.check_code(response=response, code=0)
        result = UtilsResponse().get_result(response=response)
        assert len(result) == 1

    @allure.title("hostinterface.get：sortfield=interfaceid")
    @allure.story("查询主机接口：sortfield=interfaceid")
    def test_case_07(self):
        response = self.hostinterface_get(sortfield=["interfaceid"])
        self.check_code(response=response, code=0)
        self.check_sortfield(response=response, possible=True)

    @allure.title("hostinterface.get：sortfield=interfaceid, sortorder=ASC")
    @allure.story("查询主机接口：sortfield=interfaceid, sortorder=ASC")
    def test_case_08(self):
        response = self.hostinterface_get(sortfield=["interfaceid"], sortorder=["ASC"])
        self.check_code(response=response, code=0)
        self.check_sortfield(response=response, possible="ASC")

    @allure.title("hostinterface.get：sortfield=interfaceid, sortorder=DESC")
    @allure.story("查询主机接口：sortfield=interfaceid, sortorder=DESC")
    def test_case_09(self):
        response = self.hostinterface_get(sortfield=["interfaceid"], sortorder=["DESC"])
        self.check_code(response=response, code=0)
        self.check_sortfield(response=response, possible="DESC")


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 = TestHostInterfaceGet()
    # a.setup_class()
    # a.test_case_05()
