Skip to content

Commit

Permalink
✨ feat: 新增用例操作逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wieszheng committed Sep 17, 2024
1 parent 1e32a8c commit 9bd245b
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 29 deletions.
18 changes: 9 additions & 9 deletions app/apis/v1/testcase/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,28 @@
'/constructor',
endpoint=ConstructorService.get_constructor,
methods=['get'],
summary='',
summary='获取数据构造器',
)

router.add_api_route(
'/constructors',
endpoint=ConstructorService.get_constructor_list,
methods=['get'],
summary='',
summary='获取数据构造器全部',
)

router.add_api_route(
'/constructor/order',
endpoint=ConstructorService.update_constructor_order,
methods=['put'],
summary='',
summary='修改前后置构造器顺序',
)

router.add_api_route(
'/constructor/tree',
endpoint=ConstructorService.get_constructor_tree,
methods=['get'],
summary='',
summary='获取数据构造器树',
)

router.add_api_route(
Expand Down Expand Up @@ -156,7 +156,7 @@
'/directory',
endpoint=TestcaseDirectoryService.get_directory,
methods=['get'],
summary='',
summary='获取case目录',
)

router.add_api_route(
Expand All @@ -167,7 +167,7 @@
)

router.add_api_route(
'/directory',
'/directory/query',
endpoint=TestcaseDirectoryService.get_directory,
methods=['get'],
summary='',
Expand All @@ -177,21 +177,21 @@
'/directory',
endpoint=TestcaseDirectoryService.create_directory,
methods=['post'],
summary='',
summary='新增case目录',
)

router.add_api_route(
'/directory',
endpoint=TestcaseDirectoryService.update_directory,
methods=['put'],
summary='',
summary='修改case目录',
)

router.add_api_route(
'/directory',
endpoint=TestcaseDirectoryService.delete_directory,
methods=['delete'],
summary='',
summary='删除case目录',
)

