mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-25 21:38:59 +02:00
Feature/improve app test (#2190)
* modify app_test.go * merge NewContext and FromContext into one test Co-authored-by: Yu Weifeng <yu.weifeng@bcrdteam.onmicrosoft.com>
This commit is contained in:
parent
ede5f889d4
commit
d1da8aa1ae
75
app_test.go
75
app_test.go
@ -3,6 +3,7 @@ package kratos
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
@ -92,6 +93,80 @@ func TestApp_Metadata(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApp_Endpoint(t *testing.T) {
|
||||
v := []string{"https://go-kratos.dev", "localhost"}
|
||||
var endpoints []*url.URL
|
||||
for _, urlStr := range v {
|
||||
if endpoint, err := url.Parse(urlStr); err != nil {
|
||||
t.Errorf("invalid endpoint:%v", urlStr)
|
||||
} else {
|
||||
endpoints = append(endpoints, endpoint)
|
||||
}
|
||||
}
|
||||
o := New(Endpoint(endpoints...))
|
||||
if instance, err := o.buildInstance(); err != nil {
|
||||
t.Error("build instance failed")
|
||||
} else {
|
||||
o.instance = instance
|
||||
}
|
||||
if !reflect.DeepEqual(o.Endpoint(), v) {
|
||||
t.Errorf("Endpoint() = %v, want %v", o.Endpoint(), v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApp_buildInstance(t *testing.T) {
|
||||
want := struct {
|
||||
id string
|
||||
name string
|
||||
version string
|
||||
metadata map[string]string
|
||||
endpoints []string
|
||||
}{
|
||||
id: "1",
|
||||
name: "kratos",
|
||||
version: "v1.0.0",
|
||||
metadata: map[string]string{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
},
|
||||
endpoints: []string{"https://go-kratos.dev", "localhost"},
|
||||
}
|
||||
var endpoints []*url.URL
|
||||
for _, urlStr := range want.endpoints {
|
||||
if endpoint, err := url.Parse(urlStr); err != nil {
|
||||
t.Errorf("invalid endpoint:%v", urlStr)
|
||||
} else {
|
||||
endpoints = append(endpoints, endpoint)
|
||||
}
|
||||
}
|
||||
app := New(
|
||||
ID(want.id),
|
||||
Name(want.name),
|
||||
Version(want.version),
|
||||
Metadata(want.metadata),
|
||||
Endpoint(endpoints...),
|
||||
)
|
||||
if got, err := app.buildInstance(); err != nil {
|
||||
t.Error("build got failed")
|
||||
} else {
|
||||
if got.ID != want.id {
|
||||
t.Errorf("ID() = %v, want %v", got.ID, want.id)
|
||||
}
|
||||
if got.Name != want.name {
|
||||
t.Errorf("Name() = %v, want %v", got.Name, want.name)
|
||||
}
|
||||
if got.Version != want.version {
|
||||
t.Errorf("Version() = %v, want %v", got.Version, want.version)
|
||||
}
|
||||
if !reflect.DeepEqual(got.Endpoints, want.endpoints) {
|
||||
t.Errorf("Endpoint() = %v, want %v", got.Endpoints, want.endpoints)
|
||||
}
|
||||
if !reflect.DeepEqual(got.Metadata, want.metadata) {
|
||||
t.Errorf("Metadata() = %v, want %v", got.Metadata, want.metadata)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApp_Context(t *testing.T) {
|
||||
type fields struct {
|
||||
id string
|
||||
version string
|
||||
|
Loading…
x
Reference in New Issue
Block a user