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


from __future__ import division

from WorkCase import CaseBase
from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsResponse import UtilsResponse
from WorkApi.API.Script.script_get import ScriptGet

import allure

# allure.label("")


@allure.feature("测试模块：script.get")
class TestScriptGet(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"]

    @classmethod
    def setup_class(cls):
        cls.log.debug("开始执行测试套件.......")

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

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

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

    @allure.step("调用接口：script.get")
    def script_get(self, groupids=None, hostids=None, scriptids=None, usrgrpids=None, selectGroups=None, selectHosts=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 = ScriptGet(_host=self.host)
        api.groupids = groupids
        api.hostids = hostids
        api.scriptids = scriptids
        api.usrgrpids = usrgrpids
        api.selectGroups = selectGroups
        api.selectHosts = selectHosts
        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("断言返回结果")
    def check_code(self, response, code):
        _code = UtilsResponse().get_code(response=response)
        assert _code == code

    @allure.title("script.get：无参数调用")
    @allure.story("查询脚本：无参数调用")
    @allure.severity("blocker")
    def test_case_01(self):
        response = self.script_get()
        self.check_code(response=response, code=0)

    @allure.title("script.get：countOutput=true")
    @allure.story("查询脚本：countOutput=true")
    def test_case_02(self):
        response = self.script_get(countOutput="true")
        self.check_code(response=response, code=0)

    @allure.title("script.get：scriptids=特定IDs")
    @allure.story("查询脚本：scriptids=特定IDs")
    def test_case_03(self):
        response = self.script_get(scriptids=[5])
        self.check_code(response=response, code=0)

    @allure.title("script.get：output=[name]")
    @allure.story("查询脚本：output=[name]")
    def test_case_04(self):
        response = self.script_get(output=["name"])
        self.check_code(response=response, code=0)

    @allure.title("script.get：groupids=特定IDs, output=[name, internal, flags]")
    @allure.story("查询脚本：groupids=特定IDs, output=[name, internal, flags]")
    def test_case_05(self):
        response = self.script_get(groupids=[5], output=["name", "internal", "flags"])
        self.check_code(response=response, code=0)
        self.check_sql(response=response, groupid=5)

    @allure.title("script.get：limit=1")
    @allure.story("查询脚本：limit=1")
    def test_case_06(self):
        response = self.script_get(limit=1)
        self.check_code(response=response, code=0)
        result = UtilsResponse().get_result(response=response)
        assert len(result) == 1

    @allure.title("script.get：精确搜索")
    @allure.story("查询脚本：精确搜索")
    def test_case_07(self):
        response = self.script_get(search={"name": ["Zabbix servers"]})
        self.check_code(response=response, code=0)
        result = UtilsResponse().get_result(response=response)
        assert len(result) == 1

    @allure.title("script.get：模糊搜索")
    @allure.story("查询脚本：模糊搜索")
    def test_case_08(self):
        response = self.script_get(search={"name": ["*Zabbix*"]}, searchByAny=True, searchWildcardsEnabled=True)
        self.check_code(response=response, code=0)
        result = UtilsResponse().get_result(response=response)
        assert len(result) == 1

    @allure.title("script.get：sortfield=groupid")
    @allure.story("查询脚本：sortfield=groupid")
    @allure.severity("blocker")
    def test_case_09(self):
        response = self.script_get(sortfield=["groupid"])
        self.check_code(response=response, code=0)
        self.check_sortfield(response=response, possible=True)

    @allure.title("script.get：sortfield=groupid, sortorder=ASC")
    @allure.story("查询脚本：sortfield=groupid, sortorder=ASC")
    @allure.severity("blocker")
    def test_case_10(self):
        response = self.script_get(sortfield=["groupid"], sortorder=["ASC"])
        self.check_code(response=response, code=0)
        self.check_sortfield(response=response, possible="ASC")

    @allure.title("script.get：sortfield=groupid, sortorder=DESC")
    @allure.story("查询脚本：sortfield=groupid, sortorder=DESC")
    @allure.severity("blocker")
    def test_case_11(self):
        response = self.script_get(sortfield=["groupid"], 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(version=case["version"])
    # r.run_main(case_info=case_info)

    a = TestScriptGet()
    a.setup_class()
    a.test_case_04()
    a.test_case_05()
