refactor(测试计划): 测试计划关联用例优化

This commit is contained in:
WangXu10 2024-07-17 21:00:18 +08:00 committed by Craftsman
parent bb8833b03c
commit 0e06314dbb
11 changed files with 192 additions and 127 deletions

View File

@ -86,5 +86,8 @@ public interface ExtApiScenarioMapper {
List<ApiScenario> selectAllCase(@Param("isRepeat") boolean isRepeat, @Param("projectId") String projectId, @Param("testPlanId") String testPlanId);
List<ApiScenario> selectCaseByModules(@Param("isRepeat") boolean isRepeat, @Param("projectId") String projectId, @Param("dto") AssociateCaseDTO dto, @Param("testPlanId") String testPlanId);
List<ApiScenario> getListBySelectModules(@Param("projectId") String projectId, @Param("moduleIds") List<String> moduleIds, @Param("testPlanId") String testPlanId);
List<ApiScenario> getListBySelectIds(@Param("projectId") String projectId, @Param("ids") List<String> ids, @Param("testPlanId") String testPlanId);
}

View File

@ -694,38 +694,36 @@
order by api_scenario.pos asc
</select>
<select id="selectCaseByModules" resultType="io.metersphere.api.domain.ApiScenario">
<select id="getListBySelectModules" resultType="io.metersphere.api.domain.ApiScenario">
select
api_scenario.id,
api_scenario.create_user,
api_scenario.environment_id,
api_scenario.grouped
api_scenario.grouped,
api_scenario.pos
from api_scenario
where api_scenario.deleted = false
and api_scenario.project_id = #{projectId}
<if test="!isRepeat">
AND api_scenario.id not in (
select test_plan_api_scenario.api_scenario_id from test_plan_api_scenario where test_plan_api_scenario.test_plan_id = #{testPlanId}
)
</if>
<if test="dto.moduleIds != null and dto.moduleIds.size() > 0">
and api_scenario.module_id in
<foreach collection="dto.moduleIds" item="moduleId" open="(" separator="," close=")">
<foreach collection="moduleIds" item="moduleId" open="(" separator="," close=")">
#{moduleId}
</foreach>
</if>
<if test="dto.excludeIds != null and dto.excludeIds.size() > 0">
AND api_scenario.id not in
<foreach collection="dto.excludeIds" item="excludeId" separator="," open="(" close=")">
#{excludeId}
</foreach>
</if>
<if test="dto.selectIds != null and dto.selectIds.size() > 0">
or api_scenario.id in
<foreach collection="dto.selectIds" item="selectId" separator="," open="(" close=")">
#{selectId}
</foreach>
</if>
order by api_scenario.pos asc
</select>
<select id="getListBySelectIds" resultType="io.metersphere.api.domain.ApiScenario">
select
api_scenario.id,
api_scenario.create_user,
api_scenario.environment_id,
api_scenario.grouped,
api_scenario.pos
from api_scenario
where api_scenario.deleted = false
and api_scenario.project_id = #{projectId}
and api_scenario.id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
</mapper>

View File

@ -105,7 +105,9 @@ public interface ExtApiTestCaseMapper {
List<ApiTestCase> selectAllApiCase(@Param("isRepeat") boolean isRepeat, @Param("projectId") String projectId, @Param("testPlanId") String testPlanId);
List<ApiTestCase> selectCaseByModules(@Param("isRepeat") boolean isRepeat, @Param("projectId") String projectId, @Param("dto") AssociateCaseDTO dto, @Param("testPlanId") String testPlanId);
List<ApiTestCase> getListBySelectModules(@Param("projectId") String projectId, @Param("moduleIds") List<String> moduleIds, @Param("testPlanId") String testPlanId);
List<ApiTestCase> selectCaseByApiModules(@Param("isRepeat") boolean isRepeat, @Param("projectId") String projectId, @Param("dto") AssociateCaseDTO dto, @Param("testPlanId") String testPlanId);
List<ApiTestCase> getListBySelectIds(@Param("projectId") String projectId, @Param("ids") List<String> ids, @Param("testPlanId") String testPlanId);
List<ApiTestCase> getCaseListBySelectIds(@Param("projectId") String projectId, @Param("ids") List<String> ids, @Param("testPlanId") String testPlanId);
}

