|
| 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 | +} |
0 commit comments