micro/options/options_test.go

162 lines
2.7 KiB
Go
Raw Normal View History

package options_test
import (
2024-02-28 15:06:29 +03:00
"fmt"
"testing"
"go.unistack.org/micro/v4/codec"
2024-02-28 15:06:29 +03:00
"go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/options"
2024-02-28 15:06:29 +03:00
"go.unistack.org/micro/v4/util/reflect"
)
func TestAddress(t *testing.T) {
var err error
type s struct {
Address []string
}
src := &s{}
var opts []options.Option
opts = append(opts, options.Address("host:port"))
for _, o := range opts {
if err = o(src); err != nil {
t.Fatal(err)
}
}
if src.Address[0] != "host:port" {
t.Fatal("failed to set Address")
}
}
func TestCodecs(t *testing.T) {
var err error
type s struct {
Codecs map[string]codec.Codec
}
src := &s{}
var opts []options.Option
c := codec.NewCodec()
opts = append(opts, options.Codecs("text/html", c))
for _, o := range opts {
if err = o(src); err != nil {
t.Fatal(err)
}
}
for k, v := range src.Codecs {
if k != "text/html" || v != c {
continue
}
return
}
t.Fatalf("failed to set Codecs")
}
func TestLabels(t *testing.T) {
type str1 struct {
Labels []string
}
type str2 struct {
Labels []interface{}
}
x1 := &str1{}
if err := options.Labels("one", "two")(x1); err != nil {
t.Fatal(err)
}
if len(x1.Labels) != 2 {
t.Fatal("failed to set labels")
}
x2 := &str2{}
if err := options.Labels("key", "val")(x2); err != nil {
t.Fatal(err)
}
if len(x2.Labels) != 2 {
t.Fatal("failed to set labels")
}
if x2.Labels[0] != "key" {
t.Fatal("failed to set labels")
}
}
2024-02-28 15:06:29 +03:00
func TestMetadataAny(t *testing.T) {
type s struct {
Metadata metadata.Metadata
}
testCases := []struct {
Name string
Data any
Expected metadata.Metadata
}{
{
"strings_even",
[]any{"key1", "val1", "key2", "val2"},
2024-02-28 15:06:29 +03:00
metadata.Metadata{
"Key1": "val1",
"Key2": "val2",
},
},
{
"strings_odd",
[]any{"key1", "val1", "key2"},
2024-02-28 15:06:29 +03:00
metadata.Metadata{
"Key1": "val1",
},
},
{
Name: "map",
Data: map[string]string{
2024-02-28 15:06:29 +03:00
"key1": "val1",
"key2": "val2",
},
Expected: metadata.Metadata{
2024-02-28 15:06:29 +03:00
"Key1": "val1",
"Key2": "val2",
},
},
{
"metadata.Metadata",
metadata.Metadata{
"key1": "val1",
"key2": "val2",
},
metadata.Metadata{
"Key1": "val1",
"Key2": "val2",
},
},
}
for _, tt := range testCases {
t.Run(tt.Name, func(t *testing.T) {
src := &s{}
var opts []options.Option
switch valData := tt.Data.(type) {
case []any:
opts = append(opts, options.Metadata(valData...))
case map[string]string, metadata.Metadata:
opts = append(opts, options.Metadata(valData))
}
2024-02-28 15:06:29 +03:00
for _, o := range opts {
if err := o(src); err != nil {
t.Fatal(err)
}
if !reflect.Equal(tt.Expected, src.Metadata) {
t.Fatal(fmt.Sprintf("expected: %v, actual: %v", tt.Expected, src.Metadata))
}
}
})
}
}