Skip to content

Commit

Permalink
Added test for env opts changes
Browse files Browse the repository at this point in the history
  • Loading branch information
karantin2020 committed Jul 28, 2018
1 parent f60cd99 commit e695ff9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
88 changes: 87 additions & 1 deletion backend/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package env
import (
"context"
"os"
"reflect"
"testing"

"github.com/heetch/confita/backend"
"github.com/karantin2020/confita/backend"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -35,3 +36,88 @@ func TestEnvBackend(t *testing.T) {
require.Equal(t, "ok", string(val))
})
}

func TestOpts(t *testing.T) {
type args struct {
fns []opt
key string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test opts positive",
args: args{
fns: []opt{
ToUpper(),
WithPreffix("TEST_"),
},
key: "var",
},
want: "TEST_VAR",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := opts(tt.args.key, tt.args.fns...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewBackend() = %v, want %v", got, tt.want)
}
})
}
}

func TestWithPreffix(t *testing.T) {
type args struct {
preffix string
key string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test add preffix positive",
args: args{
preffix: "TEST_",
key: "VAR",
},
want: "TEST_VAR",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := WithPreffix(tt.args.preffix)(tt.args.key); !reflect.DeepEqual(got, tt.want) {
t.Errorf("WithPreffix() = %v, want %v", got, tt.want)
}
})
}
}

func TestToUpper(t *testing.T) {
type args struct {
key string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test add preffix positive",
args: args{
"test",
},
want: "TEST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToUpper()(tt.args.key); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToUpper() = %v, want %v", got, tt.want)
}
})
}
}
21 changes: 21 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"encoding/json"
"math"
"os"
"strconv"
"testing"
"time"

"github.com/heetch/confita"
"github.com/heetch/confita/backend"
"github.com/heetch/confita/backend/env"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -169,6 +171,25 @@ func TestLoadRequired(t *testing.T) {
require.Error(t, err)
}

func TestLoadEnvOpts(t *testing.T) {
s := struct {
Name string `config:"name"`
}{
Name: "yepp",
}

os.Setenv("TEST_NAME", "yepp")
loader := confita.NewLoader(
env.NewBackend(
env.ToUpper(),
env.WithPreffix("TEST_"),
),
)
err := loader.Load(context.Background(), &s)
require.NoError(t, err)
require.Equal(t, "yepp", s.Name)
}

func TestLoadIgnored(t *testing.T) {
s := struct {
Name string `config:"-"`
Expand Down

0 comments on commit e695ff9

Please sign in to comment.