Debian Codegraph @

dependency/sqltypes/testing.go

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