# -*- coding: utf-8 -*-
# @Time : 2022/7/21 14:36
# @Author : wangyinghao
# @Site : 
# @File : api_test_plan.py
# @Software: PyCharm
from django.db import models
from automated_main.models.api_automation.api_management import ApiManagement
from automated_main.models.api_automation.api_environment import APIEnvironment
from automated_main.models.api_automation.api_database import APIDatabase


class ApiTestPlan(models.Model):
    """
    API计划表
    """
    api_test_plan_name = models.CharField("API测试计划名称", max_length=100, blank=False, default="")
    api_test_plan_describe = models.TextField("描述", default="", null=True)
    status = models.IntegerField("状态：", default=0)  # 未执行、执行中、执行完成、排队中
    cases = models.TextField("关联API用例", default="")
    api_send_email = models.CharField("API邮件发送", max_length=9999, blank=True, default="", null=True)
    api_send_enterprise_wechat = models.IntegerField("API企业微信发送消息", blank=True, default="0")
    api_environment = models.ForeignKey(APIEnvironment, on_delete=models.CASCADE)
    timing_task_status = models.CharField("定时任务是否开启", max_length=50, blank=True, default="", null=True)
    starting_time = models.CharField("起始时间", max_length=50, blank=True, default="", null=True)
    end_time = models.CharField("截至时间", max_length=50, blank=True, default="", null=True)
    time_interval_day = models.IntegerField("间隔时间-天", blank=True, default=0, null=True)
    time_interval_minutes = models.IntegerField("间隔时间-分钟", blank=True, default=0, null=True)
    time_interval_hours = models.IntegerField("间隔时间-小时", blank=True, default=0, null=True)
    time_interval_seconds = models.IntegerField("间隔时间-秒", blank=True, default=0, null=True)
    database = models.ForeignKey(APIDatabase, on_delete=models.CASCADE)
    api_management = models.ForeignKey(ApiManagement, on_delete=models.CASCADE)
    update_time = models.DateTimeField(auto_now_add=True)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)

    def __str__(self):
        return self.api_test_plan_name


class APITestPlanResult(models.Model):
    """
    API测试计划结果表
    """
    api_test_result_name = models.CharField("api测试报告名称", max_length=200, blank=False, default="")
    api_test_plan = models.ForeignKey(ApiTestPlan, on_delete=models.CASCADE)
    api_error_total_number = models.CharField("失败总数", max_length=100, null=True, blank=True)
    api_successful_total_number = models.CharField("成功总数", max_length=100, null=True, blank=True)
    api_total_number = models.CharField("API总数", max_length=100, null=True, blank=True)
    api_environment_id = models.CharField("API环境", max_length=100, null=True, blank=True)
    update_time = models.DateTimeField(auto_now_add=True)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)

    def __str__(self):
        return self.api_test_result_name


class APITestPlanResultAssociated(models.Model):
    """
    API测试结果关联表
    """
    api_test_case_name = models.CharField("api测试用例名称", max_length=1000, blank=False, default="")
    api_test_plan = models.ForeignKey(ApiTestPlan, on_delete=models.CASCADE)
    api_result = models.ForeignKey(APITestPlanResult, on_delete=models.CASCADE)
    api_error = models.CharField("失败", max_length=100, null=True, blank=True)
    api_successful = models.CharField("成功", max_length=100, null=True, blank=True)
    abnormal = models.TextField("异常", max_length=50000, null=True, blank=True)
    json_extract_variable_conversion = models.TextField("json提取变量转换",
                                                        max_length=5000, null=True, blank=True)
    api_assertion_results = models.TextField("断言结果", max_length=50000, null=True, blank=True)
    api_variable_results = models.TextField("参数提取", max_length=50000, null=True, blank=True)
    api_request_results = models.TextField("API请求结果", max_length=500000, null=True, default=None)
    api_business_test_name = models.TextField("API业务测试名称", max_length=50000, null=True, blank=True)
    api_header = models.TextField("header", max_length=50000, null=True, blank=True)
    api_url = models.TextField("请求地址", max_length=50000, null=True, blank=True)
    api_body = models.TextField("body", max_length=500000, null=True, blank=True)
    update_time = models.DateTimeField(auto_now_add=True)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)

    def __str__(self):
        return self.api_test_case_name
