Skip to content

Commit f24ea3d

Browse files
committed
add unit test
1 parent 1ad06ea commit f24ea3d

File tree

5 files changed

+129
-4
lines changed

5 files changed

+129
-4
lines changed

apistructs/sceneset_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 apistructs
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
"k8s.io/apimachinery/pkg/util/rand"
22+
)
23+
24+
func TestSceneSetRequestValidate(t *testing.T) {
25+
tt := []struct {
26+
req SceneSetRequest
27+
want bool
28+
}{
29+
{SceneSetRequest{Name: rand.String(50), Description: "1"}, true},
30+
{SceneSetRequest{Name: rand.String(51), Description: "1"}, false},
31+
{SceneSetRequest{Name: "1", Description: rand.String(255)}, true},
32+
{SceneSetRequest{Name: "1", Description: rand.String(256)}, false},
33+
{SceneSetRequest{Name: "****", Description: "1"}, false},
34+
{SceneSetRequest{Name: "/", Description: "1"}, false},
35+
{SceneSetRequest{Name: "_abd1", Description: "1"}, true},
36+
{SceneSetRequest{Name: "_测试", Description: "1"}, true},
37+
{SceneSetRequest{Name: "1_测试a", Description: "1"}, true},
38+
{SceneSetRequest{Name: "a_测试1", Description: "1"}, true},
39+
}
40+
for _, v := range tt {
41+
assert.Equal(t, v.want, v.req.Validate() == nil)
42+
}
43+
44+
}

apistructs/testplan_v2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ type TestPlanV2StepMoveRequest struct {
245245
ScenesSetId uint64 `json:"scenesSetId"`
246246
TestPlanID uint64 `json:"-"`
247247
TargetStepID uint64 `json:"targetStepID"`
248-
IsGroup bool `json:"isGroup"` // if move with group
248+
IsGroup bool `json:"isGroup"` // true: means move with group
249249

250250
IdentityInfo
251251
}

modules/dop/dao/testplan_v2_step.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ func (client *DBClient) MoveTestPlanV2Step(req *apistructs.TestPlanV2StepMoveReq
177177
// update step groupID in the group if isGroup is false
178178
defer func() error {
179179
if err == nil && !req.IsGroup {
180-
groupIDs := []uint64{oldGroupID, newGroupID}
181-
groupIDs = strutil.DedupUint64Slice(groupIDs, true)
180+
groupIDs := strutil.DedupUint64Slice([]uint64{oldGroupID, newGroupID}, true)
182181
return updateStepGroup(tx, groupIDs...)
183182
}
184183
return nil

modules/dop/services/autotest_v2/testplan_v2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ func (svc *Service) ExecuteDiceAutotestTestPlan(req apistructs.AutotestExecuteTe
510510
func getStepMapByGroupID(steps []*apistructs.TestPlanV2Step) (map[uint64][]*apistructs.TestPlanV2Step, []uint64) {
511511
// stepGroupMap key: groupID, if groupID is 0, set id as groupID
512512
stepGroupMap := make(map[uint64][]*apistructs.TestPlanV2Step, 0)
513-
groupIDs := make([]uint64, 0) // order to sort
513+
groupIDs := make([]uint64, 0) // make sure the order remains the same
514514
for _, v := range steps {
515515
if v.SceneSetID <= 0 {
516516
continue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 autotestv2
16+
17+
import (
18+
"reflect"
19+
"testing"
20+
21+
"github.com/erda-project/erda/apistructs"
22+
)
23+
24+
func TestGetStepMapByGroupID(t *testing.T) {
25+
tt := []struct {
26+
steps []*apistructs.TestPlanV2Step
27+
wantGroupIDs []uint64
28+
wantStepGroupMap map[uint64][]*apistructs.TestPlanV2Step
29+
}{
30+
{
31+
steps: []*apistructs.TestPlanV2Step{
32+
{ID: 1, PreID: 0, GroupID: 0, SceneSetID: 1},
33+
{ID: 2, PreID: 1, GroupID: 2, SceneSetID: 2},
34+
{ID: 3, PreID: 2, GroupID: 2, SceneSetID: 3},
35+
{ID: 4, PreID: 3, GroupID: 4, SceneSetID: 4},
36+
{ID: 5, PreID: 4, GroupID: 4, SceneSetID: 5},
37+
{ID: 6, PreID: 5, GroupID: 4, SceneSetID: 6},
38+
{ID: 7, PreID: 6, GroupID: 7, SceneSetID: 7},
39+
},
40+
wantGroupIDs: []uint64{1, 2, 4, 7},
41+
wantStepGroupMap: map[uint64][]*apistructs.TestPlanV2Step{
42+
1: {{ID: 1, PreID: 0, GroupID: 0, SceneSetID: 1}},
43+
2: {{ID: 2, PreID: 1, GroupID: 2, SceneSetID: 2}, {ID: 3, PreID: 2, GroupID: 2, SceneSetID: 3}},
44+
4: {{ID: 4, PreID: 3, GroupID: 4, SceneSetID: 4}, {ID: 5, PreID: 4, GroupID: 4, SceneSetID: 5}, {ID: 6, PreID: 5, GroupID: 4, SceneSetID: 6}},
45+
7: {{ID: 7, PreID: 6, GroupID: 7, SceneSetID: 7}},
46+
},
47+
},
48+
{
49+
steps: []*apistructs.TestPlanV2Step{
50+
{ID: 5, PreID: 0, GroupID: 0, SceneSetID: 1},
51+
{ID: 3, PreID: 5, GroupID: 3, SceneSetID: 2},
52+
{ID: 7, PreID: 3, GroupID: 4, SceneSetID: 3},
53+
{ID: 4, PreID: 7, GroupID: 4, SceneSetID: 4},
54+
{ID: 1, PreID: 4, GroupID: 1, SceneSetID: 5},
55+
{ID: 2, PreID: 1, GroupID: 1, SceneSetID: 6},
56+
{ID: 6, PreID: 2, GroupID: 6, SceneSetID: 7},
57+
},
58+
wantGroupIDs: []uint64{5, 3, 4, 1, 6},
59+
wantStepGroupMap: map[uint64][]*apistructs.TestPlanV2Step{
60+
5: {{ID: 5, PreID: 0, GroupID: 0, SceneSetID: 1}},
61+
3: {{ID: 3, PreID: 5, GroupID: 3, SceneSetID: 2}},
62+
4: {{ID: 7, PreID: 3, GroupID: 4, SceneSetID: 3}, {ID: 4, PreID: 7, GroupID: 4, SceneSetID: 4}},
63+
1: {{ID: 1, PreID: 4, GroupID: 1, SceneSetID: 5}, {ID: 2, PreID: 1, GroupID: 1, SceneSetID: 6}},
64+
6: {{ID: 6, PreID: 2, GroupID: 6, SceneSetID: 7}},
65+
},
66+
},
67+
}
68+
69+
for _, v := range tt {
70+
stepGroupMap, groupIDs := getStepMapByGroupID(v.steps)
71+
if !reflect.DeepEqual(v.wantGroupIDs, groupIDs) {
72+
t.Error("fail")
73+
}
74+
for k, v1 := range stepGroupMap {
75+
for i, v2 := range v1 {
76+
if v.wantStepGroupMap[k][i].ID != v2.ID {
77+
t.Error("fail")
78+
}
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)