Skip to content

Commit 1ad06ea

Browse files
committed
fix: add unit test
1 parent 9e54151 commit 1ad06ea

File tree

3 files changed

+90
-9
lines changed

3 files changed

+90
-9
lines changed

modules/dop/dao/testplan_v2_step.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (client *DBClient) MoveTestPlanV2Step(req *apistructs.TestPlanV2StepMoveReq
197197
oldGroupID = step.GroupID
198198

199199
// the order of the linked list has not changed
200-
if req.PreID == step.PreID || req.PreID == step.ID {
200+
if req.PreID == step.PreID || req.PreID == lastStepIDInGroup {
201201
if req.IsGroup {
202202
return nil
203203
}
@@ -258,7 +258,7 @@ func (client *DBClient) MoveTestPlanV2Step(req *apistructs.TestPlanV2StepMoveReq
258258
})
259259
}
260260

261-
// updateStepGroup update step group, set min SetID in the group as groupID
261+
// updateStepGroup update step group, set min setID in the group as groupID
262262
func updateStepGroup(tx *gorm.DB, groupIDs ...uint64) error {
263263
for _, v := range groupIDs {
264264
if v == 0 {

modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/stages.go

+24-7
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (i *ComponentStageForm) RenderGroupMoveStagesForm() (err error) {
187187
if len(stepDragGroup) <= 0 {
188188
return errors.New("the dragGroupKey is not exists")
189189
}
190-
firstStepDrag, lastStepDrag := findFirstLastStepIDInGroup(stepDragGroup)
190+
firstStepDrag, lastStepDrag := findFirstLastStepInGroup(stepDragGroup)
191191
req.StepID = firstStepDrag.ID
192192
req.LastStepID = lastStepDrag.ID
193193

@@ -202,9 +202,10 @@ func (i *ComponentStageForm) RenderGroupMoveStagesForm() (err error) {
202202
if len(stepDropGroup) <= 0 {
203203
return errors.New("the dropGroupKey is not exists")
204204
}
205-
firstStepDrop, _ := findFirstLastStepIDInGroup(stepDropGroup)
205+
firstStepDrop, _ := findFirstLastStepInGroup(stepDropGroup)
206206
req.PreID = firstStepDrop.PreID
207207
req.TargetStepID = firstStepDrop.ID
208+
// the order of the linked list has not changed
208209
if req.PreID == req.LastStepID {
209210
return nil
210211
}
@@ -216,10 +217,11 @@ func (i *ComponentStageForm) RenderGroupMoveStagesForm() (err error) {
216217
if len(stepDropGroup) <= 0 {
217218
return errors.New("the dropGroupKey is not exists")
218219
}
219-
_, lastStepDrop := findFirstLastStepIDInGroup(stepDropGroup)
220+
_, lastStepDrop := findFirstLastStepInGroup(stepDropGroup)
220221
req.PreID = lastStepDrop.ID
221222
req.TargetStepID = lastStepDrop.ID
222-
if req.PreID == req.StepID {
223+
// the order of the linked list has not changed
224+
if req.PreID == firstStepDrag.PreID {
223225
return nil
224226
}
225227
default:
@@ -228,7 +230,7 @@ func (i *ComponentStageForm) RenderGroupMoveStagesForm() (err error) {
228230
return i.ctxBdl.Bdl.MoveTestPlansV2Step(req)
229231
}
230232

231-
func findFirstLastStepIDInGroup(steps []*apistructs.TestPlanV2Step) (firstStep, lastStep *apistructs.TestPlanV2Step) {
233+
func findFirstLastStepInGroup(steps []*apistructs.TestPlanV2Step) (firstStep, lastStep *apistructs.TestPlanV2Step) {
232234
stepIDMap := make(map[uint64]*apistructs.TestPlanV2Step, len(steps))
233235
preIDMap := make(map[uint64]*apistructs.TestPlanV2Step, len(steps))
234236
for _, v := range steps {
@@ -256,12 +258,15 @@ func (i *ComponentStageForm) RenderItemMoveStagesForm() (err error) {
256258
req apistructs.TestPlanV2StepMoveRequest
257259
testPlan *apistructs.TestPlanV2GetResponse
258260
)
261+
dragGroupKey := uint64(i.State.DragParams.DragGroupKey)
262+
dropGroupKey := uint64(i.State.DragParams.DropGroupKey)
263+
259264
req.UserID = i.ctxBdl.Identity.UserID
260265
req.TestPlanID = i.State.TestPlanId
261266
req.StepID = uint64(i.State.DragParams.DragKey)
262267
req.LastStepID = uint64(i.State.DragParams.DragKey)
263268
req.IsGroup = false
264-
if i.State.DragParams.DropKey == -1 {
269+
if i.State.DragParams.DropKey == -1 { // move to the end and be independent group
265270
req.TargetStepID = 0
266271
} else {
267272
req.TargetStepID = uint64(i.State.DragParams.DropKey)
@@ -284,12 +289,21 @@ func (i *ComponentStageForm) RenderItemMoveStagesForm() (err error) {
284289
return
285290
}
286291
req.PreID = uint64(i.State.DragParams.DropKey)
292+
// the order of the linked list has not changed in the same group
293+
if req.PreID == step.PreID && dragGroupKey == dropGroupKey {
294+
return nil
295+
}
296+
287297
case -1: // in front of the target
288298
step, err = i.ctxBdl.Bdl.GetTestPlanV2Step(uint64(i.State.DragParams.DropKey))
289299
if err != nil {
290300
return
291301
}
292302
req.PreID = step.PreID
303+
// the order of the linked list has not changed in the same group
304+
if req.PreID == req.LastStepID && dragGroupKey == dropGroupKey {
305+
return nil
306+
}
293307
default:
294308
return errors.New("unknown position")
295309
}
@@ -319,7 +333,10 @@ func (i *ComponentStageForm) RenderSplitStagesForm(opsData interface{}) (err err
319333
if len(stepGroup) <= 0 {
320334
return errors.New("the groupID is not exists")
321335
}
322-
_, lastStep := findFirstLastStepIDInGroup(stepGroup)
336+
if len(stepGroup) == 1 {
337+
return nil
338+
}
339+
_, lastStep := findFirstLastStepInGroup(stepGroup)
323340
req.PreID = lastStep.ID
324341
return i.ctxBdl.Bdl.MoveTestPlansV2Step(req)
325342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2021 Terminus, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package stages
16+
17+
import (
18+
"testing"
19+
20+
"github.com/erda-project/erda/apistructs"
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestFindFirstLastStepInGroup(t *testing.T) {
25+
tt := []struct {
26+
steps []*apistructs.TestPlanV2Step
27+
wantFirst uint64
28+
wantLast uint64
29+
}{
30+
{
31+
steps: []*apistructs.TestPlanV2Step{
32+
{ID: 2, PreID: 1},
33+
{ID: 3, PreID: 2},
34+
{ID: 4, PreID: 3},
35+
{ID: 5, PreID: 4},
36+
},
37+
wantFirst: 2,
38+
wantLast: 5,
39+
},
40+
{
41+
steps: []*apistructs.TestPlanV2Step{
42+
{ID: 4, PreID: 0},
43+
{ID: 2, PreID: 4},
44+
{ID: 3, PreID: 5},
45+
{ID: 5, PreID: 2},
46+
},
47+
wantFirst: 4,
48+
wantLast: 3,
49+
},
50+
{
51+
steps: []*apistructs.TestPlanV2Step{
52+
{ID: 1, PreID: 0},
53+
},
54+
wantFirst: 1,
55+
wantLast: 1,
56+
},
57+
}
58+
for _, v := range tt {
59+
firstStep, lastStep := findFirstLastStepInGroup(v.steps)
60+
firstStepID, lastStepID := firstStep.ID, lastStep.ID
61+
assert.Equal(t, v.wantFirst, firstStepID)
62+
assert.Equal(t, v.wantLast, lastStepID)
63+
}
64+
}

0 commit comments

Comments
 (0)