forked from wuranxu/pity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wuranxu#3 from wuranxu/feature/test_report
Feature/test report
- Loading branch information
Showing
8 changed files
with
298 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from datetime import datetime | ||
|
||
from sqlalchemy import select | ||
|
||
from app.models import async_session | ||
from app.models.report import PityReport | ||
from app.utils.logger import Log | ||
|
||
|
||
class TestReportDao(object): | ||
log = Log("TestReportDao") | ||
|
||
@staticmethod | ||
async def start(executor: int, env: int, mode: int = 0) -> int: | ||
""" | ||
生成buildId,开始执行任务,任务完成后通过回调方法更新报告 | ||
:return: 返回report_id | ||
""" | ||
try: | ||
async with async_session() as session: | ||
async with session.begin(): | ||
report = PityReport(executor, env, mode=mode) | ||
session.add(report) | ||
await session.flush() | ||
return report.id | ||
except Exception as e: | ||
TestReportDao.log.error(f"新增报告失败, error: {e}") | ||
raise Exception("新增报告失败") | ||
|
||
@staticmethod | ||
async def end(report_id: int, success_count: int, failed_count: int, | ||
error_count: int, skipped_count: int, status: int) -> None: | ||
try: | ||
async with async_session() as session: | ||
async with session.begin(): | ||
sql = select(PityReport).where(PityReport.id == report_id) | ||
data = await session.execute(sql) | ||
report = data.scalars().first() | ||
if report is None: | ||
raise Exception("获取报告失败") | ||
report.status = status | ||
report.success_count = success_count | ||
report.failed_count = failed_count | ||
report.error_count = error_count | ||
report.skipped_count = skipped_count | ||
report.finished_at = datetime.now() | ||
await session.flush() | ||
except Exception as e: | ||
TestReportDao.log.error(f"更新报告失败, error: {e}") | ||
raise Exception("更新报告失败") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from datetime import datetime | ||
|
||
from app.models import async_session | ||
from app.models.result import PityTestResult | ||
from app.utils.logger import Log | ||
|
||
|
||
class TestResultDao(object): | ||
log = Log("TestResultDao") | ||
|
||
@staticmethod | ||
async def insert(report_id: int, case_id: int, status: int, | ||
case_log: str, start_at: datetime, finished_at: datetime, | ||
url: str, body: str, request_method: str, cost: str, | ||
asserts: str, response_headers: str, response: str, | ||
status_code: int, cookies: str, retry: int = None, ) -> None: | ||
try: | ||
async with async_session() as session: | ||
async with session.begin(): | ||
report = PityTestResult(report_id, case_id, status, | ||
case_log, start_at, finished_at, | ||
url, body, request_method, cost, | ||
asserts, response_headers, response, | ||
status_code, cookies, retry) | ||
session.add(report) | ||
await session.flush() | ||
except Exception as e: | ||
TestResultDao.log.error(f"新增测试结果失败, error: {e}") | ||
raise Exception("新增测试结果失败") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from datetime import datetime | ||
|
||
from sqlalchemy import INT, Column, DATETIME | ||
from sqlalchemy.dialects.mysql import SMALLINT | ||
|
||
from app.models import Base | ||
|
||
|
||
class PityReport(Base): | ||
__tablename__ = 'pity_report' | ||
id = Column(INT, primary_key=True) | ||
# 执行人 0则为CPU | ||
executor = Column(INT, index=True) | ||
|
||
# 环境 | ||
env = Column(INT, nullable=False) | ||
|
||
# 测试集合id,预留字段 | ||
plan_id = Column(INT, index=True, nullable=True) | ||
# 开始时间 | ||
start_at = Column(DATETIME, nullable=False) | ||
# 结束时间 | ||
finished_at = Column(DATETIME) | ||
# 成功数量 | ||
success_count = Column(INT, nullable=False, default=0) | ||
error_count = Column(INT, nullable=False, default=0) | ||
failed_count = Column(INT, nullable=False, default=0) | ||
skipped_count = Column(INT, nullable=False, default=0) | ||
|
||
# 执行状态 | ||
status = Column(SMALLINT, nullable=False, comment="0: pending, 1: running, 2: stopped, 3: finished", index=True) | ||
|
||
# case执行模式 | ||
mode = Column(SMALLINT, default=0, comment="0: 手动, 1: 自动, 2: 测试集, 3: pipeline, 4: 其他") | ||
|
||
deleted_at = Column(DATETIME, index=True) | ||
|
||
def __init__(self, executor: int, env: int, success_count: int = 0, failed_count: int = 0, | ||
error_count: int = 0, skipped_count: int = 0, status: int = 0, mode: int = 0, | ||
plan_id: int = None, finished_at: datetime = None): | ||
self.executor = executor | ||
self.env = env | ||
self.start_at = datetime.now() | ||
self.success_count = success_count | ||
self.failed_count = failed_count | ||
self.error_count = error_count | ||
self.skipped_count = skipped_count | ||
self.status = status | ||
self.mode = mode | ||
self.status = status | ||
self.plan_id = plan_id | ||
self.finished_at = finished_at | ||
self.deleted_at = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from datetime import datetime | ||
|
||
from sqlalchemy import INT, Column, DATETIME, String | ||
from sqlalchemy import SMALLINT | ||
from sqlalchemy import TEXT | ||
|
||
from app.models import Base | ||
|
||
|
||
class PityTestResult(Base): | ||
__tablename__ = 'pity_test_result' | ||
id = Column(INT, primary_key=True) | ||
|
||
# 报告id | ||
report_id = Column(INT, index=True) | ||
|
||
# case_id | ||
case_id = Column(INT, index=True) | ||
|
||
status = Column(SMALLINT, comment="对应状态 0: 成功 1: 失败 2: 出错 3: 跳过") | ||
|
||
# 开始时间 | ||
start_at = Column(DATETIME, nullable=False) | ||
# 结束时间 | ||
finished_at = Column(DATETIME, nullable=False) | ||
|
||
case_log = Column(TEXT) | ||
|
||
# 重试次数,预留字段 | ||
retry = Column(INT, default=0) | ||
|
||
# http状态码 | ||
status_code = Column(INT) | ||
|
||
url = Column(TEXT, nullable=False) | ||
|
||
body = Column(TEXT) | ||
|
||
request_method = Column(String(12), nullable=True) | ||
|
||
cost = Column(String(12), nullable=False) | ||
|
||
asserts = Column(TEXT) | ||
|
||
response_headers = Column(TEXT) | ||
|
||
response = Column(TEXT) | ||
|
||
cookies = Column(TEXT) | ||
|
||
deleted_at = Column(DATETIME, index=True) | ||
|
||
def __init__(self, report_id: int, case_id: int, status: int, | ||
case_log: str, start_at: datetime, finished_at: datetime, | ||
url: str, body: str, request_method: str, cost: str, | ||
asserts: str, response_headers: str, response: str, | ||
status_code: int, cookies: str, retry: int = None, | ||
): | ||
self.report_id = report_id | ||
self.case_id = case_id | ||
self.status = status | ||
self.case_log = case_log | ||
self.start_at = start_at | ||
self.finished_at = finished_at | ||
self.status = status | ||
self.retry = retry | ||
self.status_code = status_code | ||
self.url = url | ||
self.request_method = request_method | ||
self.body = body | ||
self.cost = cost | ||
self.response = response | ||
self.response_headers = response_headers | ||
self.asserts = asserts | ||
self.cookies = cookies | ||
self.deleted_at = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters