github.com/matrixorigin/matrixone@v0.7.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/testutil" 25 "github.com/matrixorigin/matrixone/pkg/vm/process" 26 "github.com/stretchr/testify/require" 27 ) 28 29 const ( 30 Rows = 10 // default rows 31 ) 32 33 // add unit tests for cases 34 type outputTestCase struct { 35 arg *Argument 36 types []types.Type 37 proc *process.Process 38 } 39 40 var ( 41 tcs []outputTestCase 42 ) 43 44 func sqlOutput(_ interface{}, _ *batch.Batch) error { 45 return nil 46 } 47 48 func init() { 49 tcs = []outputTestCase{ 50 { 51 proc: testutil.NewProcessWithMPool(mpool.MustNewZero()), 52 types: []types.Type{ 53 {Oid: types.T_int8}, 54 }, 55 arg: &Argument{ 56 Data: nil, 57 Func: sqlOutput, 58 }, 59 }, 60 } 61 } 62 63 func TestString(t *testing.T) { 64 buf := new(bytes.Buffer) 65 for _, tc := range tcs { 66 String(tc.arg, buf) 67 } 68 } 69 70 func TestPrepare(t *testing.T) { 71 for _, tc := range tcs { 72 err := Prepare(tc.proc, tc.arg) 73 require.NoError(t, err) 74 } 75 } 76 77 func TestOutput(t *testing.T) { 78 for _, tc := range tcs { 79 err := Prepare(tc.proc, tc.arg) 80 require.NoError(t, err) 81 tc.proc.Reg.InputBatch = newBatch(t, tc.types, tc.proc, Rows) 82 _, err = Call(0, tc.proc, tc.arg, false, false) 83 require.NoError(t, err) 84 tc.proc.Reg.InputBatch = newBatch(t, tc.types, tc.proc, Rows) 85 _, err = Call(0, tc.proc, tc.arg, false, false) 86 require.NoError(t, err) 87 tc.proc.Reg.InputBatch = &batch.Batch{} 88 _, err = Call(0, tc.proc, tc.arg, false, false) 89 require.NoError(t, err) 90 tc.proc.Reg.InputBatch = nil 91 _, err = Call(0, tc.proc, tc.arg, false, false) 92 require.NoError(t, err) 93 require.Equal(t, int64(0), tc.proc.Mp().CurrNB()) 94 } 95 } 96 97 // create a new block based on the type information 98 func newBatch(t *testing.T, ts []types.Type, proc *process.Process, rows int64) *batch.Batch { 99 return testutil.NewBatch(ts, false, int(rows), proc.Mp()) 100 }