vitess.io/vitess@v0.16.2/go/sqltypes/proto3_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 sqltypes 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 "google.golang.org/protobuf/proto" 24 25 querypb "vitess.io/vitess/go/vt/proto/query" 26 vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" 27 "vitess.io/vitess/go/vt/vterrors" 28 ) 29 30 func TestResult(t *testing.T) { 31 fields := []*querypb.Field{{ 32 Name: "col1", 33 Type: VarChar, 34 }, { 35 Name: "col2", 36 Type: Int64, 37 }, { 38 Name: "col3", 39 Type: Float64, 40 }} 41 sqlResult := &Result{ 42 Fields: fields, 43 InsertID: 1, 44 RowsAffected: 2, 45 Rows: [][]Value{{ 46 TestValue(VarChar, "aa"), 47 TestValue(Int64, "1"), 48 TestValue(Float64, "2"), 49 }, { 50 MakeTrusted(VarChar, []byte("bb")), 51 NULL, 52 NULL, 53 }}, 54 } 55 p3Result := &querypb.QueryResult{ 56 Fields: fields, 57 InsertId: 1, 58 RowsAffected: 2, 59 Rows: []*querypb.Row{{ 60 Lengths: []int64{2, 1, 1}, 61 Values: []byte("aa12"), 62 }, { 63 Lengths: []int64{2, -1, -1}, 64 Values: []byte("bb"), 65 }}, 66 } 67 p3converted := ResultToProto3(sqlResult) 68 if !proto.Equal(p3converted, p3Result) { 69 t.Errorf("P3:\n%v, want\n%v", p3converted, p3Result) 70 } 71 72 reverse := Proto3ToResult(p3Result) 73 if !reverse.Equal(sqlResult) { 74 t.Errorf("reverse:\n%#v, want\n%#v", reverse, sqlResult) 75 } 76 77 // Test custom fields. 78 fields[1].Type = VarBinary 79 sqlResult.Rows[0][1] = TestValue(VarBinary, "1") 80 reverse = CustomProto3ToResult(fields, p3Result) 81 if !reverse.Equal(sqlResult) { 82 t.Errorf("reverse:\n%#v, want\n%#v", reverse, sqlResult) 83 } 84 } 85 86 func TestResults(t *testing.T) { 87 fields1 := []*querypb.Field{{ 88 Name: "col1", 89 Type: VarChar, 90 }, { 91 Name: "col2", 92 Type: Int64, 93 }, { 94 Name: "col3", 95 Type: Float64, 96 }} 97 fields2 := []*querypb.Field{{ 98 Name: "col11", 99 Type: VarChar, 100 }, { 101 Name: "col12", 102 Type: Int64, 103 }, { 104 Name: "col13", 105 Type: Float64, 106 }} 107 sqlResults := []Result{{ 108 Fields: fields1, 109 InsertID: 1, 110 RowsAffected: 2, 111 Rows: [][]Value{{ 112 TestValue(VarChar, "aa"), 113 TestValue(Int64, "1"), 114 TestValue(Float64, "2"), 115 }}, 116 }, { 117 Fields: fields2, 118 InsertID: 3, 119 RowsAffected: 4, 120 Rows: [][]Value{{ 121 TestValue(VarChar, "bb"), 122 TestValue(Int64, "3"), 123 TestValue(Float64, "4"), 124 }}, 125 }} 126 p3Results := []*querypb.QueryResult{{ 127 Fields: fields1, 128 InsertId: 1, 129 RowsAffected: 2, 130 Rows: []*querypb.Row{{ 131 Lengths: []int64{2, 1, 1}, 132 Values: []byte("aa12"), 133 }}, 134 }, { 135 Fields: fields2, 136 InsertId: 3, 137 RowsAffected: 4, 138 Rows: []*querypb.Row{{ 139 Lengths: []int64{2, 1, 1}, 140 Values: []byte("bb34"), 141 }}, 142 }} 143 p3converted := ResultsToProto3(sqlResults) 144 if !Proto3ResultsEqual(p3converted, p3Results) { 145 t.Errorf("P3:\n%v, want\n%v", p3converted, p3Results) 146 } 147 148 reverse := Proto3ToResults(p3Results) 149 if !ResultsEqual(reverse, sqlResults) { 150 t.Errorf("reverse:\n%#v, want\n%#v", reverse, sqlResults) 151 } 152 } 153 154 func TestQueryReponses(t *testing.T) { 155 fields1 := []*querypb.Field{{ 156 Name: "col1", 157 Type: VarChar, 158 }, { 159 Name: "col2", 160 Type: Int64, 161 }, { 162 Name: "col3", 163 Type: Float64, 164 }} 165 fields2 := []*querypb.Field{{ 166 Name: "col11", 167 Type: VarChar, 168 }, { 169 Name: "col12", 170 Type: Int64, 171 }, { 172 Name: "col13", 173 Type: Float64, 174 }} 175 176 queryResponses := []QueryResponse{ 177 { 178 QueryResult: &Result{ 179 Fields: fields1, 180 InsertID: 1, 181 RowsAffected: 2, 182 Rows: [][]Value{{ 183 TestValue(VarChar, "aa"), 184 TestValue(Int64, "1"), 185 TestValue(Float64, "2"), 186 }}, 187 }, 188 QueryError: nil, 189 }, { 190 QueryResult: &Result{ 191 Fields: fields2, 192 InsertID: 3, 193 RowsAffected: 4, 194 Rows: [][]Value{{ 195 TestValue(VarChar, "bb"), 196 TestValue(Int64, "3"), 197 TestValue(Float64, "4"), 198 }}, 199 }, 200 QueryError: nil, 201 }, { 202 QueryResult: nil, 203 QueryError: vterrors.New(vtrpcpb.Code_DEADLINE_EXCEEDED, "deadline exceeded"), 204 }, 205 } 206 207 p3ResultWithError := []*querypb.ResultWithError{ 208 { 209 Error: nil, 210 Result: &querypb.QueryResult{ 211 Fields: fields1, 212 InsertId: 1, 213 RowsAffected: 2, 214 Rows: []*querypb.Row{{ 215 Lengths: []int64{2, 1, 1}, 216 Values: []byte("aa12"), 217 }}, 218 }, 219 }, { 220 Error: nil, 221 Result: &querypb.QueryResult{ 222 Fields: fields2, 223 InsertId: 3, 224 RowsAffected: 4, 225 Rows: []*querypb.Row{{ 226 Lengths: []int64{2, 1, 1}, 227 Values: []byte("bb34"), 228 }}, 229 }, 230 }, { 231 Error: &vtrpcpb.RPCError{ 232 Message: "deadline exceeded", 233 Code: vtrpcpb.Code_DEADLINE_EXCEEDED, 234 }, 235 Result: nil, 236 }, 237 } 238 p3converted := QueryResponsesToProto3(queryResponses) 239 if !Proto3QueryResponsesEqual(p3converted, p3ResultWithError) { 240 t.Errorf("P3:\n%v, want\n%v", p3converted, p3ResultWithError) 241 } 242 243 reverse := Proto3ToQueryReponses(p3ResultWithError) 244 if !QueryResponsesEqual(reverse, queryResponses) { 245 t.Errorf("reverse:\n%#v, want\n%#v", reverse, queryResponses) 246 } 247 } 248 249 func TestProto3ValuesEqual(t *testing.T) { 250 for _, tc := range []struct { 251 v1, v2 []*querypb.Value 252 expected bool 253 }{ 254 { 255 v1: []*querypb.Value{ 256 { 257 Type: 0, 258 Value: []byte{0, 1}, 259 }, 260 }, 261 v2: []*querypb.Value{ 262 { 263 Type: 0, 264 Value: []byte{0, 1}, 265 }, 266 { 267 Type: 1, 268 Value: []byte{0, 1, 2}, 269 }, 270 }, 271 expected: false, 272 }, 273 { 274 v1: []*querypb.Value{ 275 { 276 Type: 0, 277 Value: []byte{0, 1}, 278 }, 279 { 280 Type: 1, 281 Value: []byte{0, 1, 2}, 282 }, 283 }, 284 v2: []*querypb.Value{ 285 { 286 Type: 0, 287 Value: []byte{0, 1}, 288 }, 289 { 290 Type: 1, 291 Value: []byte{0, 1, 2}, 292 }, 293 }, 294 expected: true, 295 }, 296 { 297 v1: []*querypb.Value{ 298 { 299 Type: 0, 300 Value: []byte{0, 1}, 301 }, 302 { 303 Type: 1, 304 Value: []byte{0, 1}, 305 }, 306 }, 307 v2: []*querypb.Value{ 308 { 309 Type: 0, 310 Value: []byte{0, 1}, 311 }, 312 { 313 Type: 1, 314 Value: []byte{0, 1, 2}, 315 }, 316 }, 317 expected: false, 318 }, 319 } { 320 require.Equal(t, tc.expected, Proto3ValuesEqual(tc.v1, tc.v2)) 321 } 322 }