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


from __future__ import division

from WorkCase import CaseBase
from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsResponse import UtilsResponse
from WorkUtils.UtilsDataBase import UtilsDataBase
from WorkApi.APP.Experience.experience_create import ExperienceCreate

from WorkData.Argus.experience import DataExperience


import allure


@allure.feature("测试模块：experience.create")
class TestExperienceCreate(object):
    log = UtilsLog()
    env = CaseBase().app_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_title = "SS测试经验标题"
    base_description = "SS测试经验描述"
    base_content = "SS测试经验内容"
    base_businessName = "SS测试经验业务名称"
    base_monitorName = "SS测试经验监控名称"
    base_hostid = 10266

    @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)
        DataExperience().delete_like_title(session=session, title=cls.base_title)

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

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

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

    @allure.step("调用接口：experience.create")
    def experience_create(self, title=None, content=None, businessid=None, businessName=None, hostType=None, hostid=None,
                          versions=None, monitorid=None, monitorName=None, alarmRuleids=None, description=None, tagNameList=None):
        api = ExperienceCreate(_host=self.host)
        api.title = title
        api.content = content
        api.businessid = businessid
        api.businessName = businessName
        api.hostType = hostType
        api.hostid = hostid
        api.versions = versions
        api.monitorid = monitorid
        api.monitorName = monitorName
        api.alarmRuleids = alarmRuleids
        api.description = description
        api.tagNameList = tagNameList
        api.get_response()
        return api.response

    @allure.step("断言返回结果")
    def check_code(self, response, code):
        _code = UtilsResponse().get_code(response=response)
        assert _code == code

    @allure.step("连接数据库：Argus")
    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("查询表：experience")
    def select_experience(self, experienceid=None, title=None, description=None):
        session = self.db_session()
        sql = DataExperience().select_all_from_allKeys(session=session, experienceid=experienceid, title=title, description=description)
        return sql

    @allure.step("校验查询结果")
    def check_select(self, experienceid, title, content, businessid, business_name, host_type, hostid, versions,
                     monitorid, monitor_name, description, eventid):
        sql = self.select_experience(experienceid=experienceid)
        for x, y in enumerate(sql):
            assert y.title == title
            assert y.content == content
            assert y.businessid == businessid
            assert y.business_name == business_name
            assert y.host_type == host_type
            assert y.hostid == hostid
            assert y.versions == versions
            assert y.monitorid == monitorid
            assert y.monitor_name == monitor_name
            assert y.description == description
            assert y.eventid == eventid

    @allure.title("experience.create：必填项")
    @allure.story("创建经验手册：必填项")
    @allure.severity("blocker")
    def test_case_01(self):
        title = self.base_title + "01"
        description = self.base_description + "01"
        response = self.experience_create(title=title, description=description, hostType=1)
        self.check_code(response=response, code=0)
        experienceid = UtilsResponse().get_data(response=response)
        self.check_select(experienceid=experienceid, title=title, description=description, host_type=1, content=None,
                          businessid=None, business_name=None, hostid=None, versions=1, monitorid=None, monitor_name=None,
                          eventid=None)

    @allure.title("experience.create：模糊查询hostName")
    @allure.story("创建经验手册：模糊查询hostName")
    def test_case_02(self):
        title = self.base_title + "02"
        description = self.base_description + "02"
        content = self.base_content + "02"
        business_name = self.base_businessName + "02"
        monitor_name = self.base_monitorName + "02"
        response = self.experience_create(title=title, description=description, hostType=1, content=content, businessid=1,
                                          businessName=business_name, monitorid=1, monitorName=monitor_name, hostid=self.base_hostid)
        self.check_code(response=response, code=0)
        experienceid = UtilsResponse().get_data(response=response)
        self.check_select(experienceid=experienceid, title=title, description=description, host_type=1, content=content,
                          businessid=1, business_name=business_name, hostid=self.base_hostid, versions=1,
                          monitorid=1, monitor_name=monitor_name, eventid=None)

    @allure.title("experience.create：查询assetCode")
    @allure.story("创建经验手册：查询assetCode")
    def test_case_03(self):
        host_name = self.base_host_name + "03"
        asset_code = self.base_asset_code + "03"
        manage_ip = self.base_manage_ip + "3"
        parent_host = self.base_parent_host + "03"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=1)
        response = self.networkmonitor_get(assetCode=self.base_asset_code)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="assetCode", value=self.base_asset_code, is_default=0)

    @allure.title("experience.create：模糊查询manageIp")
    @allure.story("创建经验手册：模糊查询manageIp")
    def test_case_04(self):
        host_name = self.base_host_name + "04"
        asset_code = self.base_asset_code + "04"
        manage_ip = self.base_manage_ip + "4"
        parent_host = self.base_parent_host + "04"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=1)
        response = self.networkmonitor_get(manageIp=self.base_manage_ip)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="manageIp", value=self.base_manage_ip, is_default=0)

    @allure.title("experience.create：模糊查询parentHost")
    @allure.story("创建经验手册：模糊查询parentHost")
    def test_case_05(self):
        host_name = self.base_host_name + "05"
        asset_code = self.base_asset_code + "05"
        manage_ip = self.base_manage_ip + "5"
        parent_host = self.base_parent_host + "05"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=1)
        response = self.networkmonitor_get(parentHost=self.base_parent_host)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="parentHost", value=self.base_parent_host, is_default=0)

    @allure.title("experience.create：必填项")
    @allure.story("创建经验手册：必填项")
    def test_case_06(self):
        host_name = self.base_host_name + "06"
        asset_code = self.base_asset_code + "06"
        manage_ip = self.base_manage_ip + "6"
        parent_host = self.base_parent_host + "06"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=0, monitor_status=1)
        response = self.networkmonitor_get(available=0)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="available", value=0, is_default=1)

    @allure.title("experience.create：模糊查询monitor_status")
    @allure.story("创建经验手册：模糊查询monitor_status")
    def test_case_07(self):
        host_name = self.base_host_name + "07"
        asset_code = self.base_asset_code + "07"
        manage_ip = self.base_manage_ip + "7"
        parent_host = self.base_parent_host + "07"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=0)
        response = self.networkmonitor_get(status=0)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="monitorStatus", value=0, is_default=1)


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 = TestExperienceCreate()
    a.setup_class()
    a.test_case_02()
