-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_lookup_test.go
154 lines (143 loc) · 4.21 KB
/
multi_lookup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package tempura_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/ebi-yade/go-tempura"
"github.com/stretchr/testify/assert"
)
func TestMultiLookup_Validate(t *testing.T) {
t.Parallel()
keyAsValue := func(val string) (string, bool) {
return val, true
}
fetchSecret := func(ctx context.Context, key string) (string, bool, error) {
return "XXXXXXXX", true, nil
}
always := func(val string) (string, bool, error) {
return "", false, fmt.Errorf("this function always returns an error")
}
tests := []struct {
name string
receiver *tempura.MultiLookup
checkErr func(t *testing.T, err error)
}{
// ==================== VALID CASES ====================
{
name: "single valid function",
receiver: &tempura.MultiLookup{
tempura.DotPrefix("env"): tempura.Func(os.LookupEnv),
},
checkErr: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
{
name: "multiple valid functions",
receiver: &tempura.MultiLookup{
tempura.DotPrefix("env"): tempura.Func(os.LookupEnv),
tempura.DotPrefix("default"): tempura.Func(keyAsValue),
tempura.DotPrefix("oops"): tempura.FuncWithError(always),
},
checkErr: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
// ==================== INVALID CASES ====================
{
name: "no functions registered",
receiver: &tempura.MultiLookup{},
checkErr: func(t *testing.T, err error) {
assert.ErrorIs(t, err, tempura.ErrNoFunctionRegistered)
},
},
{
name: "contains invalid function that receives context",
receiver: &tempura.MultiLookup{
tempura.DotPrefix("env"): tempura.Func(os.LookupEnv),
tempura.DotPrefix("default"): tempura.Func(keyAsValue),
tempura.DotPrefix("secret"): tempura.FuncWithContextError(fetchSecret),
},
checkErr: func(t *testing.T, err error) {
expected := tempura.InvalidFunctionError{}
assert.ErrorAs(t, err, &expected)
assert.Equal(t, "MultiLookup", expected.Type, "Type mismatch")
assert.Equal(t, tempura.DotPrefix("secret"), expected.Prefix, "Prefix mismatch")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.receiver.Validate()
tt.checkErr(t, err)
})
}
}
func TestMultiLookupContext_Validate(t *testing.T) {
t.Parallel()
keyAsValue := func(val string) (string, bool) {
return val, true
}
fetchSecret := func(ctx context.Context, key string) (string, bool, error) {
return "XXXXXXXX", true, nil
}
always := func(val string) (string, bool, error) {
return "", false, fmt.Errorf("this function always returns an error")
}
ctx := context.TODO()
tests := []struct {
name string
receiver *tempura.MultiLookupContext
checkErr func(t *testing.T, err error)
}{
// ==================== VALID CASES ====================
{
name: "single valid function that receives context",
receiver: tempura.MultiLookup{
tempura.DotPrefix("secret"): tempura.FuncWithContextError(fetchSecret),
}.BindContext(ctx),
checkErr: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
{
name: "multiple valid functions",
receiver: tempura.MultiLookup{
tempura.DotPrefix("env"): tempura.Func(os.LookupEnv),
tempura.DotPrefix("default"): tempura.Func(keyAsValue),
tempura.DotPrefix("oops"): tempura.FuncWithError(always),
tempura.DotPrefix("secret"): tempura.FuncWithContextError(fetchSecret),
}.BindContext(ctx),
checkErr: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
// ==================== INVALID CASES ====================
{
name: "no functions registered",
receiver: tempura.MultiLookup{}.BindContext(ctx),
checkErr: func(t *testing.T, err error) {
assert.ErrorIs(t, err, tempura.ErrNoFunctionRegistered)
},
},
{
name: "forget to call BindContext",
receiver: &tempura.MultiLookupContext{
MultiLookup: tempura.MultiLookup{
tempura.DotPrefix("secret"): tempura.FuncWithContextError(fetchSecret),
},
//Ctx is nil
},
checkErr: func(t *testing.T, err error) {
assert.ErrorIs(t, err, tempura.ErrContextUntypedNil)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.receiver.Validate()
tt.checkErr(t, err)
})
}
}