|
| 1 | +// Copyright 2015, Google Inc. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package proto |
| 6 | + |
| 7 | +import ( |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/youtube/vitess/go/sqltypes" |
| 12 | + "github.com/youtube/vitess/go/vt/proto/query" |
| 13 | +) |
| 14 | + |
| 15 | +func TestFields(t *testing.T) { |
| 16 | + fields := []Field{{ |
| 17 | + Name: "aa", |
| 18 | + Type: 1, |
| 19 | + Flags: 32, |
| 20 | + }, { |
| 21 | + Name: "bb", |
| 22 | + Type: 2, |
| 23 | + }} |
| 24 | + p3, err := FieldsToProto3(fields) |
| 25 | + if err != nil { |
| 26 | + t.Error(err) |
| 27 | + } |
| 28 | + wantp3 := []*query.Field{ |
| 29 | + &query.Field{ |
| 30 | + Name: "aa", |
| 31 | + Type: sqltypes.Uint8, |
| 32 | + }, |
| 33 | + &query.Field{ |
| 34 | + Name: "bb", |
| 35 | + Type: sqltypes.Int16, |
| 36 | + }, |
| 37 | + } |
| 38 | + if !reflect.DeepEqual(p3, wantp3) { |
| 39 | + t.Errorf("p3: %v, want %v", p3, wantp3) |
| 40 | + } |
| 41 | + |
| 42 | + reverse := Proto3ToFields(p3) |
| 43 | + if !reflect.DeepEqual(reverse, fields) { |
| 44 | + t.Errorf("reverse: %v, want %v", reverse, fields) |
| 45 | + } |
| 46 | + |
| 47 | + fields = []Field{{ |
| 48 | + Name: "aa", |
| 49 | + Type: 15, |
| 50 | + }} |
| 51 | + _, err = FieldsToProto3(fields) |
| 52 | + want := "Could not map: 15 to a vitess type" |
| 53 | + if err == nil || err.Error() != want { |
| 54 | + t.Errorf("Error: %v, want %v", err, want) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestRowsToProto3(t *testing.T) { |
| 59 | + rows := [][]sqltypes.Value{{ |
| 60 | + sqltypes.MakeString([]byte("aa")), |
| 61 | + sqltypes.NULL, |
| 62 | + sqltypes.MakeString([]byte("12")), |
| 63 | + }, { |
| 64 | + sqltypes.MakeString([]byte("bb")), |
| 65 | + sqltypes.NULL, |
| 66 | + sqltypes.NULL, |
| 67 | + }} |
| 68 | + p3 := RowsToProto3(rows) |
| 69 | + want := []*query.Row{ |
| 70 | + &query.Row{ |
| 71 | + Lengths: []int64{2, -1, 2}, |
| 72 | + Values: []byte("aa12"), |
| 73 | + }, |
| 74 | + &query.Row{ |
| 75 | + Lengths: []int64{2, -1, -1}, |
| 76 | + Values: []byte("bb"), |
| 77 | + }, |
| 78 | + } |
| 79 | + if !reflect.DeepEqual(p3, want) { |
| 80 | + t.Errorf("P3: %v, want %v", p3, want) |
| 81 | + } |
| 82 | + |
| 83 | + reverse := Proto3ToRows(p3) |
| 84 | + if !reflect.DeepEqual(reverse, rows) { |
| 85 | + t.Errorf("reverse: \n%#v, want \n%#v", reverse, rows) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestInvalidRowsProto(t *testing.T) { |
| 90 | + p3 := []*query.Row{ |
| 91 | + &query.Row{ |
| 92 | + Lengths: []int64{3, 5, -1, 6}, |
| 93 | + Values: []byte("aa12"), |
| 94 | + }, |
| 95 | + } |
| 96 | + rows := Proto3ToRows(p3) |
| 97 | + want := [][]sqltypes.Value{{ |
| 98 | + sqltypes.MakeString([]byte("aa1")), |
| 99 | + sqltypes.NULL, |
| 100 | + sqltypes.NULL, |
| 101 | + sqltypes.NULL, |
| 102 | + }} |
| 103 | + if !reflect.DeepEqual(rows, want) { |
| 104 | + t.Errorf("reverse: \n%#v, want \n%#v", rows, want) |
| 105 | + } |
| 106 | + |
| 107 | + p3 = []*query.Row{ |
| 108 | + &query.Row{ |
| 109 | + Lengths: []int64{2, -2, 2}, |
| 110 | + Values: []byte("aa12"), |
| 111 | + }, |
| 112 | + } |
| 113 | + rows = Proto3ToRows(p3) |
| 114 | + want = [][]sqltypes.Value{{ |
| 115 | + sqltypes.MakeString([]byte("aa")), |
| 116 | + sqltypes.NULL, |
| 117 | + sqltypes.MakeString([]byte("12")), |
| 118 | + }} |
| 119 | + if !reflect.DeepEqual(rows, want) { |
| 120 | + t.Errorf("reverse: \n%#v, want \n%#v", rows, want) |
| 121 | + } |
| 122 | +} |
0 commit comments