From 55cf83062fc0f7b870fc0e6d277255eef2e305d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=88=E5=A2=A8=E5=A4=95?= <99347745@qq.com> Date: Thu, 12 Aug 2021 19:34:58 +0800 Subject: [PATCH] add: metadata test (#1324) --- metadata/metadata_test.go | 240 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 metadata/metadata_test.go diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go new file mode 100644 index 000000000..61a2ef36f --- /dev/null +++ b/metadata/metadata_test.go @@ -0,0 +1,240 @@ +package metadata + +import ( + "context" + "reflect" + "testing" +) + +func TestNew(t *testing.T) { + type args struct { + mds []map[string]string + } + tests := []struct { + name string + args args + want Metadata + }{ + { + name: "hello", + args: args{[]map[string]string{{"hello": "kratos"}, {"hello2": "go-kratos"}}}, + want: Metadata{"hello": "kratos", "hello2": "go-kratos"}, + }, + { + name: "hi", + args: args{[]map[string]string{{"hi": "kratos"}, {"hi2": "go-kratos"}}}, + want: Metadata{"hi": "kratos", "hi2": "go-kratos"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.mds...); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMetadata_Get(t *testing.T) { + type args struct { + key string + } + tests := []struct { + name string + m Metadata + args args + want string + }{ + { + name: "kratos", + m: Metadata{"kratos": "value", "env": "dev"}, + args: args{key: "kratos"}, + want: "value", + }, + { + name: "env", + m: Metadata{"kratos": "value", "env": "dev"}, + args: args{key: "env"}, + want: "dev", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.m.Get(tt.args.key); got != tt.want { + t.Errorf("Get() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMetadata_Set(t *testing.T) { + type args struct { + key string + value string + } + tests := []struct { + name string + m Metadata + args args + want Metadata + }{ + { + name: "kratos", + m: Metadata{}, + args: args{key: "hello", value: "kratos"}, + want: Metadata{"hello": "kratos"}, + }, + { + name: "env", + m: Metadata{"hello": "kratos"}, + args: args{key: "env", value: "pro"}, + want: Metadata{"hello": "kratos", "env": "pro"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.m.Set(tt.args.key, tt.args.value) + if !reflect.DeepEqual(tt.m, tt.want) { + t.Errorf("Set() = %v, want %v", tt.m, tt.want) + } + }) + } +} + +func TestClientContext(t *testing.T) { + type args struct { + ctx context.Context + md Metadata + } + tests := []struct { + name string + args args + }{ + { + name: "kratos", + args: args{context.Background(), Metadata{"hello": "kratos", "kratos": "https://go-kratos.dev"}}, + }, + { + name: "hello", + args: args{context.Background(), Metadata{"hello": "kratos", "hello2": "https://go-kratos.dev"}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := NewClientContext(tt.args.ctx, tt.args.md) + m, ok := FromClientContext(ctx) + if !ok { + t.Errorf("FromClientContext() = %v, want %v", ok, true) + } + + if !reflect.DeepEqual(m, tt.args.md) { + t.Errorf("meta = %v, want %v", m, tt.args.md) + } + }) + } +} + +func TestServerContext(t *testing.T) { + type args struct { + ctx context.Context + md Metadata + } + tests := []struct { + name string + args args + }{ + { + name: "kratos", + args: args{context.Background(), Metadata{"hello": "kratos", "kratos": "https://go-kratos.dev"}}, + }, + { + name: "hello", + args: args{context.Background(), Metadata{"hello": "kratos", "hello2": "https://go-kratos.dev"}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := NewServerContext(tt.args.ctx, tt.args.md) + m, ok := FromServerContext(ctx) + if !ok { + t.Errorf("FromServerContext() = %v, want %v", ok, true) + } + + if !reflect.DeepEqual(m, tt.args.md) { + t.Errorf("meta = %v, want %v", m, tt.args.md) + } + }) + } +} + +func TestAppendToClientContext(t *testing.T) { + type args struct { + md Metadata + kv []string + } + tests := []struct { + name string + args args + want Metadata + }{ + { + name: "kratos", + args: args{Metadata{}, []string{"hello", "kratos", "env", "dev"}}, + want: Metadata{"hello": "kratos", "env": "dev"}, + }, + { + name: "hello", + args: args{Metadata{"hi": "https://go-kratos.dev/"}, []string{"hello", "kratos", "env", "dev"}}, + want: Metadata{"hello": "kratos", "env": "dev", "hi": "https://go-kratos.dev/"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := NewClientContext(context.Background(), tt.args.md) + ctx = AppendToClientContext(ctx, tt.args.kv...) + md, ok := FromClientContext(ctx) + if !ok { + t.Errorf("FromServerContext() = %v, want %v", ok, true) + } + if !reflect.DeepEqual(md, tt.want) { + t.Errorf("metadata = %v, want %v", md, tt.want) + } + }) + } +} + +func TestMergeToClientContext(t *testing.T) { + type args struct { + md Metadata + appendMd Metadata + } + tests := []struct { + name string + args args + want Metadata + }{ + { + name: "kratos", + args: args{Metadata{}, Metadata{"hello": "kratos", "env": "dev"}}, + want: Metadata{"hello": "kratos", "env": "dev"}, + }, + { + name: "hello", + args: args{Metadata{"hi": "https://go-kratos.dev/"}, Metadata{"hello": "kratos", "env": "dev"}}, + want: Metadata{"hello": "kratos", "env": "dev", "hi": "https://go-kratos.dev/"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := NewClientContext(context.Background(), tt.args.md) + ctx = MergeToClientContext(ctx, tt.args.appendMd) + md, ok := FromClientContext(ctx) + if !ok { + t.Errorf("FromServerContext() = %v, want %v", ok, true) + } + if !reflect.DeepEqual(md, tt.want) { + t.Errorf("metadata = %v, want %v", md, tt.want) + } + }) + } +}