github.com/m3db/m3@v1.5.0/src/cluster/kv/util/util_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 util 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/cluster/generated/proto/commonpb" 28 "github.com/m3db/m3/src/cluster/kv" 29 "github.com/m3db/m3/src/cluster/kv/mem" 30 31 "github.com/stretchr/testify/require" 32 ) 33 34 func TestBoolFromValue(t *testing.T) { 35 defaultValue := true 36 37 tests := []struct { 38 input kv.Value 39 expectedErr bool 40 expectedVal bool 41 }{ 42 { 43 input: mem.NewValue(0, &commonpb.BoolProto{Value: true}), 44 expectedErr: false, 45 expectedVal: true, 46 }, 47 { 48 input: mem.NewValue(0, &commonpb.BoolProto{Value: false}), 49 expectedErr: false, 50 expectedVal: false, 51 }, 52 { 53 input: nil, 54 expectedErr: false, 55 expectedVal: defaultValue, 56 }, 57 { 58 input: mem.NewValue(0, &commonpb.Float64Proto{Value: 123}), 59 expectedErr: true, 60 }, 61 } 62 63 for _, test := range tests { 64 v, err := BoolFromValue(test.input, "key", defaultValue, nil) 65 if test.expectedErr { 66 require.Error(t, err) 67 continue 68 } 69 require.NoError(t, err) 70 require.Equal(t, test.expectedVal, v) 71 } 72 73 // Invalid updates should return an error. 74 opts := NewOptions().SetValidateFn(testValidateBoolFn) 75 _, err := BoolFromValue( 76 mem.NewValue(0, &commonpb.BoolProto{Value: false}), "key", defaultValue, opts, 77 ) 78 require.Error(t, err) 79 } 80 81 func TestFloat64FromValue(t *testing.T) { 82 defaultValue := 3.7 83 84 tests := []struct { 85 input kv.Value 86 expectedErr bool 87 expectedVal float64 88 }{ 89 { 90 input: mem.NewValue(0, &commonpb.Float64Proto{Value: 0}), 91 expectedErr: false, 92 expectedVal: 0, 93 }, 94 { 95 input: mem.NewValue(0, &commonpb.Float64Proto{Value: 13.2}), 96 expectedErr: false, 97 expectedVal: 13.2, 98 }, 99 { 100 input: nil, 101 expectedErr: false, 102 expectedVal: defaultValue, 103 }, 104 { 105 input: mem.NewValue(0, &commonpb.Int64Proto{Value: 123}), 106 expectedErr: true, 107 }, 108 } 109 110 for _, test := range tests { 111 v, err := Float64FromValue(test.input, "key", defaultValue, nil) 112 if test.expectedErr { 113 require.Error(t, err) 114 continue 115 } 116 require.NoError(t, err) 117 require.Equal(t, test.expectedVal, v) 118 } 119 120 // Invalid updates should return an error. 121 opts := NewOptions().SetValidateFn(testValidateBoolFn) 122 _, err := Float64FromValue( 123 mem.NewValue(0, &commonpb.Float64Proto{Value: 1.24}), "key", defaultValue, opts, 124 ) 125 require.Error(t, err) 126 } 127 128 func TestInt64FromValue(t *testing.T) { 129 var defaultValue int64 = 5 130 131 tests := []struct { 132 input kv.Value 133 expectedErr bool 134 expectedVal int64 135 }{ 136 { 137 input: mem.NewValue(0, &commonpb.Int64Proto{Value: 0}), 138 expectedErr: false, 139 expectedVal: 0, 140 }, 141 { 142 input: mem.NewValue(0, &commonpb.Int64Proto{Value: 13}), 143 expectedErr: false, 144 expectedVal: 13, 145 }, 146 { 147 input: nil, 148 expectedErr: false, 149 expectedVal: defaultValue, 150 }, 151 { 152 input: mem.NewValue(0, &commonpb.Float64Proto{Value: 1.23}), 153 expectedErr: true, 154 }, 155 } 156 157 for _, test := range tests { 158 v, err := Int64FromValue(test.input, "key", defaultValue, nil) 159 if test.expectedErr { 160 require.Error(t, err) 161 continue 162 } 163 require.NoError(t, err) 164 require.Equal(t, test.expectedVal, v) 165 } 166 167 // Invalid updates should return an error. 168 opts := NewOptions().SetValidateFn(testValidateInt64Fn) 169 _, err := Int64FromValue( 170 mem.NewValue(0, &commonpb.Int64Proto{Value: 22}), "key", defaultValue, opts, 171 ) 172 require.Error(t, err) 173 } 174 175 func TestTimeFromValue(t *testing.T) { 176 var ( 177 zero = time.Time{} 178 defaultValue = time.Now() 179 customValue = defaultValue.Add(time.Minute) 180 ) 181 182 tests := []struct { 183 input kv.Value 184 expectedErr bool 185 expectedVal time.Time 186 }{ 187 { 188 input: mem.NewValue(0, &commonpb.Int64Proto{Value: zero.Unix()}), 189 expectedErr: false, 190 expectedVal: zero, 191 }, 192 { 193 input: mem.NewValue(0, &commonpb.Int64Proto{Value: customValue.Unix()}), 194 expectedErr: false, 195 expectedVal: customValue, 196 }, 197 { 198 input: nil, 199 expectedErr: false, 200 expectedVal: defaultValue, 201 }, 202 { 203 input: mem.NewValue(0, &commonpb.Float64Proto{Value: 1.23}), 204 expectedErr: true, 205 }, 206 } 207 208 for _, test := range tests { 209 v, err := TimeFromValue(test.input, "key", defaultValue, nil) 210 if test.expectedErr { 211 require.Error(t, err) 212 continue 213 } 214 require.NoError(t, err) 215 require.Equal(t, test.expectedVal.Unix(), v.Unix()) 216 } 217 218 // Invalid updates should return an error. 219 opts := NewOptions().SetValidateFn(testValidateTimeFn) 220 _, err := TimeFromValue( 221 mem.NewValue(0, &commonpb.Int64Proto{Value: testNow.Add(time.Hour).Unix()}), 222 "key", 223 defaultValue, 224 opts, 225 ) 226 require.Error(t, err) 227 } 228 229 func TestStringFromValue(t *testing.T) { 230 defaultValue := "bcd" 231 232 tests := []struct { 233 input kv.Value 234 expectedErr bool 235 expectedVal string 236 }{ 237 { 238 input: mem.NewValue(0, &commonpb.StringProto{Value: ""}), 239 expectedErr: false, 240 expectedVal: "", 241 }, 242 { 243 input: mem.NewValue(0, &commonpb.StringProto{Value: "foo"}), 244 expectedErr: false, 245 expectedVal: "foo", 246 }, 247 { 248 input: nil, 249 expectedErr: false, 250 expectedVal: defaultValue, 251 }, 252 { 253 input: mem.NewValue(0, &commonpb.Float64Proto{Value: 1.23}), 254 expectedErr: true, 255 }, 256 } 257 258 for _, test := range tests { 259 v, err := StringFromValue(test.input, "key", defaultValue, nil) 260 if test.expectedErr { 261 require.Error(t, err) 262 continue 263 } 264 require.NoError(t, err) 265 require.Equal(t, test.expectedVal, v) 266 } 267 268 // Invalid updates should return an error. 269 opts := NewOptions().SetValidateFn(testValidateStringFn) 270 _, err := StringFromValue( 271 mem.NewValue(0, &commonpb.StringProto{Value: "abc"}), "key", defaultValue, opts, 272 ) 273 require.Error(t, err) 274 } 275 276 func TestStringArrayFromValue(t *testing.T) { 277 defaultValue := []string{"a", "b"} 278 279 tests := []struct { 280 input kv.Value 281 expectedErr bool 282 expectedVal []string 283 }{ 284 { 285 input: mem.NewValue(0, &commonpb.StringArrayProto{Values: nil}), 286 expectedErr: false, 287 expectedVal: nil, 288 }, 289 { 290 input: mem.NewValue(0, &commonpb.StringArrayProto{Values: []string{"foo", "bar"}}), 291 expectedErr: false, 292 expectedVal: []string{"foo", "bar"}, 293 }, 294 { 295 input: nil, 296 expectedErr: false, 297 expectedVal: defaultValue, 298 }, 299 { 300 input: mem.NewValue(0, &commonpb.Float64Proto{Value: 1.23}), 301 expectedErr: true, 302 }, 303 } 304 305 for _, test := range tests { 306 v, err := StringArrayFromValue(test.input, "key", defaultValue, nil) 307 if test.expectedErr { 308 require.Error(t, err) 309 continue 310 } 311 require.NoError(t, err) 312 require.Equal(t, test.expectedVal, v) 313 } 314 315 // Invalid updates should return an error. 316 opts := NewOptions().SetValidateFn(testValidateStringArrayFn) 317 _, err := StringArrayFromValue( 318 mem.NewValue(0, &commonpb.StringArrayProto{Values: []string{"abc"}}), "key", defaultValue, opts, 319 ) 320 require.Error(t, err) 321 }