View File

@ -634,75 +634,55 @@
order by api_test_case.pos asc
</select>
<select id="selectCaseByModules" resultType="io.metersphere.api.domain.ApiTestCase">
<select id="getListBySelectModules" resultType="io.metersphere.api.domain.ApiTestCase">
select
api_test_case.id,
api_test_case.create_user,
api_test_case.environment_id
api_test_case.environment_id,
api_test_case.pos
from api_test_case
inner join api_definition on api_definition.id = api_test_case.api_definition_id
where api_test_case.deleted = false
and api_test_case.project_id = #{projectId}
<if test="!isRepeat">
AND api_test_case.id not in (
select test_plan_api_case.api_case_id from test_plan_api_case where test_plan_api_case.test_plan_id = #{testPlanId}
)
</if>
<if test="dto.moduleIds != null and dto.moduleIds.size() > 0">
and api_definition.module_id in
<foreach collection="dto.moduleIds" item="moduleId" open="(" separator="," close=")">
<foreach collection="moduleIds" item="moduleId" open="(" separator="," close=")">
#{moduleId}
</foreach>
</if>
<if test="dto.excludeIds != null and dto.excludeIds.size() > 0">
AND api_test_case.id not in
<foreach collection="dto.excludeIds" item="excludeId" separator="," open="(" close=")">
#{excludeId}
</foreach>
</if>
<if test="dto.selectIds != null and dto.selectIds.size() > 0">
or api_test_case.id in
<foreach collection="dto.selectIds" item="selectId" separator="," open="(" close=")">
#{selectId}
</foreach>
</if>
order by api_test_case.pos asc
</select>
<select id="selectCaseByApiModules" resultType="io.metersphere.api.domain.ApiTestCase">
<select id="getListBySelectIds" resultType="io.metersphere.api.domain.ApiTestCase">
select
api_test_case.id,
api_test_case.create_user,
api_test_case.environment_id
api_test_case.environment_id,
api_test_case.pos
from api_test_case
inner join api_definition on api_definition.id = api_test_case.api_definition_id
where api_test_case.deleted = false
and api_test_case.project_id = #{projectId}
<if test="!isRepeat">
AND api_test_case.id not in (
select test_plan_api_case.api_case_id from test_plan_api_case where test_plan_api_case.test_plan_id =
#{testPlanId}
)
</if>
<if test="dto.moduleIds != null and dto.moduleIds.size() > 0">
and api_definition.module_id in
<foreach collection="dto.moduleIds" item="moduleId" open="(" separator="," close=")">
#{moduleId}
AND api_test_case.id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
<if test="dto.excludeIds != null and dto.excludeIds.size() > 0">
AND api_definition.id not in
<foreach collection="dto.excludeIds" item="excludeId" separator="," open="(" close=")">
#{excludeId}
</foreach>
</if>
<if test="dto.selectIds != null and dto.selectIds.size() > 0">
or api_definition.id in
<foreach collection="dto.selectIds" item="selectId" separator="," open="(" close=")">
#{selectId}
</foreach>
</if>
order by api_test_case.pos asc
</select>
<select id="getCaseListBySelectIds" resultType="io.metersphere.api.domain.ApiTestCase">
select
api_test_case.id,
api_test_case.create_user,
api_test_case.environment_id,
api_test_case.pos
from api_test_case
inner join api_definition on api_definition.id = api_test_case.api_definition_id
where api_test_case.deleted = false
and api_test_case.project_id = #{projectId}
and api_definition.id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
</mapper>

View File

