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


from __future__ import division

from WorkCase import CaseBase
from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsResponse import UtilsResponse
from WorkApi.API.UserGroup.userGroup_get import UserGroupGet

import allure

# allure.label("")


@allure.feature("测试模块：usergroup.get")
class TestUserGroupGet(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("调用接口：usergroup.get")
    def usergroup_get(self, status=None, userids=None, usrgrpids=None, with_gui_access=None, selectTagFilters=None,
                      selectUsers=None, selectRights=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 = UserGroupGet(_host=self.host)
        api.status = status
        api.userids = userids
        api.usrgrpids = usrgrpids
        api.with_gui_access = with_gui_access
        api.selectTagFilters = selectTagFilters
        api.selectUsers = selectUsers
        api.selectRights = selectRights
        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("usergroup.get：无参数调用")
    @allure.story("查询主机组：无参数调用")
    @allure.severity("blocker")
    def test_case_01(self):
        response = self.usergroup_get()
        self.check_code(response=response, code=0)

    @allure.title("usergroup.get：countOutput=true")
    @allure.story("查询主机组：countOutput=true")
    def test_case_02(self):
        response = self.usergroup_get(countOutput="true")
        self.check_code(response=response, code=0)

    @allure.title("usergroup.get：usrgrpids=特定IDs")
    @allure.story("查询主机组：usrgrpids=特定IDs")
    def test_case_03(self):
        response = self.usergroup_get(usrgrpids=[22])
        self.check_code(response=response, code=0)

    @allure.title("hostgroup.get：output=[name]")
    @allure.story("查询主机组：output=[name]")
    def test_case_04(self):
        response = self.usergroup_get(output=["name"])
        self.check_code(response=response, code=0)

    @allure.title("hostgroup.get：groupids=特定IDs, output=[name, internal, flags]")
    @allure.story("查询主机组：groupids=特定IDs, output=[name, internal, flags]")
    def test_case_05(self):
        response = self.hostgroup_get(groupids=[5], output=["name", "internal", "flags"])
        self.check_code(response=response, code=0)
        self.check_sql(response=response, groupid=5)

    @allure.title("hostgroup.get：limit=1")
    @allure.story("查询主机组：limit=1")
    def test_case_06(self):
        response = self.hostgroup_get(limit=1)
        self.check_code(response=response, code=0)
        result = UtilsResponse().get_result(response=response)
        assert len(result) == 1

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

    @allure.title("hostgroup.get：模糊搜索")
    @allure.story("查询主机组：模糊搜索")
    def test_case_08(self):
        response = self.hostgroup_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("hostgroup.get：sortfield=groupid")
    @allure.story("查询主机组：sortfield=groupid")
    @allure.severity("blocker")
    def test_case_09(self):
        response = self.hostgroup_get(sortfield=["groupid"])
        self.check_code(response=response, code=0)
        self.check_sortfield(response=response, possible=True)

    @allure.title("hostgroup.get：sortfield=groupid, sortorder=ASC")
    @allure.story("查询主机组：sortfield=groupid, sortorder=ASC")
    @allure.severity("blocker")
    def test_case_10(self):
        response = self.hostgroup_get(sortfield=["groupid"], sortorder=["ASC"])
        self.check_code(response=response, code=0)
        self.check_sortfield(response=response, possible="ASC")

    @allure.title("hostgroup.get：sortfield=groupid, sortorder=DESC")
    @allure.story("查询主机组：sortfield=groupid, sortorder=DESC")
    @allure.severity("blocker")
    def test_case_11(self):
        response = self.hostgroup_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 = TestUserGroupGet()
    # a.test_case_01()
    a.test_case_04()
    a.test_case_05()