router.add_api_route(
Expand Down
3 changes: 3 additions & 0 deletions app/commons/response/response_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ class CustomErrorCode(CustomCodeBase):
CASE_CONSTRUCTOR_NOT_EXIST = (15006, '构造器不存在,请检查')
CASE_CONSTRUCTOR_NOT_EMPTY = (15007, '构造器不能为空')
CASE_CONSTRUCTOR_EXIST = (15008, '构造器信息已存在, 请检查')
CASE_DIRECTORY_NOT_EXIST = (15009, '目录不存在,请检查')
CASE_DIRECTORY_EXIST = (15009, '目录信息已存在, 请检查')
CASE_DIRECTORY_NOT_EMPTY = (15010, '目录不能为空')


class StandardResponseCode:
Expand Down
46 changes: 44 additions & 2 deletions app/crud/testcase/testcase_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""

from collections import defaultdict
from typing import Annotated

from app.crud import BaseCRUD
from app.models.testcase_directory import TestcaseDirectory
Expand All @@ -30,7 +29,7 @@ def get_sub_son(cls, parent_map: dict, son: list, result: list):
cls.get_sub_son(parent_map, sons, result)

@classmethod
async def get_directory_son(cls, directory_id: Annotated[int | None, ...]):
async def get_directory_son(cls, directory_id: int):
parent_map = defaultdict(list)
ans = [directory_id]
result = await cls.get_multi_by_cursor(
Expand All @@ -41,3 +40,46 @@ async def get_directory_son(cls, directory_id: Annotated[int | None, ...]):
son = parent_map.get(directory_id)
cls.get_sub_son(parent_map, son, ans)
return ans

@classmethod
async def get_directory_list(cls, project_id: int):
return await cls.get_multi_by_cursor(project_id=project_id, is_deleted=False)

@classmethod
async def get_directory(
cls,
ans_map: dict,
parent_map,
parent,
children,
case_map,
case_node=None,
move=False,
):
current = parent_map.get(parent)
if case_node is not None:
nodes, cs = await case_node(parent)
children.extend(nodes)
case_map.update(cs)
if current is None:
return
for c in current:
temp = ans_map.get(c)
if case_node is None:
child = list()
else:
child, cs = await case_node(temp.id)
case_map.update(cs)
children.append(
dict(
title=temp['name'],
key=temp['id'],
children=child,
label=temp['name'],
value=temp['id'],
disabled=len(child) == 0 and not move,
)
)
await cls.get_directory(
ans_map, parent_map, temp['id'], child, case_node, move=move
)
28 changes: 28 additions & 0 deletions app/schemas/testcase/testcase_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Version : Python 3.12
@Time : 2024/9/17 22:02
@Author : wiesZheng
@Software : PyCharm
"""

from typing import List

from pydantic import BaseModel


class TestcaseDirectoryParams(BaseModel):
name: str
project_id: int
parent: int = None


class UpdateTestcaseDirectoryParams(TestcaseDirectoryParams):
id: int


class MoveTestCaseParams(BaseModel):
project_id: int
id_list: List[int]
directory_id: int
91 changes: 81 additions & 10 deletions app/service/testcase/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
@Software : PyCharm
"""

from typing import Annotated
from collections import defaultdict
from typing import Annotated, List

from fastapi import Depends

Expand All @@ -18,7 +19,11 @@
from app.crud.testcase.testcase import TestCaseCRUD
from app.exceptions.errors import CustomException
from app.schemas.auth.user import CurrentUserInfo
from app.schemas.testcase.constructor import ConstructorParams, UpdateConstructorParams
from app.schemas.testcase.constructor import (
ConstructorParams,
UpdateConstructorParams,
ConstructorIndexParams,
)


class ConstructorService:
Expand Down Expand Up @@ -60,17 +65,83 @@ async def delete_constructor(id: Annotated[int, ...]) -> ResponseModel:
return await ResponseBase.success()

@staticmethod
async def get_constructor(id: int):
pass
async def get_constructor(id: Annotated[int, ...]) -> ResponseModel:
input_ = await ConstructorCRUD.exists(id=id, is_deleted=False)
if not input_:
raise CustomException(CustomErrorCode.CASE_CONSTRUCTOR_NOT_EXIST)
result = await ConstructorCRUD.get(id=id)
return await ResponseBase.success(result=result)

@staticmethod
async def get_constructor_list(id: int):
pass
async def get_constructor_list(
constructor_type: Annotated[int, ...], suffix: Annotated[bool, ...]
) -> ResponseModel:
ans = list()
constructors = defaultdict(list)
result_all = await ConstructorCRUD.get_all(
suffix=suffix, type=constructor_type, public=True, is_deleted=False
)
for item in result_all['data']:
constructors[item['case_id']].append({
'title': item['name'],
'key': f"constructor_{item['id']}",
'value': f"constructor_{item['id']}",
'isLeaf': True,
'constructor_json': item['constructor_json'],
})
if len(constructors.keys()) == 0:
return await ResponseBase.success(result={'data': []})
result_all = await TestCaseCRUD.get_all(
id__in=list(constructors.keys()), is_deleted=False
)
for item in result_all['data']:
ans.append({
'title': item['name'],
'key': f"caseId_{item['id']}",
'disabled': True,
'children': constructors[item['id']],
})
return await ResponseBase.success(result={'data': ans})

@staticmethod
async def update_constructor_order(id: int):
pass
async def update_constructor_order(
data: Annotated[List[ConstructorIndexParams], ...],
) -> ResponseModel:
for item in data:
await ConstructorCRUD.update(obj={'index': item.index}, id=item.id)
return await ResponseBase.success()

@staticmethod
async def get_constructor_tree(id: int):
pass
async def get_constructor_tree(
suffix: Annotated[bool, ...],
name: Annotated[str, ...] = '',
) -> ResponseModel:
result = await ConstructorCRUD.get_all(
suffix=suffix, public=True, is_deleted=False, name__startswith=name
)
if not result['data']:
return await ResponseBase.success(result={'data': []})
temp = defaultdict(list)
# 建立caseID -> constructor的map
for c in result['data']:
temp[c['case_id']].append(c)
result_all = await TestCaseCRUD.get_all(
id__in=list(temp.keys()), is_deleted=False
)
testcase_info = {t['id']: t for t in result_all['data']}
result = []
for k, v in temp.items():
result.append({
'title': testcase_info[k]['name'],
'key': f'caseId_{k}',
'disabled': True,
'children': [
{
'key': f"constructor_{item['id']}",
'title': item['name'],
'value': f"constructor_{item['id']}",
}
for item in v
],
})
return await ResponseBase.success(result={'data': result})
Loading

0 comments on commit 9bd245b

Please sign in to comment.