@ -11,7 +11,6 @@ import io.metersphere.functional.request.*;
import io.metersphere.project.dto.ModuleCountDTO;
import io.metersphere.request.AssociateOtherCaseRequest;
import io.metersphere.request.TestCasePageProviderRequest;
import io.metersphere.sdk.dto.AssociateCaseDTO;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import org.apache.ibatis.annotations.Param;
@ -102,5 +101,7 @@ public interface ExtFunctionalCaseMapper {
List<FunctionalCase> selectAllFunctionalCase(@Param("isRepeat") boolean isRepeat, @Param("projectId") String projectId, @Param("testPlanId") String testPlanId);
List<FunctionalCase> selectCaseByModules(@Param("isRepeat") boolean isRepeat, @Param("projectId") String projectId, @Param("dto") AssociateCaseDTO dto, @Param("testPlanId") String testPlanId);
List<FunctionalCase> getListBySelectModules(@Param("projectId") String projectId, @Param("moduleIds") List<String> moduleIds, @Param("testPlanId") String testPlanId);
List<FunctionalCase> getListBySelectIds(@Param("projectId") String projectId, @Param("ids") List<String> ids, @Param("testPlanId") String testPlanId);
}

View File

@ -918,36 +918,32 @@
order by functional_case.pos asc
</select>
<select id="selectCaseByModules" resultType="io.metersphere.functional.domain.FunctionalCase">
<select id="getListBySelectModules" resultType="io.metersphere.functional.domain.FunctionalCase">
select
functional_case.id,
functional_case.create_user
functional_case.create_user,
functional_case.pos
from functional_case
where functional_case.deleted = false
and functional_case.project_id = #{projectId}
<if test="!isRepeat">
AND functional_case.id not in (
select test_plan_functional_case.functional_case_id from test_plan_functional_case where test_plan_functional_case.test_plan_id = #{testPlanId}
)
</if>
<if test="dto.moduleIds != null and dto.moduleIds.size() > 0">
and functional_case.module_id in
<foreach collection="dto.moduleIds" item="moduleId" open="(" separator="," close=")">
<foreach collection="moduleIds" item="moduleId" open="(" separator="," close=")">
#{moduleId}
</foreach>
</if>
<if test="dto.excludeIds != null and dto.excludeIds.size() > 0">
AND functional_case.id not in
<foreach collection="dto.excludeIds" item="excludeId" separator="," open="(" close=")">
#{excludeId}
</select>
<select id="getListBySelectIds" resultType="io.metersphere.functional.domain.FunctionalCase">
select
functional_case.id,
functional_case.create_user,
functional_case.pos
from functional_case
where functional_case.deleted = false
and functional_case.project_id = #{projectId}
and functional_case.id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
<if test="dto.selectIds != null and dto.selectIds.size() > 0">
or functional_case.id in
<foreach collection="dto.selectIds" item="selectId" separator="," open="(" close=")">
#{selectId}
</foreach>
</if>
order by functional_case.pos asc
</select>
</mapper>

View File

