github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/output/output_test.go (about)

     1  // Copyright 2021 Matrix Origin
     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 output
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/value_scan"
    25  	"github.com/matrixorigin/matrixone/pkg/testutil"
    26  	"github.com/matrixorigin/matrixone/pkg/vm"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  const (
    32  	Rows = 10 // default rows
    33  )
    34  
    35  // add unit tests for cases
    36  type outputTestCase struct {
    37  	arg   *Argument
    38  	types []types.Type
    39  	proc  *process.Process
    40  }
    41  
    42  var (
    43  	tcs []outputTestCase
    44  )
    45  
    46  func sqlOutput(_ *batch.Batch) error {
    47  	return nil
    48  }
    49  
    50  func init() {
    51  	tcs = []outputTestCase{
    52  		{
    53  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    54  			types: []types.Type{
    55  				types.T_int8.ToType(),
    56  			},
    57  			arg: &Argument{
    58  				Data: nil,
    59  				Func: sqlOutput,
    60  				OperatorBase: vm.OperatorBase{
    61  					OperatorInfo: vm.OperatorInfo{
    62  						Idx:     0,
    63  						IsFirst: false,
    64  						IsLast:  false,
    65  					},
    66  				},
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  func TestString(t *testing.T) {
    73  	buf := new(bytes.Buffer)
    74  	for _, tc := range tcs {
    75  		tc.arg.String(buf)
    76  	}
    77  }
    78  
    79  func TestPrepare(t *testing.T) {
    80  	for _, tc := range tcs {
    81  		err := tc.arg.Prepare(tc.proc)
    82  		require.NoError(t, err)
    83  	}
    84  }
    85  
    86  func TestOutput(t *testing.T) {
    87  	for _, tc := range tcs {
    88  		err := tc.arg.Prepare(tc.proc)
    89  		require.NoError(t, err)
    90  
    91  		bats := []*batch.Batch{
    92  			newBatch(tc.types, tc.proc, Rows),
    93  			newBatch(tc.types, tc.proc, Rows),
    94  			batch.EmptyBatch,
    95  		}
    96  		resetChildren(tc.arg, bats)
    97  		_, err = tc.arg.Call(tc.proc)
    98  		require.NoError(t, err)
    99  		tc.arg.Free(tc.proc, false, nil)
   100  		tc.arg.GetChildren(0).Free(tc.proc, false, nil)
   101  		tc.proc.FreeVectors()
   102  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
   103  	}
   104  }
   105  
   106  // create a new block based on the type information
   107  func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   108  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   109  }
   110  
   111  func resetChildren(arg *Argument, bats []*batch.Batch) {
   112  	arg.SetChildren(
   113  		[]vm.Operator{
   114  			&value_scan.Argument{
   115  				Batchs: bats,
   116  			},
   117  		})
   118  }