Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
littlejiancc committed Sep 28, 2021
1 parent 1ad06ea commit f24ea3d
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 4 deletions.
44 changes: 44 additions & 0 deletions apistructs/sceneset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apistructs

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/rand"
)

func TestSceneSetRequestValidate(t *testing.T) {
tt := []struct {
req SceneSetRequest
want bool
}{
{SceneSetRequest{Name: rand.String(50), Description: "1"}, true},
{SceneSetRequest{Name: rand.String(51), Description: "1"}, false},
{SceneSetRequest{Name: "1", Description: rand.String(255)}, true},
{SceneSetRequest{Name: "1", Description: rand.String(256)}, false},
{SceneSetRequest{Name: "****", Description: "1"}, false},
{SceneSetRequest{Name: "/", Description: "1"}, false},
{SceneSetRequest{Name: "_abd1", Description: "1"}, true},
{SceneSetRequest{Name: "_测试", Description: "1"}, true},
{SceneSetRequest{Name: "1_测试a", Description: "1"}, true},
{SceneSetRequest{Name: "a_测试1", Description: "1"}, true},
}
for _, v := range tt {
assert.Equal(t, v.want, v.req.Validate() == nil)
}

}
2 changes: 1 addition & 1 deletion apistructs/testplan_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ type TestPlanV2StepMoveRequest struct {
ScenesSetId uint64 `json:"scenesSetId"`
TestPlanID uint64 `json:"-"`
TargetStepID uint64 `json:"targetStepID"`
IsGroup bool `json:"isGroup"` // if move with group
IsGroup bool `json:"isGroup"` // true: means move with group

IdentityInfo
}
3 changes: 1 addition & 2 deletions modules/dop/dao/testplan_v2_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ func (client *DBClient) MoveTestPlanV2Step(req *apistructs.TestPlanV2StepMoveReq
// update step groupID in the group if isGroup is false
defer func() error {
if err == nil && !req.IsGroup {
groupIDs := []uint64{oldGroupID, newGroupID}
groupIDs = strutil.DedupUint64Slice(groupIDs, true)
groupIDs := strutil.DedupUint64Slice([]uint64{oldGroupID, newGroupID}, true)
return updateStepGroup(tx, groupIDs...)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion modules/dop/services/autotest_v2/testplan_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func (svc *Service) ExecuteDiceAutotestTestPlan(req apistructs.AutotestExecuteTe
func getStepMapByGroupID(steps []*apistructs.TestPlanV2Step) (map[uint64][]*apistructs.TestPlanV2Step, []uint64) {
// stepGroupMap key: groupID, if groupID is 0, set id as groupID
stepGroupMap := make(map[uint64][]*apistructs.TestPlanV2Step, 0)
groupIDs := make([]uint64, 0) // order to sort
groupIDs := make([]uint64, 0) // make sure the order remains the same
for _, v := range steps {
if v.SceneSetID <= 0 {
continue
Expand Down
82 changes: 82 additions & 0 deletions modules/dop/services/autotest_v2/testplan_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package autotestv2

import (
"reflect"
"testing"

"github.com/erda-project/erda/apistructs"
)

func TestGetStepMapByGroupID(t *testing.T) {
tt := []struct {
steps []*apistructs.TestPlanV2Step
wantGroupIDs []uint64
wantStepGroupMap map[uint64][]*apistructs.TestPlanV2Step
}{
{
steps: []*apistructs.TestPlanV2Step{
{ID: 1, PreID: 0, GroupID: 0, SceneSetID: 1},
{ID: 2, PreID: 1, GroupID: 2, SceneSetID: 2},
{ID: 3, PreID: 2, GroupID: 2, SceneSetID: 3},
{ID: 4, PreID: 3, GroupID: 4, SceneSetID: 4},
{ID: 5, PreID: 4, GroupID: 4, SceneSetID: 5},
{ID: 6, PreID: 5, GroupID: 4, SceneSetID: 6},
{ID: 7, PreID: 6, GroupID: 7, SceneSetID: 7},
},
wantGroupIDs: []uint64{1, 2, 4, 7},
wantStepGroupMap: map[uint64][]*apistructs.TestPlanV2Step{
1: {{ID: 1, PreID: 0, GroupID: 0, SceneSetID: 1}},
2: {{ID: 2, PreID: 1, GroupID: 2, SceneSetID: 2}, {ID: 3, PreID: 2, GroupID: 2, SceneSetID: 3}},
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}},
7: {{ID: 7, PreID: 6, GroupID: 7, SceneSetID: 7}},
},
},
{
steps: []*apistructs.TestPlanV2Step{
{ID: 5, PreID: 0, GroupID: 0, SceneSetID: 1},
{ID: 3, PreID: 5, GroupID: 3, SceneSetID: 2},
{ID: 7, PreID: 3, GroupID: 4, SceneSetID: 3},
{ID: 4, PreID: 7, GroupID: 4, SceneSetID: 4},
{ID: 1, PreID: 4, GroupID: 1, SceneSetID: 5},
{ID: 2, PreID: 1, GroupID: 1, SceneSetID: 6},
{ID: 6, PreID: 2, GroupID: 6, SceneSetID: 7},
},
wantGroupIDs: []uint64{5, 3, 4, 1, 6},
wantStepGroupMap: map[uint64][]*apistructs.TestPlanV2Step{
5: {{ID: 5, PreID: 0, GroupID: 0, SceneSetID: 1}},
3: {{ID: 3, PreID: 5, GroupID: 3, SceneSetID: 2}},
4: {{ID: 7, PreID: 3, GroupID: 4, SceneSetID: 3}, {ID: 4, PreID: 7, GroupID: 4, SceneSetID: 4}},
1: {{ID: 1, PreID: 4, GroupID: 1, SceneSetID: 5}, {ID: 2, PreID: 1, GroupID: 1, SceneSetID: 6}},
6: {{ID: 6, PreID: 2, GroupID: 6, SceneSetID: 7}},
},
},
}

for _, v := range tt {
stepGroupMap, groupIDs := getStepMapByGroupID(v.steps)
if !reflect.DeepEqual(v.wantGroupIDs, groupIDs) {
t.Error("fail")
}
for k, v1 := range stepGroupMap {
for i, v2 := range v1 {
if v.wantStepGroupMap[k][i].ID != v2.ID {
t.Error("fail")
}
}
}
}
}

0 comments on commit f24ea3d

Please sign in to comment.