@ -595,8 +595,30 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
buildTestPlanApiCaseDTO(apiCase, apiTestCaseList, testPlan, user, testPlanApiCaseList);
} else {
AssociateCaseDTO dto = super.getCaseIds(moduleMaps);
List<ApiTestCase> apiTestCaseList = extApiTestCaseMapper.selectCaseByModules(isRepeat, apiCase.getModules().getProjectId(), dto, testPlan.getId());
buildTestPlanApiCaseDTO(apiCase, apiTestCaseList, testPlan, user, testPlanApiCaseList);
List<ApiTestCase> apiTestCaseList = new ArrayList<>();
//获取全选的模块数据
if (CollectionUtils.isNotEmpty(dto.getModuleIds())) {
apiTestCaseList = extApiTestCaseMapper.getListBySelectModules(apiCase.getModules().getProjectId(), dto.getModuleIds(), testPlan.getId());
}
if (CollectionUtils.isNotEmpty(dto.getSelectIds())) {
CollectionUtils.removeAll(dto.getSelectIds(), apiTestCaseList.stream().map(ApiTestCase::getId).toList());
//获取选中的ids数据
List<ApiTestCase> selectIdList = extApiTestCaseMapper.getListBySelectIds(apiCase.getModules().getProjectId(), dto.getSelectIds(), testPlan.getId());
apiTestCaseList.addAll(selectIdList);
}
if (CollectionUtils.isNotEmpty(dto.getExcludeIds())) {
//排除的ids
List<String> excludeIds = dto.getExcludeIds();
apiTestCaseList = apiTestCaseList.stream().filter(item -> !excludeIds.contains(item.getId())).toList();
}
if (CollectionUtils.isNotEmpty(apiTestCaseList)) {
List<ApiTestCase> list = apiTestCaseList.stream().sorted(Comparator.comparing(ApiTestCase::getPos).reversed()).toList();
buildTestPlanApiCaseDTO(apiCase, list, testPlan, user, testPlanApiCaseList);
}
}
});
}
@ -615,8 +637,30 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
buildTestPlanApiCaseDTO(apiCase, apiTestCaseList, testPlan, user, testPlanApiCaseList);
} else {
AssociateCaseDTO dto = super.getCaseIds(moduleMaps);
List<ApiTestCase> apiTestCaseList = extApiTestCaseMapper.selectCaseByApiModules(isRepeat, apiCase.getModules().getProjectId(), dto, testPlan.getId());
buildTestPlanApiCaseDTO(apiCase, apiTestCaseList, testPlan, user, testPlanApiCaseList);
List<ApiTestCase> apiTestCaseList = new ArrayList<>();
//获取全选的模块数据
if (CollectionUtils.isNotEmpty(dto.getModuleIds())) {
apiTestCaseList = extApiTestCaseMapper.getListBySelectModules(apiCase.getModules().getProjectId(), dto.getModuleIds(), testPlan.getId());
}
if (CollectionUtils.isNotEmpty(dto.getSelectIds())) {
CollectionUtils.removeAll(dto.getSelectIds(), apiTestCaseList.stream().map(ApiTestCase::getId).toList());
//获取选中的ids数据
List<ApiTestCase> selectIdList = extApiTestCaseMapper.getCaseListBySelectIds(apiCase.getModules().getProjectId(), dto.getSelectIds(), testPlan.getId());
apiTestCaseList.addAll(selectIdList);
}
if (CollectionUtils.isNotEmpty(dto.getExcludeIds())) {
//排除的ids
List<String> excludeIds = dto.getExcludeIds();
apiTestCaseList = apiTestCaseList.stream().filter(item -> !excludeIds.contains(item.getId())).toList();
}
if (CollectionUtils.isNotEmpty(apiTestCaseList)) {
List<ApiTestCase> list = apiTestCaseList.stream().sorted(Comparator.comparing(ApiTestCase::getPos).reversed()).toList();
buildTestPlanApiCaseDTO(apiCase, list, testPlan, user, testPlanApiCaseList);
}
}
});
}

View File

@ -57,10 +57,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -206,8 +203,30 @@ public class TestPlanApiScenarioService extends TestPlanResourceService {
buildTestPlanApiScenarioDTO(apiScenario, scenarioList, testPlan, user, testPlanApiScenarioList);
} else {
AssociateCaseDTO dto = super.getCaseIds(moduleMaps);
List<ApiScenario> scenarioList = extApiScenarioMapper.selectCaseByModules(isRepeat, apiScenario.getModules().getProjectId(), dto, testPlan.getId());
buildTestPlanApiScenarioDTO(apiScenario, scenarioList, testPlan, user, testPlanApiScenarioList);
List<ApiScenario> scenarioList = new ArrayList<>();
//获取全选的模块数据
if (CollectionUtils.isNotEmpty(dto.getModuleIds())) {
scenarioList = extApiScenarioMapper.getListBySelectModules(apiScenario.getModules().getProjectId(), dto.getModuleIds(), testPlan.getId());
}
if (CollectionUtils.isNotEmpty(dto.getSelectIds())) {
CollectionUtils.removeAll(dto.getSelectIds(), scenarioList.stream().map(ApiScenario::getId).toList());
//获取选中的ids数据
List<ApiScenario> selectIdList = extApiScenarioMapper.getListBySelectIds(apiScenario.getModules().getProjectId(), dto.getSelectIds(), testPlan.getId());
scenarioList.addAll(selectIdList);
}
if (CollectionUtils.isNotEmpty(dto.getExcludeIds())) {
//排除的ids
List<String> excludeIds = dto.getExcludeIds();
scenarioList = scenarioList.stream().filter(item -> !excludeIds.contains(item.getId())).toList();
}
if (CollectionUtils.isNotEmpty(scenarioList)) {
List<ApiScenario> list = scenarioList.stream().sorted(Comparator.comparing(ApiScenario::getPos).reversed()).toList();
buildTestPlanApiScenarioDTO(apiScenario, list, testPlan, user, testPlanApiScenarioList);
}
}
});
}

