vitess.io/vitess@v0.16.2/go/sqltypes/result_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 "vitess.io/vitess/go/test/utils" 23 24 querypb "vitess.io/vitess/go/vt/proto/query" 25 ) 26 27 func TestRepair(t *testing.T) { 28 fields := []*querypb.Field{{ 29 Type: Int64, 30 }, { 31 Type: VarChar, 32 }} 33 in := &Result{ 34 Rows: [][]Value{ 35 {TestValue(VarBinary, "1"), TestValue(VarBinary, "aa")}, 36 {TestValue(VarBinary, "2"), TestValue(VarBinary, "bb")}, 37 }, 38 } 39 want := &Result{ 40 Rows: [][]Value{ 41 {TestValue(Int64, "1"), TestValue(VarChar, "aa")}, 42 {TestValue(Int64, "2"), TestValue(VarChar, "bb")}, 43 }, 44 } 45 in.Repair(fields) 46 if !in.Equal(want) { 47 t.Errorf("Repair:\n%#v, want\n%#v", in, want) 48 } 49 } 50 51 func TestCopy(t *testing.T) { 52 in := &Result{ 53 Fields: []*querypb.Field{{ 54 Type: Int64, 55 }, { 56 Type: VarChar, 57 }}, 58 InsertID: 1, 59 RowsAffected: 2, 60 Rows: [][]Value{ 61 {TestValue(Int64, "1"), MakeTrusted(Null, nil)}, 62 {TestValue(Int64, "2"), MakeTrusted(VarChar, nil)}, 63 {TestValue(Int64, "3"), TestValue(VarChar, "")}, 64 }, 65 } 66 out := in.Copy() 67 utils.MustMatch(t, in, out) 68 } 69 70 func TestTruncate(t *testing.T) { 71 in := &Result{ 72 Fields: []*querypb.Field{{ 73 Type: Int64, 74 }, { 75 Type: VarChar, 76 }}, 77 InsertID: 1, 78 RowsAffected: 2, 79 Rows: [][]Value{ 80 {TestValue(Int64, "1"), MakeTrusted(Null, nil)}, 81 {TestValue(Int64, "2"), MakeTrusted(VarChar, nil)}, 82 {TestValue(Int64, "3"), TestValue(VarChar, "")}, 83 }, 84 } 85 86 out := in.Truncate(0) 87 if !out.Equal(in) { 88 t.Errorf("Truncate(0):\n%v, want\n%v", out, in) 89 } 90 91 out = in.Truncate(1) 92 want := &Result{ 93 Fields: []*querypb.Field{{ 94 Type: Int64, 95 }}, 96 InsertID: 1, 97 RowsAffected: 2, 98 Rows: [][]Value{ 99 {TestValue(Int64, "1")}, 100 {TestValue(Int64, "2")}, 101 {TestValue(Int64, "3")}, 102 }, 103 } 104 if !out.Equal(want) { 105 t.Errorf("Truncate(1):\n%v, want\n%v", out, want) 106 } 107 } 108 109 func TestStripMetaData(t *testing.T) { 110 testcases := []struct { 111 name string 112 in *Result 113 expected *Result 114 includedFields querypb.ExecuteOptions_IncludedFields 115 }{{ 116 name: "no fields", 117 in: &Result{}, 118 expected: &Result{}, 119 }, { 120 name: "empty fields", 121 in: &Result{ 122 Fields: []*querypb.Field{}, 123 }, 124 expected: &Result{ 125 Fields: []*querypb.Field{}, 126 }, 127 }, { 128 name: "no name", 129 includedFields: querypb.ExecuteOptions_TYPE_ONLY, 130 in: &Result{ 131 Fields: []*querypb.Field{{ 132 Type: Int64, 133 }, { 134 Type: VarChar, 135 }}, 136 }, 137 expected: &Result{ 138 Fields: []*querypb.Field{{ 139 Type: Int64, 140 }, { 141 Type: VarChar, 142 }}, 143 }, 144 }, { 145 name: "names", 146 includedFields: querypb.ExecuteOptions_TYPE_ONLY, 147 in: &Result{ 148 Fields: []*querypb.Field{{ 149 Name: "field1", 150 Type: Int64, 151 }, { 152 Name: "field2", 153 Type: VarChar, 154 }}, 155 }, 156 expected: &Result{ 157 Fields: []*querypb.Field{{ 158 Type: Int64, 159 }, { 160 Type: VarChar, 161 }}, 162 }, 163 }, { 164 name: "all fields - strip to type", 165 includedFields: querypb.ExecuteOptions_TYPE_ONLY, 166 in: &Result{ 167 Fields: []*querypb.Field{{ 168 Name: "field1", 169 Table: "table1", 170 OrgTable: "orgtable1", 171 OrgName: "orgname1", 172 ColumnLength: 5, 173 Charset: 63, 174 Decimals: 0, 175 Flags: 2, 176 Type: Int64, 177 }, { 178 Name: "field2", 179 Table: "table2", 180 OrgTable: "orgtable2", 181 OrgName: "orgname2", 182 ColumnLength: 5, 183 Charset: 63, 184 Decimals: 0, 185 Flags: 2, 186 Type: VarChar, 187 }}, 188 }, 189 expected: &Result{ 190 Fields: []*querypb.Field{{ 191 Type: Int64, 192 }, { 193 Type: VarChar, 194 }}, 195 }, 196 }, { 197 name: "all fields - not stripped", 198 includedFields: querypb.ExecuteOptions_ALL, 199 in: &Result{ 200 Fields: []*querypb.Field{{ 201 Name: "field1", 202 Table: "table1", 203 OrgTable: "orgtable1", 204 OrgName: "orgname1", 205 ColumnLength: 5, 206 Charset: 63, 207 Decimals: 0, 208 Flags: 2, 209 Type: Int64, 210 }, { 211 Name: "field2", 212 Table: "table2", 213 OrgTable: "orgtable2", 214 OrgName: "orgname2", 215 ColumnLength: 5, 216 Charset: 63, 217 Decimals: 0, 218 Flags: 2, 219 Type: VarChar, 220 }}, 221 }, 222 expected: &Result{ 223 Fields: []*querypb.Field{{ 224 Name: "field1", 225 Table: "table1", 226 OrgTable: "orgtable1", 227 OrgName: "orgname1", 228 ColumnLength: 5, 229 Charset: 63, 230 Decimals: 0, 231 Flags: 2, 232 Type: Int64, 233 }, { 234 Name: "field2", 235 Table: "table2", 236 OrgTable: "orgtable2", 237 OrgName: "orgname2", 238 ColumnLength: 5, 239 Charset: 63, 240 Decimals: 0, 241 Flags: 2, 242 Type: VarChar, 243 }}, 244 }, 245 }, { 246 name: "all fields - strip to type and name", 247 in: &Result{ 248 Fields: []*querypb.Field{{ 249 Name: "field1", 250 Table: "table1", 251 OrgTable: "orgtable1", 252 OrgName: "orgname1", 253 ColumnLength: 5, 254 Charset: 63, 255 Decimals: 0, 256 Flags: 2, 257 Type: Int64, 258 }, { 259 Name: "field2", 260 Table: "table2", 261 OrgTable: "orgtable2", 262 OrgName: "orgname2", 263 ColumnLength: 5, 264 Charset: 63, 265 Decimals: 0, 266 Flags: 2, 267 Type: VarChar, 268 }}, 269 }, 270 expected: &Result{ 271 Fields: []*querypb.Field{{ 272 Name: "field1", 273 Type: Int64, 274 }, { 275 Name: "field2", 276 Type: VarChar, 277 }}, 278 }, 279 }} 280 for _, tcase := range testcases { 281 t.Run(tcase.name, func(t *testing.T) { 282 inCopy := tcase.in.Copy() 283 out := inCopy.StripMetadata(tcase.includedFields) 284 if !out.Equal(tcase.expected) { 285 t.Errorf("StripMetaData unexpected result for %v: %v", tcase.name, out) 286 } 287 if len(tcase.in.Fields) > 0 { 288 // check the out array is different than the in array. 289 if out.Fields[0] == inCopy.Fields[0] && tcase.includedFields != querypb.ExecuteOptions_ALL { 290 t.Errorf("StripMetaData modified original Field for %v", tcase.name) 291 } 292 } 293 // check we didn't change the original result. 294 utils.MustMatch(t, tcase.in, inCopy) 295 }) 296 } 297 } 298 299 func TestAppendResult(t *testing.T) { 300 src := &Result{ 301 Fields: []*querypb.Field{{ 302 Type: Int64, 303 }, { 304 Type: VarChar, 305 }}, 306 InsertID: 1, 307 RowsAffected: 2, 308 Rows: [][]Value{ 309 {TestValue(Int64, "2"), MakeTrusted(VarChar, nil)}, 310 {TestValue(Int64, "3"), TestValue(VarChar, "")}, 311 }, 312 } 313 314 result := &Result{ 315 Fields: []*querypb.Field{{ 316 Type: Int64, 317 }, { 318 Type: VarChar, 319 }}, 320 InsertID: 3, 321 RowsAffected: 4, 322 Rows: [][]Value{ 323 {TestValue(Int64, "1"), MakeTrusted(Null, nil)}, 324 }, 325 } 326 327 want := &Result{ 328 Fields: []*querypb.Field{{ 329 Type: Int64, 330 }, { 331 Type: VarChar, 332 }}, 333 InsertID: 1, 334 RowsAffected: 6, 335 Rows: [][]Value{ 336 {TestValue(Int64, "1"), MakeTrusted(Null, nil)}, 337 {TestValue(Int64, "2"), MakeTrusted(VarChar, nil)}, 338 {TestValue(Int64, "3"), TestValue(VarChar, "")}, 339 }, 340 } 341 342 result.AppendResult(src) 343 344 if !result.Equal(want) { 345 t.Errorf("Got:\n%#v, want:\n%#v", result, want) 346 } 347 }