github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/merger/aggregatemerger/aggregator/sum_test.go (about) 1 // Copyright 2021 ecodeclub 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 aggregator 16 17 import ( 18 "testing" 19 20 "github.com/ecodeclub/eorm/internal/merger" 21 22 "github.com/ecodeclub/eorm/internal/merger/internal/errs" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestSum_Aggregate(t *testing.T) { 28 testcases := []struct { 29 name string 30 input [][]any 31 wantVal any 32 wantErr error 33 sumIndex int 34 }{ 35 { 36 name: "sum正常合并", 37 input: [][]any{ 38 { 39 int64(10), 40 }, 41 { 42 int64(20), 43 }, 44 { 45 int64(30), 46 }, 47 }, 48 wantVal: int64(60), 49 sumIndex: 0, 50 }, 51 52 { 53 name: "传入的参数非AggregateElement类型", 54 input: [][]any{ 55 { 56 "1", 57 }, 58 { 59 "3", 60 }, 61 }, 62 wantErr: errs.ErrMergerAggregateFuncNotFound, 63 sumIndex: 0, 64 }, 65 { 66 name: "columnInfo的index不合法", 67 input: [][]any{ 68 { 69 int64(10), 70 }, 71 { 72 int64(20), 73 }, 74 { 75 int64(30), 76 }, 77 }, 78 sumIndex: 20, 79 wantErr: errs.ErrMergerInvalidAggregateColumnIndex, 80 }, 81 } 82 for _, tc := range testcases { 83 t.Run(tc.name, func(t *testing.T) { 84 sum := NewSum(merger.NewColumnInfo(tc.sumIndex, "SUM(id)")) 85 val, err := sum.Aggregate(tc.input) 86 assert.Equal(t, tc.wantErr, err) 87 if err != nil { 88 return 89 } 90 assert.Equal(t, tc.wantVal, val) 91 assert.Equal(t, "SUM(id)", sum.ColumnName()) 92 }) 93 } 94 95 }