# -*- 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.History.history_get import HistoryGet

import allure

# allure.label("")


@allure.feature("测试模块：history.get")
class TestHistoryGet(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("调用接口：history.get")
    def history_get(self, history=None, hostids=None, itemids=None, time_from=None, time_till=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 = HistoryGet(_host=self.host)
        api.history = history
        api.hostids = hostids
        api.itemids = itemids
        api.time_from = time_from
        api.time_till = time_till
        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("history.get：无参数调用")
    @allure.story("查询历史：无参数调用")
    @allure.severity("blocker")
    def test_case_01(self):
        response = self.history_get()
        self.check_code(response=response, code=0)

    @allure.title("history.get：countOutput=true")
    @allure.story("查询历史：countOutput=true")
    def test_case_02(self):
        response = self.history_get(countOutput="true")
        self.check_code(response=response, code=0)

    @allure.title("history.get：history=0")
    @allure.story("查询历史：history=0")
    def test_case_03(self):
        response = self.history_get(history=0)
        self.check_code(response=response, code=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 = TestHistoryGet()
    # a.test_case_01()
