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