gf/protocol/goai/goai_test.go

275 lines
9.1 KiB
Go
Raw Normal View History

2021-10-02 18:54:06 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2021-10-02 14:52:28 +08:00
package goai_test
import (
"context"
2021-10-06 19:52:19 +08:00
"fmt"
2021-10-02 14:52:28 +08:00
"github.com/gogf/gf/protocol/goai"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gmeta"
"testing"
)
func Test_Basic(t *testing.T) {
type CommonReq struct {
2021-10-02 18:54:06 +08:00
AppId int64 `json:"appId" v:"required" in:"cookie" description:"应用Id"`
ResourceId string `json:"resourceId" in:"query" description:"资源Id"`
2021-10-02 14:52:28 +08:00
}
type SetSpecInfo struct {
StorageType string `v:"required|in:CLOUD_PREMIUM,CLOUD_SSD,CLOUD_HSSD" description:"StorageType"`
Shards int32 `description:"shards 分片数"`
Params []string `description:"默认参数(json 串-ClickHouseParams)"`
}
type CreateResourceReq struct {
CommonReq
gmeta.Meta `path:"/CreateResourceReq" method:"POST" tags:"default"`
Name string `description:"实例名称"`
Product string `description:"业务类型"`
Region string `v:"required" description:"区域"`
SetMap map[string]*SetSpecInfo `v:"required" description:"配置Map"`
SetSlice []SetSpecInfo `v:"required" description:"配置Slice"`
}
gtest.C(t, func(t *gtest.T) {
var (
2021-10-02 18:54:06 +08:00
err error
oai = goai.New()
2021-10-02 14:52:28 +08:00
req = new(CreateResourceReq)
)
2021-10-02 18:54:06 +08:00
err = oai.Add(goai.AddInput{
2021-10-02 14:52:28 +08:00
Object: req,
})
2021-10-02 18:54:06 +08:00
t.AssertNil(err)
// Schema asserts.
t.Assert(len(oai.Components.Schemas), 2)
2021-10-06 21:51:21 +08:00
t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Type, goai.TypeObject)
t.Assert(len(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties), 7)
t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties[`appId`].Value.Type, goai.TypeNumber)
t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties[`resourceId`].Value.Type, goai.TypeString)
2021-10-02 18:54:06 +08:00
2021-10-06 21:51:21 +08:00
t.Assert(len(oai.Components.Schemas[`goai_test.SetSpecInfo`].Value.Properties), 3)
t.Assert(oai.Components.Schemas[`goai_test.SetSpecInfo`].Value.Properties[`Params`].Value.Type, goai.TypeArray)
2021-10-02 14:52:28 +08:00
})
}
func TestOpenApiV3_Add(t *testing.T) {
type CommonReq struct {
2021-10-02 18:54:06 +08:00
AppId int64 `json:"appId" v:"required" in:"path" description:"应用Id"`
ResourceId string `json:"resourceId" in:"query" description:"资源Id"`
2021-10-02 14:52:28 +08:00
}
type SetSpecInfo struct {
StorageType string `v:"required|in:CLOUD_PREMIUM,CLOUD_SSD,CLOUD_HSSD" description:"StorageType"`
Shards int32 `description:"shards 分片数"`
Params []string `description:"默认参数(json 串-ClickHouseParams)"`
}
type CreateResourceReq struct {
CommonReq
gmeta.Meta `path:"/CreateResourceReq" method:"POST" tags:"default"`
Name string `description:"实例名称"`
Product string `description:"业务类型"`
Region string `v:"required" description:"区域"`
SetMap map[string]*SetSpecInfo `v:"required" description:"配置Map"`
SetSlice []SetSpecInfo `v:"required" description:"配置Slice"`
}
type CreateResourceRes struct {
2021-10-02 15:07:47 +08:00
gmeta.Meta `description:"Demo Response Struct"`
FlowId int64 `description:"创建实例流程id"`
2021-10-02 14:52:28 +08:00
}
f := func(ctx context.Context, req *CreateResourceReq) (res *CreateResourceRes, err error) {
return
}
gtest.C(t, func(t *gtest.T) {
2021-10-02 18:54:06 +08:00
var (
err error
oai = goai.New()
)
err = oai.Add(goai.AddInput{
Path: "/test1/{appId}",
Method: goai.HttpMethodPut,
2021-10-02 14:52:28 +08:00
Object: f,
})
2021-10-02 18:54:06 +08:00
t.AssertNil(err)
err = oai.Add(goai.AddInput{
2021-10-08 14:44:10 +08:00
Path: "/test1/{appId}",
2021-10-02 18:54:06 +08:00
Method: goai.HttpMethodPost,
Object: f,
})
t.AssertNil(err)
2021-10-06 21:11:16 +08:00
//fmt.Println(oai.String())
2021-10-02 18:54:06 +08:00
// Schema asserts.
t.Assert(len(oai.Components.Schemas), 3)
2021-10-06 21:51:21 +08:00
t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Type, goai.TypeObject)
t.Assert(len(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties), 7)
t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties[`appId`].Value.Type, goai.TypeNumber)
t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties[`resourceId`].Value.Type, goai.TypeString)
2021-10-02 18:54:06 +08:00
2021-10-06 21:51:21 +08:00
t.Assert(len(oai.Components.Schemas[`goai_test.SetSpecInfo`].Value.Properties), 3)
t.Assert(oai.Components.Schemas[`goai_test.SetSpecInfo`].Value.Properties[`Params`].Value.Type, goai.TypeArray)
2021-10-02 18:54:06 +08:00
// Paths.
2021-10-08 14:44:10 +08:00
t.Assert(len(oai.Paths), 1)
2021-10-02 18:54:06 +08:00
t.AssertNE(oai.Paths[`/test1/{appId}`].Put, nil)
t.Assert(len(oai.Paths[`/test1/{appId}`].Put.Tags), 1)
t.Assert(len(oai.Paths[`/test1/{appId}`].Put.Parameters), 2)
2021-10-08 14:44:10 +08:00
t.AssertNE(oai.Paths[`/test1/{appId}`].Post, nil)
t.Assert(len(oai.Paths[`/test1/{appId}`].Post.Tags), 1)
t.Assert(len(oai.Paths[`/test1/{appId}`].Post.Parameters), 2)
2021-10-02 14:52:28 +08:00
})
}
func TestOpenApiV3_Add_Recursive(t *testing.T) {
type CategoryTreeItem struct {
Id uint `json:"id"`
ParentId uint `json:"parent_id"`
Items []*CategoryTreeItem `json:"items,omitempty"`
}
type CategoryGetTreeReq struct {
gmeta.Meta `path:"/category-get-tree" method:"GET" tags:"default"`
ContentType string `in:"query"`
}
type CategoryGetTreeRes struct {
List []*CategoryTreeItem
}
f := func(ctx context.Context, req *CategoryGetTreeReq) (res *CategoryGetTreeRes, err error) {
return
}
gtest.C(t, func(t *gtest.T) {
var (
err error
oai = goai.New()
)
err = oai.Add(goai.AddInput{
Path: "/tree",
Object: f,
})
t.AssertNil(err)
// Schema asserts.
t.Assert(len(oai.Components.Schemas), 3)
2021-10-06 21:51:21 +08:00
t.Assert(oai.Components.Schemas[`goai_test.CategoryTreeItem`].Value.Type, goai.TypeObject)
t.Assert(len(oai.Components.Schemas[`goai_test.CategoryTreeItem`].Value.Properties), 3)
})
}
2021-10-06 19:52:19 +08:00
func TestOpenApiV3_Add_EmptyReqAndRes(t *testing.T) {
type CaptchaIndexReq struct {
gmeta.Meta `method:"GET" summary:"获取默认的验证码" description:"注意直接返回的是图片二进制内容" tags:"前台-验证码"`
}
type CaptchaIndexRes struct {
gmeta.Meta `mime:"png" description:"验证码二进制内容" `
}
f := func(ctx context.Context, req *CaptchaIndexReq) (res *CaptchaIndexRes, err error) {
return
}
gtest.C(t, func(t *gtest.T) {
var (
err error
oai = goai.New()
)
err = oai.Add(goai.AddInput{
Path: "/tree",
Object: f,
})
t.AssertNil(err)
// Schema asserts.
fmt.Println(oai.String())
})
}
2021-10-08 21:16:47 +08:00
func TestOpenApiV3_CommonResponse(t *testing.T) {
type CommonResponse struct {
Code int `json:"code" description:"Error code"`
Message string `json:"message" description:"Error message"`
Data interface{} `json:"data" description:"Result data for certain request according API definition"`
}
type Req struct {
gmeta.Meta `method:"GET"`
Product string `json:"product" in:"query" v:"required" description:"Unique product key"`
Name string `json:"name" in:"query" v:"required" description:"Instance name"`
}
type Res struct {
Product string `json:"product" v:"required" description:"Unique product key"`
TemplateName string `json:"templateName" v:"required" description:"Workflow template name"`
Version string `json:"version" v:"required" description:"Workflow template version"`
TxID string `json:"txID" v:"required" description:"Transaction ID for creating instance"`
Globals string `json:"globals" description:"Globals"`
}
f := func(ctx context.Context, req *Req) (res *Res, err error) {
return
}
gtest.C(t, func(t *gtest.T) {
var (
err error
oai = goai.New()
)
oai.Config.CommonResponse = CommonResponse{}
oai.Config.CommonResponseDataField = `Data`
err = oai.Add(goai.AddInput{
Path: "/index",
Object: f,
})
t.AssertNil(err)
// Schema asserts.
t.Assert(len(oai.Components.Schemas), 2)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties), 3)
})
}
2021-10-08 21:23:30 +08:00
func TestOpenApiV3_CommonResponse_EmptyResponse(t *testing.T) {
type CommonResponse struct {
Code int `json:"code" description:"Error code"`
Message string `json:"message" description:"Error message"`
Data interface{} `json:"data" description:"Result data for certain request according API definition"`
}
type Req struct {
gmeta.Meta `method:"GET"`
Product string `json:"product" in:"query" v:"required" description:"Unique product key"`
Name string `json:"name" in:"query" v:"required" description:"Instance name"`
}
type Res struct{}
f := func(ctx context.Context, req *Req) (res *Res, err error) {
return
}
gtest.C(t, func(t *gtest.T) {
var (
err error
oai = goai.New()
)
oai.Config.CommonResponse = CommonResponse{}
oai.Config.CommonResponseDataField = `Data`
err = oai.Add(goai.AddInput{
Path: "/index",
Object: f,
})
t.AssertNil(err)
// Schema asserts.
t.Assert(len(oai.Components.Schemas), 2)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties), 3)
})
}