yjp před 1 rokem
rodič
revize
419e7208c7

+ 1 - 0
examples/examples/project/application/application.go

@@ -28,6 +28,7 @@ func DestroyApp() {
 	}
 
 	baize.DestroyApplication(appInstance)
+	appInstance = nil
 }
 
 type Service interface {

+ 1 - 1
examples/examples/project/deployment/config/config.yaml

@@ -1,5 +1,5 @@
 api:
-  url_prefix: example
+  url_prefix: example/api
   port: 31000
 infrastructure:
   database:

+ 83 - 0
examples/examples/project/test/configuration_test.go

@@ -0,0 +1,83 @@
+package test
+
+import (
+	"git.sxidc.com/go-framework/baize/convenient/binding/response"
+	"git.sxidc.com/go-framework/baize/convenient/domain/configuration"
+	"git.sxidc.com/go-tools/utils/strutils"
+	"net/http"
+	"testing"
+)
+
+func TestConfiguration(t *testing.T) {
+	Init()
+	defer Destroy()
+
+	scope := strutils.SimpleUUID()
+	group := strutils.SimpleUUID()
+	value := strutils.SimpleUUID()
+
+	values := make([]string, 0)
+
+	NewToolKit(t).CreateConfiguration(scope, group, value).
+		GetConfigurationValues(scope, group, &values).
+		AssertEqual(1, len(values), "值的长度不正确").
+		AssertEqual(value, values[0], "值不正确").
+		DeleteConfiguration(scope, group).
+		GetConfigurationValues(scope, group, &values).
+		AssertEqual(0, len(values), "值的长度不正确")
+}
+
+func (toolKit *ToolKit) CreateConfiguration(scope string, group string, value string) *ToolKit {
+	msgResponse := new(response.MsgResponse)
+
+	toolKit.SetHeader("Content-Type", "application/json").
+		SetJsonBody(&configuration.AddConfigurationJsonBody{
+			Scope: scope,
+			Group: group,
+			Value: value,
+		}).
+		SetJsonResponse(msgResponse).
+		Request("/example/api/configuration/create", http.MethodPost).
+		AssertStatusCode(http.StatusOK).
+		AssertEqual(true, msgResponse.Success, msgResponse.Msg)
+
+	return toolKit
+}
+
+func (toolKit *ToolKit) DeleteConfiguration(scope string, group string) *ToolKit {
+	msgResponse := new(response.MsgResponse)
+
+	toolKit.SetHeader("Content-Type", "application/json").
+		SetJsonResponse(msgResponse).
+		SetJsonBody(&configuration.RemoveConfigurationJsonBody{
+			Scope: scope,
+			Group: group,
+		}).
+		Request("/example/api/configuration/delete", http.MethodPost).
+		AssertStatusCode(http.StatusOK).
+		AssertEqual(true, msgResponse.Success, msgResponse.Msg)
+
+	return toolKit
+}
+
+func (toolKit *ToolKit) GetConfigurationValues(scope string, group string, retValues *[]string) *ToolKit {
+	getConfigurationValuesResponse := new(struct {
+		response.MsgResponse
+		Values []string `json:"values"`
+	})
+
+	toolKit.SetHeader("Content-Type", "application/json").
+		SetJsonResponse(getConfigurationValuesResponse).
+		SetQueryParams("scope", scope).
+		SetQueryParams("group", group).
+		Request("/example/api/configuration/values", http.MethodGet).
+		AssertStatusCode(http.StatusOK).
+		AssertEqual(true, getConfigurationValuesResponse.Success, getConfigurationValuesResponse.Msg)
+
+	if retValues != nil {
+		*retValues = make([]string, 0)
+		*retValues = getConfigurationValuesResponse.Values
+	}
+
+	return toolKit
+}

+ 186 - 0
examples/examples/project/test/tool_kit.go

@@ -0,0 +1,186 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"git.sxidc.com/go-framework/baize/examples/examples/project/application"
+	"git.sxidc.com/go-tools/utils/strutils"
+	"github.com/stretchr/testify/assert"
+	"io"
+	"net/http"
+	"net/http/httptest"
+	"reflect"
+	"testing"
+	"time"
+)
+
+type ToolKit struct {
+	t *testing.T
+
+	body         io.Reader
+	header       map[string]string
+	queryParams  map[string]string
+	jsonResponse interface{}
+	token        string
+
+	responseRecorder *httptest.ResponseRecorder
+}
+
+func Init() {
+	application.NewApp()
+
+	go func() {
+		err := application.Start()
+		if err != nil {
+			panic(err)
+		}
+	}()
+
+	time.Sleep(100 * time.Millisecond)
+}
+
+func Destroy() {
+	err := application.Finish()
+	if err != nil {
+		panic(err)
+	}
+
+	application.DestroyApp()
+}
+
+func NewToolKit(t *testing.T) *ToolKit {
+	return &ToolKit{
+		t:           t,
+		header:      make(map[string]string),
+		queryParams: make(map[string]string),
+	}
+}
+
+func (toolKit *ToolKit) SetHeader(key string, value string) *ToolKit {
+	toolKit.header[key] = value
+	return toolKit
+}
+
+func (toolKit *ToolKit) SetBody(body *bytes.Buffer) *ToolKit {
+	toolKit.body = body
+	return toolKit
+}
+
+func (toolKit *ToolKit) SetQueryParams(key string, value string) *ToolKit {
+	toolKit.queryParams[key] = value
+	return toolKit
+}
+
+func (toolKit *ToolKit) SetJsonBody(body interface{}) *ToolKit {
+	jsonBody, err := json.Marshal(body)
+	if err != nil {
+		toolKit.t.Fatal("转换JSON失败")
+	}
+
+	toolKit.body = bytes.NewBuffer(jsonBody)
+
+	return toolKit
+}
+
+func (toolKit *ToolKit) SetJsonResponse(response interface{}) *ToolKit {
+	if response == nil {
+		toolKit.jsonResponse = nil
+		return toolKit
+	}
+
+	responseValue := reflect.ValueOf(response)
+	if responseValue.Kind() != reflect.Ptr {
+		toolKit.t.Fatal("JsonResponse应该传递指针类型")
+	}
+
+	toolKit.jsonResponse = response
+
+	return toolKit
+}
+
+func (toolKit *ToolKit) SetToken(token string) *ToolKit {
+	toolKit.token = token
+	return toolKit
+}
+
+func (toolKit *ToolKit) Request(url string, method string) *ToolKit {
+	if len(toolKit.queryParams) != 0 {
+		url = url + "?"
+		for key, value := range toolKit.queryParams {
+			url = url + key + "=" + value + "&"
+		}
+
+		url = url[:len(url)-1]
+
+		toolKit.queryParams = make(map[string]string)
+	}
+
+	request, err := http.NewRequest(method, url, toolKit.body)
+	if err != nil {
+		toolKit.t.Fatal("创建请求失败", err)
+	}
+
+	if len(toolKit.header) != 0 {
+		for key, value := range toolKit.header {
+			request.Header.Add(key, value)
+		}
+
+		toolKit.header = make(map[string]string)
+	}
+
+	if !strutils.IsStringEmpty(toolKit.token) {
+		request.Header.Add("Authorization", toolKit.token)
+	}
+
+	toolKit.responseRecorder = httptest.NewRecorder()
+	application.ServerHttpForTest(toolKit.responseRecorder, request)
+
+	if toolKit.responseRecorder.Code == http.StatusOK && toolKit.jsonResponse != nil {
+		responseBody, err := io.ReadAll(toolKit.responseRecorder.Body)
+		if err != nil {
+			toolKit.t.Fatal("读取响应Body失败")
+		}
+
+		err = json.Unmarshal(responseBody, toolKit.jsonResponse)
+		if err != nil {
+			toolKit.t.Fatal("转换Response失败")
+		}
+	}
+
+	return toolKit
+}
+
+func (toolKit *ToolKit) AssertStatusCode(code int) *ToolKit {
+	assert.Equal(toolKit.t, code, toolKit.responseRecorder.Code)
+	return toolKit
+}
+
+func (toolKit *ToolKit) AssertBodyEqual(body string) *ToolKit {
+	assert.Equal(toolKit.t, body, toolKit.responseRecorder.Body.String())
+	return toolKit
+}
+
+func (toolKit *ToolKit) AssertEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) *ToolKit {
+	assert.Equal(toolKit.t, expected, actual, msgAndArgs)
+	return toolKit
+}
+
+func (toolKit *ToolKit) AssertNotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) *ToolKit {
+	assert.NotEqual(toolKit.t, expected, actual, msgAndArgs)
+	return toolKit
+}
+
+func (toolKit *ToolKit) AssertNotEmpty(object interface{}, msgAndArgs ...interface{}) *ToolKit {
+	assert.NotEmpty(toolKit.t, object, msgAndArgs)
+	return toolKit
+}
+
+func (toolKit *ToolKit) AssertGreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) *ToolKit {
+	assert.GreaterOrEqual(toolKit.t, e1, e2, msgAndArgs)
+	return toolKit
+}
+
+func (toolKit *ToolKit) AssertZero(e1 interface{}, msgAndArgs ...interface{}) *ToolKit {
+	assert.Zero(toolKit.t, e1, msgAndArgs)
+	return toolKit
+}

+ 20 - 0
examples/examples/project/test/version_test.go

@@ -0,0 +1,20 @@
+package test
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestVersion(t *testing.T) {
+	Init()
+	defer Destroy()
+
+	versionResponse := &struct {
+		Version string `json:"version"`
+	}{}
+
+	NewToolKit(t).SetJsonResponse(versionResponse).
+		Request("/example/api/version", http.MethodGet).
+		AssertStatusCode(http.StatusOK).
+		AssertEqual(versionResponse.Version, "v1.0.0")
+}

+ 3 - 0
go.mod

@@ -12,6 +12,7 @@ require (
 	github.com/golang/protobuf v1.5.4
 	github.com/iancoleman/strcase v0.3.0
 	github.com/mwitkow/go-proto-validators v0.3.2
+	github.com/stretchr/testify v1.9.0
 	github.com/vrecan/death v3.0.1+incompatible
 	go.uber.org/zap v1.27.0
 	google.golang.org/grpc v1.64.0
@@ -31,6 +32,7 @@ require (
 	github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
 	github.com/cloudwego/base64x v0.1.4 // indirect
 	github.com/cloudwego/iasm v0.2.0 // indirect
+	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/gabriel-vasile/mimetype v1.4.3 // indirect
 	github.com/gin-contrib/sse v0.1.0 // indirect
 	github.com/go-playground/locales v0.14.1 // indirect
@@ -58,6 +60,7 @@ require (
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/olahol/melody v1.2.1 // indirect
 	github.com/pelletier/go-toml/v2 v2.2.2 // indirect
+	github.com/pmezard/go-difflib v1.0.0 // indirect
 	github.com/puzpuzpuz/xsync v1.5.2 // indirect
 	github.com/rogpeppe/go-internal v1.12.0 // indirect
 	github.com/satori/go.uuid v1.2.0 // indirect