vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/simple_projection_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package engine
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/sqltypes"
    27  
    28  	querypb "vitess.io/vitess/go/vt/proto/query"
    29  )
    30  
    31  func TestSubqueryExecute(t *testing.T) {
    32  	prim := &fakePrimitive{
    33  		results: []*sqltypes.Result{
    34  			sqltypes.MakeTestResult(
    35  				sqltypes.MakeTestFields(
    36  					"col1|col2|col3",
    37  					"int64|varchar|varchar",
    38  				),
    39  				"1|a|aa",
    40  				"2|b|bb",
    41  				"3|c|cc",
    42  			),
    43  		},
    44  	}
    45  
    46  	sq := &SimpleProjection{
    47  		Cols:  []int{0, 2},
    48  		Input: prim,
    49  	}
    50  
    51  	bv := map[string]*querypb.BindVariable{
    52  		"a": sqltypes.Int64BindVariable(1),
    53  	}
    54  
    55  	r, err := sq.TryExecute(context.Background(), &noopVCursor{}, bv, true)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	prim.ExpectLog(t, []string{
    60  		`Execute a: type:INT64 value:"1" true`,
    61  	})
    62  	expectResult(t, "sq.Execute", r, sqltypes.MakeTestResult(
    63  		sqltypes.MakeTestFields(
    64  			"col1|col3",
    65  			"int64|varchar",
    66  		),
    67  		"1|aa",
    68  		"2|bb",
    69  		"3|cc",
    70  	))
    71  
    72  	// Error case.
    73  	sq.Input = &fakePrimitive{
    74  		sendErr: errors.New("err"),
    75  	}
    76  	_, err = sq.TryExecute(context.Background(), &noopVCursor{}, bv, true)
    77  	require.EqualError(t, err, `err`)
    78  }
    79  
    80  func TestSubqueryStreamExecute(t *testing.T) {
    81  	prim := &fakePrimitive{
    82  		results: []*sqltypes.Result{
    83  			sqltypes.MakeTestResult(
    84  				sqltypes.MakeTestFields(
    85  					"col1|col2|col3",
    86  					"int64|varchar|varchar",
    87  				),
    88  				"1|a|aa",
    89  				"2|b|bb",
    90  				"3|c|cc",
    91  			),
    92  		},
    93  	}
    94  
    95  	sq := &SimpleProjection{
    96  		Cols:  []int{0, 2},
    97  		Input: prim,
    98  	}
    99  
   100  	bv := map[string]*querypb.BindVariable{
   101  		"a": sqltypes.Int64BindVariable(1),
   102  	}
   103  
   104  	r, err := wrapStreamExecute(sq, &noopVCursor{}, bv, true)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	prim.ExpectLog(t, []string{
   109  		`StreamExecute a: type:INT64 value:"1" true`,
   110  	})
   111  	expectResult(t, "sq.Execute", r, sqltypes.MakeTestResult(
   112  		sqltypes.MakeTestFields(
   113  			"col1|col3",
   114  			"int64|varchar",
   115  		),
   116  		"1|aa",
   117  		"2|bb",
   118  		"3|cc",
   119  	))
   120  
   121  	// Error case.
   122  	sq.Input = &fakePrimitive{
   123  		sendErr: errors.New("err"),
   124  	}
   125  	_, err = wrapStreamExecute(sq, &noopVCursor{}, bv, true)
   126  	require.EqualError(t, err, `err`)
   127  }
   128  
   129  func TestSubqueryGetFields(t *testing.T) {
   130  	prim := &fakePrimitive{
   131  		results: []*sqltypes.Result{
   132  			sqltypes.MakeTestResult(
   133  				sqltypes.MakeTestFields(
   134  					"col1|col2|col3",
   135  					"int64|varchar|varchar",
   136  				),
   137  				"1|a|aa",
   138  				"2|b|bb",
   139  				"3|c|cc",
   140  			),
   141  		},
   142  	}
   143  
   144  	sq := &SimpleProjection{
   145  		Cols:  []int{0, 2},
   146  		Input: prim,
   147  	}
   148  
   149  	bv := map[string]*querypb.BindVariable{
   150  		"a": sqltypes.Int64BindVariable(1),
   151  	}
   152  
   153  	r, err := sq.GetFields(context.Background(), nil, bv)
   154  	if err != nil {
   155  		t.Fatal(err)
   156  	}
   157  	prim.ExpectLog(t, []string{
   158  		`GetFields a: type:INT64 value:"1"`,
   159  		`Execute a: type:INT64 value:"1" true`,
   160  	})
   161  	expectResult(t, "sq.Execute", r, sqltypes.MakeTestResult(
   162  		sqltypes.MakeTestFields(
   163  			"col1|col3",
   164  			"int64|varchar",
   165  		),
   166  	))
   167  
   168  	// Error case.
   169  	sq.Input = &fakePrimitive{
   170  		sendErr: errors.New("err"),
   171  	}
   172  	_, err = sq.GetFields(context.Background(), nil, bv)
   173  	require.EqualError(t, err, `err`)
   174  }