Skip to content

Commit 50b0468

Browse files
committed
add unit test for mt-case-paging refactor
1 parent d177867 commit 50b0468

File tree

8 files changed

+388
-39
lines changed

8 files changed

+388
-39
lines changed

.erda/migrations/qa/20211109-manual-test-fix-slow-sql.sql

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ALTER TABLE `dice_test_plan_case_relations` ADD INDEX `idx_testcaseid` (`test_ca
66
ALTER TABLE `dice_test_plan_case_relations` ADD INDEX `idx_plan_set_case_status_updater` (`test_plan_id`, `test_set_id`, `test_case_id`, `exec_status`, `updater_id`);
77

88
# dice_test_sets
9+
## related to: modules/dop/dao/testset.go:28
910
ALTER TABLE `dice_test_sets` MODIFY COLUMN `directory` varchar(5000) NOT NULL DEFAULT '' COMMENT '当前节点+所有父级节点的name集合(参考值:新建测试集1/新建测试集2/测试集名称3),这里冗余是为了方便界面展示。';
1011
ALTER TABLE `dice_test_sets` ADD INDEX `idx_directory` (`directory`(191));
1112

modules/dop/dao/testcase_test.go

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 dao
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
22+
"github.com/erda-project/erda/apistructs"
23+
)
24+
25+
func Test_validateTestCasePagingRequest(t *testing.T) {
26+
type args struct {
27+
req apistructs.TestCasePagingRequest
28+
}
29+
orderBy := true
30+
tests := []struct {
31+
name string
32+
args args
33+
wantErr bool
34+
}{
35+
{
36+
name: "missing project",
37+
args: args{
38+
req: apistructs.TestCasePagingRequest{},
39+
},
40+
wantErr: true,
41+
},
42+
{
43+
name: "invalid priority",
44+
args: args{
45+
req: apistructs.TestCasePagingRequest{Priorities: []apistructs.TestCasePriority{"xxx"}},
46+
},
47+
wantErr: true,
48+
},
49+
{
50+
name: "order by both priority asc/desc",
51+
args: args{
52+
req: apistructs.TestCasePagingRequest{OrderByPriorityAsc: &orderBy, OrderByPriorityDesc: &orderBy},
53+
},
54+
wantErr: true,
55+
},
56+
{
57+
name: "order by both updaterID asc/desc",
58+
args: args{
59+
req: apistructs.TestCasePagingRequest{OrderByUpdaterIDAsc: &orderBy, OrderByUpdaterIDDesc: &orderBy},
60+
},
61+
wantErr: true,
62+
},
63+
{
64+
name: "order by both updatedAt asc/desc",
65+
args: args{
66+
req: apistructs.TestCasePagingRequest{OrderByUpdatedAtAsc: &orderBy, OrderByUpdatedAtDesc: &orderBy},
67+
},
68+
wantErr: true,
69+
},
70+
{
71+
name: "order by both id asc/desc",
72+
args: args{
73+
req: apistructs.TestCasePagingRequest{OrderByIDAsc: &orderBy, OrderByIDDesc: &orderBy},
74+
},
75+
wantErr: true,
76+
},
77+
{
78+
name: "order by both testSetID asc/desc",
79+
args: args{
80+
req: apistructs.TestCasePagingRequest{OrderByTestSetIDAsc: &orderBy, OrderByTestSetIDDesc: &orderBy},
81+
},
82+
wantErr: true,
83+
},
84+
{
85+
name: "order by both testSetName asc/desc",
86+
args: args{
87+
req: apistructs.TestCasePagingRequest{OrderByTestSetNameAsc: &orderBy, OrderByTestSetNameDesc: &orderBy},
88+
},
89+
wantErr: true,
90+
},
91+
{
92+
name: "valid request",
93+
args: args{
94+
req: apistructs.TestCasePagingRequest{ProjectID: 1},
95+
},
96+
wantErr: false,
97+
},
98+
}
99+
for _, tt := range tests {
100+
t.Run(tt.name, func(t *testing.T) {
101+
if err := validateTestCasePagingRequest(tt.args.req); (err != nil) != tt.wantErr {
102+
t.Errorf("validateTestCasePagingRequest() error = %v, wantErr %v", err, tt.wantErr)
103+
}
104+
})
105+
}
106+
}
107+
108+
func Test_setDefaultForTestCasePagingRequest(t *testing.T) {
109+
// must order by testSet
110+
req1 := apistructs.TestCasePagingRequest{OrderByPriorityAsc: &[]bool{true}[0]}
111+
setDefaultForTestCasePagingRequest(&req1)
112+
assert.Equal(t, 1, len(req1.OrderFields))
113+
assert.Equal(t, tcFieldTestSetID, req1.OrderFields[0])
114+
assert.True(t, *req1.OrderByTestSetIDAsc)
115+
116+
// set default order inside a testSet
117+
req2 := apistructs.TestCasePagingRequest{OrderByTestSetIDAsc: &[]bool{true}[0]}
118+
setDefaultForTestCasePagingRequest(&req2)
119+
assert.Equal(t, 1, len(req1.OrderFields))
120+
assert.Equal(t, tcFieldID, req2.OrderFields[0])
121+
assert.True(t, *req2.OrderByIDAsc)
122+
123+
// default page
124+
req3 := apistructs.TestCasePagingRequest{}
125+
setDefaultForTestCasePagingRequest(&req3)
126+
assert.True(t, 1 == req3.PageNo)
127+
assert.True(t, 20 == req3.PageSize)
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 dao
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
22+
"github.com/erda-project/erda/apistructs"
23+
)
24+
25+
func Test_validateTestPlanCaseRelPagingRequest(t *testing.T) {
26+
type args struct {
27+
req apistructs.TestPlanCaseRelPagingRequest
28+
}
29+
orderBy := true
30+
tests := []struct {
31+
name string
32+
args args
33+
wantErr bool
34+
}{
35+
{
36+
name: "missing testPlanID",
37+
args: args{
38+
req: apistructs.TestPlanCaseRelPagingRequest{},
39+
},
40+
wantErr: true,
41+
},
42+
{
43+
name: "invalid priority",
44+
args: args{
45+
req: apistructs.TestPlanCaseRelPagingRequest{Priorities: []apistructs.TestCasePriority{"xxx"}},
46+
},
47+
wantErr: true,
48+
},
49+
{
50+
name: "order by both priority asc/desc",
51+
args: args{
52+
req: apistructs.TestPlanCaseRelPagingRequest{OrderByPriorityAsc: &orderBy, OrderByPriorityDesc: &orderBy},
53+
},
54+
wantErr: true,
55+
},
56+
{
57+
name: "order by both updaterID asc/desc",
58+
args: args{
59+
req: apistructs.TestPlanCaseRelPagingRequest{OrderByUpdaterIDAsc: &orderBy, OrderByUpdaterIDDesc: &orderBy},
60+
},
61+
wantErr: true,
62+
},
63+
{
64+
name: "order by both updatedAt asc/desc",
65+
args: args{
66+
req: apistructs.TestPlanCaseRelPagingRequest{OrderByUpdatedAtAsc: &orderBy, OrderByUpdatedAtDesc: &orderBy},
67+
},
68+
wantErr: true,
69+
},
70+
{
71+
name: "order by both id asc/desc",
72+
args: args{
73+
req: apistructs.TestPlanCaseRelPagingRequest{OrderByIDAsc: &orderBy, OrderByIDDesc: &orderBy},
74+
},
75+
wantErr: true,
76+
},
77+
{
78+
name: "order by both testSetID asc/desc",
79+
args: args{
80+
req: apistructs.TestPlanCaseRelPagingRequest{OrderByTestSetIDAsc: &orderBy, OrderByTestSetIDDesc: &orderBy},
81+
},
82+
wantErr: true,
83+
},
84+
{
85+
name: "order by both testSetName asc/desc",
86+
args: args{
87+
req: apistructs.TestPlanCaseRelPagingRequest{OrderByTestSetNameAsc: &orderBy, OrderByTestSetNameDesc: &orderBy},
88+
},
89+
wantErr: true,
90+
},
91+
{
92+
name: "valid request",
93+
args: args{
94+
req: apistructs.TestPlanCaseRelPagingRequest{TestPlanID: 1},
95+
},
96+
wantErr: false,
97+
},
98+
}
99+
for _, tt := range tests {
100+
t.Run(tt.name, func(t *testing.T) {
101+
if err := validateTestPlanCaseRelPagingRequest(tt.args.req); (err != nil) != tt.wantErr {
102+
t.Errorf("validateTestPlanCaseRelPagingRequest() error = %v, wantErr %v", err, tt.wantErr)
103+
}
104+
})
105+
}
106+
}
107+
108+
func Test_setDefaultForTestPlanCaseRelPagingRequest(t *testing.T) {
109+
// must order by testSet
110+
req1 := apistructs.TestPlanCaseRelPagingRequest{OrderByPriorityAsc: &[]bool{true}[0]}
111+
setDefaultForTestPlanCaseRelPagingRequest(&req1)
112+
assert.Equal(t, 1, len(req1.OrderFields))
113+
assert.Equal(t, tcFieldTestSetID, req1.OrderFields[0])
114+
assert.True(t, *req1.OrderByTestSetIDAsc)
115+
116+
// set default order inside a testSet
117+
req2 := apistructs.TestPlanCaseRelPagingRequest{OrderByTestSetIDAsc: &[]bool{true}[0]}
118+
setDefaultForTestPlanCaseRelPagingRequest(&req2)
119+
assert.Equal(t, 1, len(req1.OrderFields))
120+
assert.Equal(t, tcFieldID, req2.OrderFields[0])
121+
assert.True(t, *req2.OrderByIDAsc)
122+
123+
// default page
124+
req3 := apistructs.TestPlanCaseRelPagingRequest{}
125+
setDefaultForTestPlanCaseRelPagingRequest(&req3)
126+
assert.True(t, 1 == req3.PageNo)
127+
assert.True(t, 20 == req3.PageSize)
128+
}

modules/dop/dao/testset.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import (
2525
)
2626

2727
const (
28-
MaxTestSetDirectoryLength = 8192
28+
// related to: 20211109-manual-test-fix-slow-sql.sql #9
29+
maxTestSetDirectoryLength = 5000
2930
)
3031

3132
func ValidateTestSetDirectoryLength(dir string) error {
32-
return strutil.Validate(dir, strutil.MaxRuneCountValidator(MaxTestSetDirectoryLength))
33+
return strutil.Validate(dir, strutil.MaxRuneCountValidator(maxTestSetDirectoryLength))
3334
}
3435

3536
// TestSet 测试集

modules/dop/dao/testset_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 dao
16+
17+
import (
18+
"math/rand"
19+
"testing"
20+
"time"
21+
)
22+
23+
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
24+
25+
func RandStringRunes(n int) string {
26+
rand.Seed(time.Now().UnixNano())
27+
b := make([]rune, n)
28+
for i := range b {
29+
b[i] = letterRunes[rand.Intn(len(letterRunes))]
30+
}
31+
return string(b)
32+
}
33+
34+
func TestValidateTestSetDirectoryLength(t *testing.T) {
35+
type args struct {
36+
dir string
37+
}
38+
tests := []struct {
39+
name string
40+
args args
41+
wantErr bool
42+
}{
43+
{
44+
name: "too long directory",
45+
args: args{
46+
dir: RandStringRunes(maxTestSetDirectoryLength + 1),
47+
},
48+
wantErr: true,
49+
},
50+
{
51+
name: "directory with exact length",
52+
args: args{
53+
dir: RandStringRunes(maxTestSetDirectoryLength),
54+
},
55+
wantErr: false,
56+
},
57+
{
58+
name: "small directory",
59+
args: args{
60+
dir: RandStringRunes(30),
61+
},
62+
wantErr: false,
63+
},
64+
}
65+
for _, tt := range tests {
66+
t.Run(tt.name, func(t *testing.T) {
67+
if err := ValidateTestSetDirectoryLength(tt.args.dir); (err != nil) != tt.wantErr {
68+
t.Errorf("ValidateTestSetDirectoryLength() error = %v, wantErr %v", err, tt.wantErr)
69+
}
70+
})
71+
}
72+
}

modules/dop/services/testcase/paging.go

-31
Original file line numberDiff line numberDiff line change
@@ -203,34 +203,3 @@ func getAlphabetSortedTestSetIDs(testSets []dao.TestSet, order string) []uint64
203203
}
204204
return result
205205
}
206-
207-
func (svc *Service) getSubTestSetIDsInMemory(projectID, baseTestSetID uint64, recycled bool) ([]uint64, error) {
208-
testSets, err := svc.db.ListTestSets(apistructs.TestSetListRequest{
209-
Recycled: recycled,
210-
ProjectID: &projectID,
211-
NoSubTestSets: true,
212-
})
213-
if err != nil {
214-
return nil, err
215-
}
216-
// slice => map
217-
testSetByParentID := make(map[uint64][]uint64)
218-
for _, ts := range testSets {
219-
testSetByParentID[ts.ParentID] = append(testSetByParentID[ts.ParentID], ts.ID)
220-
}
221-
// iterate from base
222-
allTestSetIDsFromBase := findSub(testSetByParentID, baseTestSetID)
223-
return allTestSetIDsFromBase, nil
224-
}
225-
226-
func findSub(all map[uint64][]uint64, currentTestSetID uint64) []uint64 {
227-
subIDs, ok := all[currentTestSetID]
228-
if !ok {
229-
return nil
230-
}
231-
var result []uint64
232-
for _, subID := range subIDs {
233-
result = append(result, findSub(all, subID)...)
234-
}
235-
return result
236-
}

0 commit comments

Comments
 (0)