2020-01-27 09:40:05 +00:00
|
|
|
package provider
|
2019-11-14 15:40:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
func TestAddInContext(t *testing.T) {
|
2019-11-14 15:40:05 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
ctx context.Context
|
|
|
|
name string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "without provider information",
|
|
|
|
ctx: context.Background(),
|
|
|
|
name: "test",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "provider name embedded in element name",
|
|
|
|
ctx: context.Background(),
|
|
|
|
name: "test@foo",
|
|
|
|
expected: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "provider name in context",
|
2020-01-27 09:40:05 +00:00
|
|
|
ctx: context.WithValue(context.Background(), key, "foo"),
|
2019-11-14 15:40:05 +00:00
|
|
|
name: "test",
|
|
|
|
expected: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "provider name in context and different provider name embedded in element name",
|
2020-01-27 09:40:05 +00:00
|
|
|
ctx: context.WithValue(context.Background(), key, "foo"),
|
2019-11-14 15:40:05 +00:00
|
|
|
name: "test@fii",
|
|
|
|
expected: "fii",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "provider name in context and same provider name embedded in element name",
|
2020-01-27 09:40:05 +00:00
|
|
|
ctx: context.WithValue(context.Background(), key, "foo"),
|
2019-11-14 15:40:05 +00:00
|
|
|
name: "test@foo",
|
|
|
|
expected: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-01-27 09:40:05 +00:00
|
|
|
newCtx := AddInContext(test.ctx, test.name)
|
2019-11-14 15:40:05 +00:00
|
|
|
|
|
|
|
var providerName string
|
2020-01-27 09:40:05 +00:00
|
|
|
if name, ok := newCtx.Value(key).(string); ok {
|
2019-11-14 15:40:05 +00:00
|
|
|
providerName = name
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, providerName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetQualifiedName(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
ctx context.Context
|
|
|
|
name string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "empty name",
|
|
|
|
ctx: context.Background(),
|
|
|
|
name: "",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "without provider",
|
|
|
|
ctx: context.Background(),
|
|
|
|
name: "test",
|
|
|
|
expected: "test",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with explicit provider",
|
|
|
|
ctx: context.Background(),
|
|
|
|
name: "test@foo",
|
|
|
|
expected: "test@foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with provider in context",
|
2020-01-27 09:40:05 +00:00
|
|
|
ctx: context.WithValue(context.Background(), key, "foo"),
|
2019-11-14 15:40:05 +00:00
|
|
|
name: "test",
|
|
|
|
expected: "test@foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with provider in context and explicit name",
|
2020-01-27 09:40:05 +00:00
|
|
|
ctx: context.WithValue(context.Background(), key, "foo"),
|
2019-11-14 15:40:05 +00:00
|
|
|
name: "test@fii",
|
|
|
|
expected: "test@fii",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
qualifiedName := GetQualifiedName(test.ctx, test.name)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, qualifiedName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|