github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/insert/insert_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 insert
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/golang/mock/gomock"
    23  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    26  	mock_frontend "github.com/matrixorigin/matrixone/pkg/frontend/test"
    27  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    28  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/value_scan"
    29  	"github.com/matrixorigin/matrixone/pkg/testutil"
    30  	"github.com/matrixorigin/matrixone/pkg/vm"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  type mockRelation struct {
    36  	engine.Relation
    37  	result *batch.Batch
    38  }
    39  
    40  func (e *mockRelation) Write(_ context.Context, b *batch.Batch) error {
    41  	e.result = b
    42  	return nil
    43  }
    44  
    45  func TestInsertOperator(t *testing.T) {
    46  	ctrl := gomock.NewController(t)
    47  	defer ctrl.Finish()
    48  
    49  	ctx := context.TODO()
    50  	txnOperator := mock_frontend.NewMockTxnOperator(ctrl)
    51  	txnOperator.EXPECT().Commit(gomock.Any()).Return(nil).AnyTimes()
    52  	txnOperator.EXPECT().Rollback(ctx).Return(nil).AnyTimes()
    53  
    54  	txnClient := mock_frontend.NewMockTxnClient(ctrl)
    55  	txnClient.EXPECT().New(gomock.Any(), gomock.Any()).Return(txnOperator, nil).AnyTimes()
    56  
    57  	eng := mock_frontend.NewMockEngine(ctrl)
    58  	eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    59  	eng.EXPECT().Hints().Return(engine.Hints{
    60  		CommitOrRollbackTimeout: time.Second,
    61  	}).AnyTimes()
    62  
    63  	proc := testutil.NewProc()
    64  	proc.TxnClient = txnClient
    65  	proc.Ctx = ctx
    66  	batch1 := &batch.Batch{
    67  		Vecs: []*vector.Vector{
    68  			testutil.MakeInt64Vector([]int64{1, 2, 0}, []uint64{2}),
    69  			testutil.MakeScalarInt64(3, 3),
    70  			testutil.MakeVarcharVector([]string{"a", "b", "c"}, nil),
    71  			testutil.MakeScalarVarchar("d", 3),
    72  			testutil.MakeScalarNull(types.T_int64, 3),
    73  		},
    74  		Attrs: []string{"int64_column", "scalar_int64", "varchar_column", "scalar_varchar", "int64_column"},
    75  		Cnt:   1,
    76  	}
    77  	batch1.SetRowCount(3)
    78  	argument1 := Argument{
    79  		InsertCtx: &InsertCtx{
    80  			Rel: &mockRelation{},
    81  			Ref: &plan.ObjectRef{
    82  				Obj:        0,
    83  				SchemaName: "testDb",
    84  				ObjName:    "testTable",
    85  			},
    86  			AddAffectedRows: true,
    87  			Attrs:           []string{"int64_column", "scalar_int64", "varchar_column", "scalar_varchar", "int64_column"},
    88  		},
    89  		OperatorBase: vm.OperatorBase{
    90  			OperatorInfo: vm.OperatorInfo{
    91  				Idx:     0,
    92  				IsFirst: false,
    93  				IsLast:  false,
    94  			},
    95  		},
    96  		ctr: &container{
    97  			state: vm.Build,
    98  		},
    99  	}
   100  	resetChildren(&argument1, batch1)
   101  	err := argument1.Prepare(proc)
   102  	require.NoError(t, err)
   103  	_, err = argument1.Call(proc)
   104  	require.NoError(t, err)
   105  	// result := argument1.InsertCtx.Rel.(*mockRelation).result
   106  	// require.Equal(t, result.Batch, batch.EmptyBatch)
   107  
   108  	argument1.Free(proc, false, nil)
   109  	argument1.GetChildren(0).Free(proc, false, nil)
   110  	proc.FreeVectors()
   111  	require.Equal(t, int64(0), proc.GetMPool().CurrNB())
   112  }
   113  
   114  func resetChildren(arg *Argument, bat *batch.Batch) {
   115  	arg.SetChildren(
   116  		[]vm.Operator{
   117  			&value_scan.Argument{
   118  				Batchs: []*batch.Batch{bat},
   119  			},
   120  		})
   121  
   122  	arg.ctr.state = vm.Build
   123  }