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