2016-03-14 21:32:08 +03:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2019-10-25 18:27:28 +03:00
|
|
|
"reflect"
|
2016-03-14 21:32:08 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-01-17 12:40:49 +03:00
|
|
|
func TestMetadataCopy(t *testing.T) {
|
|
|
|
md := Metadata{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
}
|
|
|
|
|
|
|
|
cp := Copy(md)
|
|
|
|
|
|
|
|
for k, v := range md {
|
|
|
|
if cv := cp[k]; cv != v {
|
|
|
|
t.Fatalf("Got %s:%s for %s:%s", k, cv, k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 21:32:08 +03:00
|
|
|
func TestMetadataContext(t *testing.T) {
|
|
|
|
md := Metadata{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := NewContext(context.TODO(), md)
|
|
|
|
|
|
|
|
emd, ok := FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Unexpected error retrieving metadata, got %t", ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
if emd["foo"] != md["foo"] {
|
|
|
|
t.Errorf("Expected key: %s val: %s, got key: %s val: %s", "foo", md["foo"], "foo", emd["foo"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if i := len(emd); i != 1 {
|
|
|
|
t.Errorf("Expected metadata length 1 got %d", i)
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 03:51:54 +03:00
|
|
|
|
2019-10-25 18:27:28 +03:00
|
|
|
func TestAppendContext(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
existing Metadata
|
|
|
|
append Metadata
|
|
|
|
overwrite bool
|
2019-10-25 03:51:54 +03:00
|
|
|
}
|
2019-10-25 18:27:28 +03:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want Metadata
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "matching key, overwrite false",
|
|
|
|
args: args{
|
|
|
|
existing: Metadata{"foo": "bar", "sumo": "demo"},
|
|
|
|
append: Metadata{"sumo": "demo2"},
|
|
|
|
overwrite: false,
|
|
|
|
},
|
|
|
|
want: Metadata{"foo": "bar", "sumo": "demo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "matching key, overwrite true",
|
|
|
|
args: args{
|
|
|
|
existing: Metadata{"foo": "bar", "sumo": "demo"},
|
|
|
|
append: Metadata{"sumo": "demo2"},
|
|
|
|
overwrite: true,
|
|
|
|
},
|
|
|
|
want: Metadata{"foo": "bar", "sumo": "demo2"},
|
|
|
|
},
|
2019-10-25 03:51:54 +03:00
|
|
|
}
|
2019-10-25 18:27:28 +03:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got, _ := FromContext(AppendContext(NewContext(context.TODO(), tt.args.existing), tt.args.append, tt.args.overwrite)); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("AppendContext() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
2019-10-25 03:51:54 +03:00
|
|
|
}
|
|
|
|
}
|