View File

@ -807,10 +807,32 @@ public class TestPlanFunctionalCaseService extends TestPlanResourceService {
handleSyncCase(functionalCaseList, functional, testPlan, user);
} else {
AssociateCaseDTO dto = super.getCaseIds(moduleMaps);
List<FunctionalCase> functionalCaseList = extFunctionalCaseMapper.selectCaseByModules(isRepeat, functional.getModules().getProjectId(), dto, testPlan.getId());
buildTestPlanFunctionalCaseDTO(functional, functionalCaseList, testPlan, user, testPlanFunctionalCaseList);
List<FunctionalCase> functionalCaseList = new ArrayList<>();
//获取全选的模块数据
if (CollectionUtils.isNotEmpty(dto.getModuleIds())) {
functionalCaseList = extFunctionalCaseMapper.getListBySelectModules(functional.getModules().getProjectId(), dto.getModuleIds(), testPlan.getId());
}
if (CollectionUtils.isNotEmpty(dto.getSelectIds())) {
CollectionUtils.removeAll(dto.getSelectIds(), functionalCaseList.stream().map(FunctionalCase::getId).toList());
//获取选中的ids数据
List<FunctionalCase> selectIdList = extFunctionalCaseMapper.getListBySelectIds(functional.getModules().getProjectId(), dto.getSelectIds(), testPlan.getId());
functionalCaseList.addAll(selectIdList);
}
if (CollectionUtils.isNotEmpty(dto.getExcludeIds())) {
//排除的ids
List<String> excludeIds = dto.getExcludeIds();
functionalCaseList = functionalCaseList.stream().filter(item -> !excludeIds.contains(item.getId())).toList();
}
if (CollectionUtils.isNotEmpty(functionalCaseList)) {
List<FunctionalCase> list = functionalCaseList.stream().sorted(Comparator.comparing(FunctionalCase::getPos).reversed()).toList();
buildTestPlanFunctionalCaseDTO(functional, list, testPlan, user, testPlanFunctionalCaseList);
handleSyncCase(functionalCaseList, functional, testPlan, user);
}
}
}

View File

@ -115,9 +115,9 @@ public abstract class TestPlanResourceService extends TestPlanSortService {
.flatMap(moduleSelectDTO -> moduleSelectDTO.getExcludeIds().stream())
.toList();
// 选中的ids
List<String> selectIds = moduleMaps.entrySet().stream()
.filter(entry -> BooleanUtils.isFalse(entry.getValue().isSelectAll()) && CollectionUtils.isNotEmpty(entry.getValue().getSelectIds()))
.map(Map.Entry::getKey)
List<String> selectIds = moduleMaps.values().stream()
.filter(moduleSelectDTO -> BooleanUtils.isFalse(moduleSelectDTO.isSelectAll()) && CollectionUtils.isNotEmpty(moduleSelectDTO.getSelectIds()))
.flatMap(moduleSelectDTO -> moduleSelectDTO.getSelectIds().stream())
.toList();
// 全选的模块
List<String> moduleIds = moduleMaps.entrySet().stream()

View File

@ -280,7 +280,7 @@ public class TestPlanApiCaseControllerTests extends BaseTest {
collectionAssociates1.put(AssociateCaseType.API_CASE, baseCollectionAssociateRequests1);
testPlanApiCaseService.associateCollection("wxxx_1", collectionAssociates1, user);
baseCollectionAssociateRequest1.setModules(buildModules(AssociateCaseType.API));
baseCollectionAssociateRequest1.setModules(buildModules(AssociateCaseType.API_CASE));
testPlanApiCaseService.associateCollection("wxxx_1", collectionAssociates1, user);
apiTestCase = initApiData();