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"
|
|
|
|
)
|
|
|
|
|
2020-02-21 23:04:47 +03:00
|
|
|
func TestMetadataSet(t *testing.T) {
|
|
|
|
ctx := Set(context.TODO(), "Key", "val")
|
|
|
|
|
|
|
|
val, ok := Get(ctx, "Key")
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("key Key not found")
|
|
|
|
}
|
|
|
|
if val != "val" {
|
|
|
|
t.Errorf("key Key with value val != %v", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 23:39:18 +03:00
|
|
|
func TestMetadataDelete(t *testing.T) {
|
2020-03-31 22:55:33 +03:00
|
|
|
md := Metadata{
|
|
|
|
"Foo": "bar",
|
|
|
|
"Baz": "empty",
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := NewContext(context.TODO(), md)
|
2020-09-30 16:14:54 +03:00
|
|
|
ctx = Del(ctx, "Baz")
|
2020-03-31 22:55:33 +03:00
|
|
|
|
|
|
|
emd, ok := FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("key Key not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = emd["Baz"]
|
|
|
|
if ok {
|
|
|
|
t.Fatal("key Baz not deleted")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-17 12:40:49 +03:00
|
|
|
func TestMetadataCopy(t *testing.T) {
|
|
|
|
md := Metadata{
|
2019-12-31 16:37:29 +03:00
|
|
|
"Foo": "bar",
|
2020-10-01 16:05:05 +03:00
|
|
|
"Bar": "baz",
|
2019-01-17 12:40:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2019-12-31 16:37:29 +03:00
|
|
|
"Foo": "bar",
|
2016-03-14 21:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := NewContext(context.TODO(), md)
|
|
|
|
|
|
|
|
emd, ok := FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Unexpected error retrieving metadata, got %t", ok)
|
|
|
|
}
|
|
|
|
|
2019-12-31 16:37:29 +03:00
|
|
|
if emd["Foo"] != md["Foo"] {
|
|
|
|
t.Errorf("Expected key: %s val: %s, got key: %s val: %s", "Foo", md["Foo"], "Foo", emd["Foo"])
|
2016-03-14 21:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-26 01:28:43 +03:00
|
|
|
func TestMergeContext(t *testing.T) {
|
2019-10-25 18:27:28 +03:00
|
|
|
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{
|
2019-12-31 16:37:29 +03:00
|
|
|
existing: Metadata{"Foo": "bar", "Sumo": "demo"},
|
|
|
|
append: Metadata{"Sumo": "demo2"},
|
2019-10-25 18:27:28 +03:00
|
|
|
overwrite: false,
|
|
|
|
},
|
2019-12-31 16:37:29 +03:00
|
|
|
want: Metadata{"Foo": "bar", "Sumo": "demo"},
|
2019-10-25 18:27:28 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "matching key, overwrite true",
|
|
|
|
args: args{
|
2019-12-31 16:37:29 +03:00
|
|
|
existing: Metadata{"Foo": "bar", "Sumo": "demo"},
|
|
|
|
append: Metadata{"Sumo": "demo2"},
|
2019-10-25 18:27:28 +03:00
|
|
|
overwrite: true,
|
|
|
|
},
|
2019-12-31 16:37:29 +03:00
|
|
|
want: Metadata{"Foo": "bar", "Sumo": "demo2"},
|
2019-10-25 18:27:28 +03:00
|
|
|
},
|
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) {
|
2019-10-26 01:28:43 +03:00
|
|
|
if got, _ := FromContext(MergeContext(NewContext(context.TODO(), tt.args.existing), tt.args.append, tt.args.overwrite)); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("MergeContext() = %v, want %v", got, tt.want)
|
2019-10-25 18:27:28 +03:00
|
|
|
}
|
|
|
|
})
|
2019-10-25 03:51:54 +03:00
|
|
|
}
|
|
|
|
}
|