github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/yarpc/helpers_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package yarpc 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/uber-go/dosa" 28 dosarpc "github.com/uber/dosa-idl/.gen/dosa" 29 ) 30 31 var ( 32 tableName = "testentity" 33 uuidKeyField = "uuidkeyfield" 34 uuidField = "uuidfield" 35 stringKeyField = "stringkeyfield" 36 stringField = "stringfield" 37 int32Field = "int32field" 38 int64KeyField = "int64keyfield" 39 int64Field = "int64field" 40 doubleField = "doublefield" 41 blobField = "blobfield" 42 timestampField = "timestampfield" 43 boolField = "boolfield" 44 ) 45 46 func TestRawValueFromInterfaceBadType(t *testing.T) { 47 assert.Panics(t, func() { 48 RawValueFromInterface(func() {}) 49 }) 50 } 51 52 func TestRawValueAsInterfaceBadType(t *testing.T) { 53 assert.Panics(t, func() { 54 RawValueAsInterface(dosarpc.RawValue{}, dosa.Invalid) 55 }) 56 } 57 58 func TestRPCTypeFromClientType(t *testing.T) { 59 assert.Panics(t, func() { 60 RPCTypeFromClientType(dosa.Invalid) 61 }) 62 } 63 64 func TestRawValueFromInterfaceNilBlob(t *testing.T) { 65 var blob []byte 66 raw, _ := RawValueFromInterface(blob) 67 _, err := raw.ToWire() 68 assert.NoError(t, err) 69 } 70 71 func TestRawValueConversionError(t *testing.T) { 72 data := []struct { 73 input interface{} 74 errmsg string 75 }{ 76 {dosa.UUID(""), "short"}, // empty string 77 {dosa.UUID("1"), "short"}, 78 {dosa.UUID("this is not a uuid, uuids shouldnt contain something like a t in them"), "invalid byte"}, 79 } 80 81 for _, test := range data { 82 _, err := RawValueFromInterface(test.input) 83 assert.Error(t, err, "test %+v", test) 84 assert.Contains(t, err.Error(), test.errmsg, "test %+v", test) 85 } 86 87 // happy path 88 v, err := RawValueFromInterface(dosa.UUID("80bccd66-9517-4f54-9dec-0ddb87d0dc2a")) 89 assert.NoError(t, err) 90 assert.NotNil(t, v) 91 } 92 93 // TODO: add additional happy path unit tests here. The helpers currently get 94 // good coverage from the connectors though. 95 96 var testEntityDefinition = &dosa.EntityDefinition{ 97 Name: tableName, 98 Key: &dosa.PrimaryKey{ 99 PartitionKeys: []string{uuidKeyField}, 100 ClusteringKeys: []*dosa.ClusteringKey{ 101 { 102 Name: stringKeyField, 103 Descending: false, 104 }, 105 { 106 Name: int64KeyField, 107 Descending: true, 108 }, 109 }, 110 }, 111 Indexes: map[string]*dosa.IndexDefinition{ 112 "index1": { 113 Key: &dosa.PrimaryKey{ 114 PartitionKeys: []string{uuidField}, 115 ClusteringKeys: []*dosa.ClusteringKey{ 116 { 117 Name: stringKeyField, 118 Descending: false, 119 }, 120 }, 121 }, 122 }, 123 "index2": { 124 Key: &dosa.PrimaryKey{ 125 PartitionKeys: []string{int64Field, uuidKeyField}, 126 ClusteringKeys: []*dosa.ClusteringKey{ 127 { 128 Name: stringKeyField, 129 Descending: false, 130 }, 131 }, 132 }, 133 }, 134 }, 135 Columns: []*dosa.ColumnDefinition{ 136 { 137 Name: uuidKeyField, 138 Type: dosa.TUUID, 139 }, 140 { 141 Name: stringKeyField, 142 Type: dosa.String, 143 }, 144 { 145 Name: int64KeyField, 146 Type: dosa.Int64, 147 }, 148 149 { 150 Name: int32Field, 151 Type: dosa.Int32, 152 }, 153 { 154 Name: doubleField, 155 Type: dosa.Double, 156 }, 157 { 158 Name: blobField, 159 Type: dosa.Blob, 160 }, 161 { 162 Name: timestampField, 163 Type: dosa.Timestamp, 164 }, 165 { 166 Name: boolField, 167 Type: dosa.Bool, 168 }, 169 { 170 Name: uuidField, 171 Type: dosa.TUUID, 172 }, 173 { 174 Name: stringField, 175 Type: dosa.String, 176 }, 177 { 178 Name: int64Field, 179 Type: dosa.Int64, 180 }, 181 }, 182 } 183 184 func TestEntityDefinitionConvert(t *testing.T) { 185 rpcEd := EntityDefinitionToThrift(testEntityDefinition) 186 ed := FromThriftToEntityDefinition(rpcEd) 187 assert.Equal(t, testEntityDefinition.Key, ed.Key) 188 assert.Equal(t, testEntityDefinition.Name, ed.Name) 189 assert.Equal(t, testEntityDefinition.Indexes, ed.Indexes) 190 edCols := make(map[string]*dosa.ColumnDefinition) 191 for _, c := range ed.Columns { 192 edCols[c.Name] = c 193 } 194 195 testCols := make(map[string]*dosa.ColumnDefinition) 196 for _, c := range testEntityDefinition.Columns { 197 testCols[c.Name] = c 198 } 199 assert.Equal(t, edCols, testCols) 200 } 201 202 func TestEncodeOperator(t *testing.T) { 203 data := []struct { 204 dop dosa.Operator 205 rpcop dosarpc.Operator 206 }{ 207 {dop: dosa.Eq, rpcop: dosarpc.OperatorEq}, 208 {dop: dosa.Gt, rpcop: dosarpc.OperatorGt}, 209 {dop: dosa.Lt, rpcop: dosarpc.OperatorLt}, 210 {dop: dosa.LtOrEq, rpcop: dosarpc.OperatorLtOrEq}, 211 {dop: dosa.GtOrEq, rpcop: dosarpc.OperatorGtOrEq}, 212 } 213 214 for _, test := range data { 215 assert.Equal(t, test.rpcop, *encodeOperator(test.dop)) 216 } 217 }