github.com/blend/go-sdk@v1.20220411.3/web/value_parsers_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package web 9 10 import ( 11 "fmt" 12 "testing" 13 "time" 14 15 "github.com/blend/go-sdk/assert" 16 "github.com/blend/go-sdk/uuid" 17 ) 18 19 func Test_BoolValue(t *testing.T) { 20 its := assert.New(t) 21 22 var value bool 23 var err error 24 25 testErr := fmt.Errorf("test error") 26 value, err = BoolValue("", testErr) 27 its.Equal(testErr, err) 28 its.False(value) 29 30 trueValues := []string{"1", "true", "yes", "on"} 31 for _, tv := range trueValues { 32 value, err = BoolValue(tv, nil) 33 its.Nil(err) 34 its.True(value) 35 } 36 37 falseValues := []string{"0", "false", "no", "off"} 38 for _, tv := range falseValues { 39 value, err = BoolValue(tv, nil) 40 its.Nil(err) 41 its.False(value) 42 } 43 44 value, err = BoolValue("garbage", nil) 45 its.Equal(ErrInvalidBoolValue, err) 46 its.False(value) 47 } 48 49 func Test_IntValue(t *testing.T) { 50 its := assert.New(t) 51 52 var value int 53 var err error 54 55 testErr := fmt.Errorf("test error") 56 value, err = IntValue("", testErr) 57 its.Equal(testErr, err) 58 its.Zero(value) 59 60 value, err = IntValue("1234", nil) 61 its.Nil(err) 62 its.Equal(1234, value) 63 64 value, err = IntValue("garbage", nil) 65 its.NotNil(err) 66 its.Zero(value) 67 } 68 69 func Test_Int64Value(t *testing.T) { 70 its := assert.New(t) 71 72 var value int64 73 var err error 74 75 testErr := fmt.Errorf("test error") 76 value, err = Int64Value("", testErr) 77 its.Equal(testErr, err) 78 its.Zero(value) 79 80 value, err = Int64Value("1234", nil) 81 its.Nil(err) 82 its.Equal(1234, value) 83 84 value, err = Int64Value("garbage", nil) 85 its.NotNil(err) 86 its.Zero(value) 87 } 88 89 func Test_Float64Value(t *testing.T) { 90 its := assert.New(t) 91 92 var value float64 93 var err error 94 95 testErr := fmt.Errorf("test error") 96 value, err = Float64Value("", testErr) 97 its.Equal(testErr, err) 98 its.Zero(value) 99 100 value, err = Float64Value("1234.56", nil) 101 its.Nil(err) 102 its.Equal(1234.56, value) 103 104 value, err = Float64Value("garbage", nil) 105 its.NotNil(err) 106 its.Zero(value) 107 } 108 109 func Test_DurationValue(t *testing.T) { 110 its := assert.New(t) 111 112 var value time.Duration 113 var err error 114 115 testErr := fmt.Errorf("test error") 116 value, err = DurationValue("", testErr) 117 its.Equal(testErr, err) 118 its.Zero(value) 119 120 value, err = DurationValue("10s", nil) 121 its.Nil(err) 122 its.Equal(10*time.Second, value) 123 124 value, err = DurationValue("garbage", nil) 125 its.NotNil(err) 126 its.Zero(value) 127 } 128 129 func Test_StringValue(t *testing.T) { 130 its := assert.New(t) 131 132 var value string 133 testErr := fmt.Errorf("test error") 134 value = StringValue("foo", testErr) 135 its.Equal("foo", value) 136 } 137 138 func Test_CSVValue(t *testing.T) { 139 its := assert.New(t) 140 141 var value []string 142 var err error 143 144 testErr := fmt.Errorf("test error") 145 value, err = CSVValue("", testErr) 146 its.Equal(testErr, err) 147 its.Empty(value) 148 149 value, err = CSVValue("foo,bar", nil) 150 its.Nil(err) 151 its.Equal([]string{"foo", "bar"}, value) 152 } 153 154 func Test_UUIDValue(t *testing.T) { 155 its := assert.New(t) 156 157 var value uuid.UUID 158 var err error 159 160 testErr := fmt.Errorf("test error") 161 value, err = UUIDValue("", testErr) 162 its.Equal(testErr, err) 163 its.Empty(value) 164 165 uid := uuid.V4().String() 166 value, err = UUIDValue(uid, nil) 167 its.Nil(err) 168 its.Equal(uid, value.String()) 169 170 value, err = UUIDValue("bogus uid", nil) 171 its.NotNil(err) 172 its.Empty(value) 173 }