blueprint/blueprint/modules/openapi/model/model_file_test.go

197 lines
3.7 KiB
Go

package model
import (
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/suite"
)
const (
testFile = "../openapi_test.yaml"
)
type ModelFileTestSuite struct {
doc *openapi3.T
models []*File
suite.Suite
}
func (r *ModelFileTestSuite) SetupSuite() {
loader := openapi3.NewLoader()
doc, err := loader.LoadFromFile(testFile)
r.NoErrorf(err, "cannot load openapi file: %v")
err = doc.Validate(loader.Context)
r.NoErrorf(err, "cannot validate openapi: %s")
r.doc = doc
}
func (r *ModelFileTestSuite) TearDownSuite() {
}
func TestModelTestSuite(t *testing.T) {
suite.Run(t, new(ModelFileTestSuite))
}
func (r *ModelFileTestSuite) TestFillFromSchema() {
type args struct {
modelName string
schema *openapi3.Schema
}
modelColor := &File{
Name: "Color",
Fields: []FileField{
{
Name: "R",
Type: "int",
Tag: NewTag("r", "json"),
Title: "Red",
},
{
Name: "G",
Type: "int",
Tag: NewTag("g", "json"),
Title: "Green",
},
{
Type: "int",
Tag: NewTag("b", "json"),
Title: "Blue",
Name: "B",
},
{
Name: "A",
Type: "int",
Tag: NewTag("a", "json"),
Title: "Alpha",
},
},
}
modelProduct := &File{
Name: "Product",
Fields: []FileField{
{
Name: "ID",
Type: "string",
Tag: NewTag("_id", "json"),
Title: "ID",
Description: "Auto generated ID",
},
{
Name: "Name",
Type: "string",
Tag: NewTag("name", "json"),
Title: "Product name",
},
{
Name: "Color",
Type: "Color",
Tag: NewTag("color", "json"),
Title: "", // Title for refs cannot parsed
Description: "", // Description for refs cannot parsed
},
{
Name: "Status",
Type: "Status",
Tag: NewTag("status", "json"),
},
{
Name: "Tags",
Type: "[]string",
Tag: NewTag("tags", "json"),
Title: "Tags",
Description: "Array of tags",
},
},
Enums: []FileEnum{
{
Name: "Status",
Items: []any{"in_stock", "in reserve", "out-of-stock"},
},
},
}
modelCategory := &File{
Name: "Category",
Fields: []FileField{
{
Name: "ID",
Type: "string",
Tag: NewTag("_id", "json"),
Title: "ID",
Description: "Auto generated ID",
},
{
Name: "Name",
Type: "string",
Tag: NewTag("name", "json"),
Title: "Category name",
},
{
Name: "Products",
Type: "[]Product",
Tag: NewTag("products", "json"),
Title: "Products",
Description: "Array of included products",
},
},
}
tests := []struct {
name string
got *File
args args
want *File
}{
{
name: "Color",
got: NewFile("Color", "models"),
args: args{
modelName: "Color",
schema: r.doc.Components.Schemas["Color"].Value,
},
want: modelColor,
},
{
name: "Product",
got: NewFile("Product", "models"),
args: args{
modelName: "Product",
schema: r.doc.Components.Schemas["Product"].Value,
},
want: modelProduct,
},
{
name: "Category",
got: NewFile("Category", "models"),
args: args{
modelName: "Category",
schema: r.doc.Components.Schemas["Category"].Value,
},
want: modelCategory,
},
}
for _, tt := range tests {
r.Run(tt.name, func() {
tt.got.SetName(tt.args.modelName)
tt.got.FillFromSchema(tt.args.schema)
r.Equal(tt.want.Name, tt.got.Name)
r.ElementsMatch(tt.want.Fields, tt.got.Fields)
r.models = append(r.models, tt.got)
})
}
}
func (r *ModelFileTestSuite) TestSave() {
for _, model := range r.models {
path := "./model_test"
err := model.Save(path)
r.NoError(err)
}
}