github.com/team-ide/go-dialect@v1.9.20/vitess/sqltypes/testing.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 sqltypes
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  
    24  	querypb "github.com/team-ide/go-dialect/vitess/query"
    25  )
    26  
    27  // Functions in this file should only be used for testing.
    28  // This is an experiment to see if test code bloat can be
    29  // reduced and readability improved.
    30  
    31  // MakeTestFields builds a []*querypb.Field for testing.
    32  //   fields := sqltypes.MakeTestFields(
    33  //     "a|b",
    34  //     "int64|varchar",
    35  //   )
    36  // The field types are as defined in querypb and are case
    37  // insensitive. Column delimiters must be used only to sepearate
    38  // strings and not at the beginning or the end.
    39  func MakeTestFields(names, types string) []*querypb.Field {
    40  	n := split(names)
    41  	t := split(types)
    42  	var fields []*querypb.Field
    43  	for i := range n {
    44  		fields = append(fields, &querypb.Field{
    45  			Name: n[i],
    46  			Type: querypb.Type(querypb.Type_value[strings.ToUpper(t[i])]),
    47  		})
    48  	}
    49  	return fields
    50  }
    51  
    52  // MakeTestResult builds a *sqltypes.Result object for testing.
    53  //   result := sqltypes.MakeTestResult(
    54  //     fields,
    55  //     " 1|a",
    56  //     "10|abcd",
    57  //   )
    58  // The field type values are set as the types for the rows built.
    59  // Spaces are trimmed from row values. "null" is treated as NULL.
    60  func MakeTestResult(fields []*querypb.Field, rows ...string) *Result {
    61  	result := &Result{
    62  		Fields: fields,
    63  	}
    64  	if len(rows) > 0 {
    65  		result.Rows = make([][]Value, len(rows))
    66  	}
    67  	for i, row := range rows {
    68  		result.Rows[i] = make([]Value, len(fields))
    69  		for j, col := range split(row) {
    70  			if col == "null" {
    71  				continue
    72  			}
    73  			result.Rows[i][j] = MakeTrusted(fields[j].Type, []byte(col))
    74  		}
    75  	}
    76  	return result
    77  }
    78  
    79  // MakeTestStreamingResults builds a list of results for streaming.
    80  //   results := sqltypes.MakeStreamingResults(
    81  //     fields,
    82  //		 "1|a",
    83  //     "2|b",
    84  //     "---",
    85  //     "c|c",
    86  //   )
    87  // The first result contains only the fields. Subsequent results
    88  // are built using the field types. Every input that starts with a "-"
    89  // is treated as streaming delimiter for one result. A final
    90  // delimiter must not be supplied.
    91  func MakeTestStreamingResults(fields []*querypb.Field, rows ...string) []*Result {
    92  	var results []*Result
    93  	results = append(results, &Result{Fields: fields})
    94  	start := 0
    95  	cur := 0
    96  	// Add a final streaming delimiter to simplify the loop below.
    97  	rows = append(rows, "-")
    98  	for cur < len(rows) {
    99  		if rows[cur][0] != '-' {
   100  			cur++
   101  			continue
   102  		}
   103  		result := MakeTestResult(fields, rows[start:cur]...)
   104  		result.Fields = nil
   105  		result.RowsAffected = 0
   106  		results = append(results, result)
   107  		start = cur + 1
   108  		cur = start
   109  	}
   110  	return results
   111  }
   112  
   113  // TestBindVariable makes a *querypb.BindVariable from
   114  // an interface{}.It panics on invalid input.
   115  // This function should only be used for testing.
   116  func TestBindVariable(v interface{}) *querypb.BindVariable {
   117  	if v == nil {
   118  		return NullBindVariable
   119  	}
   120  	bv, err := BuildBindVariable(v)
   121  	if err != nil {
   122  		panic(err)
   123  	}
   124  	return bv
   125  }
   126  
   127  // TestValue builds a Value from typ and val.
   128  // This function should only be used for testing.
   129  func TestValue(typ querypb.Type, val string) Value {
   130  	return MakeTrusted(typ, []byte(val))
   131  }
   132  
   133  // PrintResults prints []*Results into a string.
   134  // This function should only be used for testing.
   135  func PrintResults(results []*Result) string {
   136  	b := new(bytes.Buffer)
   137  	for i, r := range results {
   138  		if i == 0 {
   139  			fmt.Fprintf(b, "%v", r)
   140  			continue
   141  		}
   142  		fmt.Fprintf(b, ", %v", r)
   143  	}
   144  	return b.String()
   145  }
   146  
   147  func split(str string) []string {
   148  	return strings.Split(str, "|")
   149  }