github.com/blend/go-sdk@v1.20220411.3/configutil/env_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 configutil
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  	"github.com/blend/go-sdk/env"
    17  	"github.com/blend/go-sdk/uuid"
    18  )
    19  
    20  func createEnvVarsContext(key, value string) context.Context {
    21  	options := ConfigOptions{
    22  		Env: env.Vars{key: value},
    23  	}
    24  	return options.Background()
    25  }
    26  
    27  func emptyEnvVarsContext() context.Context {
    28  	return ConfigOptions{}.Background()
    29  }
    30  
    31  func TestEnv(t *testing.T) {
    32  	assert := assert.New(t)
    33  
    34  	key := uuid.V4().String()
    35  	ctx := emptyEnvVarsContext()
    36  
    37  	stringValue, err := Env(key).String(ctx)
    38  	assert.Nil(err)
    39  	assert.Nil(stringValue)
    40  
    41  	ctx = createEnvVarsContext(key, "foo")
    42  	assert.NotNil(env.GetVars(ctx))
    43  	assert.NotEmpty(env.GetVars(ctx))
    44  	assert.Equal("foo", env.GetVars(ctx).String(key))
    45  
    46  	stringValue, err = Env(key).String(ctx)
    47  	assert.Nil(err)
    48  	assert.NotNil(stringValue)
    49  	assert.Equal("foo", *stringValue)
    50  
    51  	ctx = emptyEnvVarsContext()
    52  	stringsValue, err := Env(key).Strings(ctx)
    53  	assert.Nil(err)
    54  	assert.Nil(stringsValue)
    55  
    56  	ctx = createEnvVarsContext(key, "foo,bar")
    57  	stringsValue, err = Env(key).Strings(ctx)
    58  	assert.Nil(err)
    59  	assert.NotEmpty(stringsValue)
    60  	assert.Equal([]string{"foo", "bar"}, stringsValue)
    61  
    62  	ctx = emptyEnvVarsContext()
    63  	boolValue, err := Env(key).Bool(ctx)
    64  	assert.Nil(err)
    65  	assert.Nil(boolValue)
    66  
    67  	ctx = createEnvVarsContext(key, "true")
    68  	boolValue, err = Env(key).Bool(ctx)
    69  	assert.Nil(err)
    70  	assert.NotNil(boolValue)
    71  	assert.Equal(true, *boolValue)
    72  
    73  	ctx = emptyEnvVarsContext()
    74  	intValue, err := Env(key).Int(ctx)
    75  	assert.Nil(err)
    76  	assert.Nil(intValue)
    77  
    78  	ctx = createEnvVarsContext(key, "bad value")
    79  	intValue, err = Env(key).Int(ctx)
    80  	assert.NotNil(err)
    81  	assert.Nil(intValue)
    82  
    83  	ctx = createEnvVarsContext(key, "4321")
    84  	intValue, err = Env(key).Int(ctx)
    85  	assert.Nil(err)
    86  	assert.NotNil(intValue)
    87  	assert.Equal(4321, *intValue)
    88  
    89  	ctx = emptyEnvVarsContext()
    90  	floatValue, err := Env(key).Float64(ctx)
    91  	assert.Nil(err)
    92  	assert.Nil(floatValue)
    93  
    94  	ctx = createEnvVarsContext(key, "bad value")
    95  	floatValue, err = Env(key).Float64(ctx)
    96  	assert.NotNil(err)
    97  	assert.Nil(floatValue)
    98  
    99  	ctx = createEnvVarsContext(key, "4321")
   100  	floatValue, err = Env(key).Float64(ctx)
   101  	assert.Nil(err)
   102  	assert.NotNil(floatValue)
   103  	assert.Equal(4321, *floatValue)
   104  
   105  	ctx = emptyEnvVarsContext()
   106  	durationValue, err := Env(key).Duration(ctx)
   107  	assert.Nil(err)
   108  	assert.Nil(durationValue)
   109  
   110  	ctx = createEnvVarsContext(key, "bad value")
   111  	durationValue, err = Env(key).Duration(ctx)
   112  	assert.NotNil(err)
   113  	assert.Nil(durationValue)
   114  
   115  	ctx = createEnvVarsContext(key, "10s")
   116  	durationValue, err = Env(key).Duration(ctx)
   117  	assert.Nil(err)
   118  	assert.NotNil(durationValue)
   119  	assert.Equal(10*time.Second, *durationValue)
   120  }