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