github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/cmd/internal/openapi/v3/helper_test.go (about) 1 package v3 2 3 import ( 4 . "github.com/smartystreets/goconvey/convey" 5 "github.com/unionj-cloud/go-doudou/cmd/internal/astutils" 6 v3 "github.com/unionj-cloud/go-doudou/toolkit/openapi/v3" 7 "strings" 8 "testing" 9 ) 10 11 func Test_isSupport(t *testing.T) { 12 type args struct { 13 t string 14 } 15 tests := []struct { 16 name string 17 args args 18 want bool 19 }{ 20 { 21 name: "1", 22 args: args{ 23 t: "float32", 24 }, 25 want: true, 26 }, 27 { 28 name: "2", 29 args: args{ 30 t: "[]int64", 31 }, 32 want: true, 33 }, 34 } 35 for _, tt := range tests { 36 t.Run(tt.name, func(t *testing.T) { 37 if got := IsSupport(tt.args.t); got != tt.want { 38 t.Errorf("isSupport() = %v, want %v", got, tt.want) 39 } 40 }) 41 } 42 } 43 44 func Test_castFunc(t *testing.T) { 45 type args struct { 46 t string 47 } 48 tests := []struct { 49 name string 50 args args 51 want string 52 }{ 53 { 54 name: "1", 55 args: args{ 56 t: "uint64", 57 }, 58 want: "ToUint64", 59 }, 60 } 61 for _, tt := range tests { 62 t.Run(tt.name, func(t *testing.T) { 63 if got := CastFunc(tt.args.t); got != tt.want { 64 t.Errorf("castFunc() = %v, want %v", got, tt.want) 65 } 66 }) 67 } 68 } 69 70 func TestSchemaOf(t *testing.T) { 71 Convey("SchemaOf", t, func() { 72 So(SchemaOf(astutils.FieldMeta{ 73 Name: "name", 74 Type: "string", 75 IsExport: true, 76 }), ShouldEqual, v3.String) 77 78 So(SchemaOf(astutils.FieldMeta{ 79 Name: "age", 80 Type: "int", 81 IsExport: true, 82 }), ShouldEqual, v3.Int) 83 84 So(SchemaOf(astutils.FieldMeta{ 85 Name: "id", 86 Type: "int64", 87 IsExport: true, 88 }), ShouldEqual, v3.Int64) 89 90 So(SchemaOf(astutils.FieldMeta{ 91 Name: "married", 92 Type: "bool", 93 IsExport: true, 94 }), ShouldEqual, v3.Bool) 95 96 So(SchemaOf(astutils.FieldMeta{ 97 Name: "score", 98 Type: "float32", 99 IsExport: true, 100 }), ShouldEqual, v3.Float32) 101 102 So(SchemaOf(astutils.FieldMeta{ 103 Name: "average", 104 Type: "float64", 105 IsExport: true, 106 }), ShouldEqual, v3.Float64) 107 108 So(SchemaOf(astutils.FieldMeta{ 109 Name: "avatar", 110 Type: "v3.FileModel", 111 IsExport: true, 112 }), ShouldEqual, v3.File) 113 114 So(SchemaOf(astutils.FieldMeta{ 115 Name: "params", 116 Type: "...int", 117 IsExport: true, 118 }).Type, ShouldEqual, v3.ArrayT) 119 120 So(SchemaOf(astutils.FieldMeta{ 121 Name: "data", 122 Type: "map[string]string", 123 IsExport: true, 124 }).Type, ShouldEqual, v3.ObjectT) 125 126 So(SchemaOf(astutils.FieldMeta{ 127 Name: "anony", 128 Type: "anonystruct«{\"Name\":\"\",\"Fields\":[{\"Name\":\"Name\",\"Type\":\"string\",\"Tag\":\"\",\"Comments\":null,\"IsExport\":true,\"DocName\":\"Name\"},{\"Name\":\"Addr\",\"Type\":\"anonystruct«{\\\"Name\\\":\\\"\\\",\\\"Fields\\\":[{\\\"Name\\\":\\\"Zip\\\",\\\"Type\\\":\\\"string\\\",\\\"Tag\\\":\\\"\\\",\\\"Comments\\\":null,\\\"IsExport\\\":true,\\\"DocName\\\":\\\"Zip\\\"},{\\\"Name\\\":\\\"Block\\\",\\\"Type\\\":\\\"string\\\",\\\"Tag\\\":\\\"\\\",\\\"Comments\\\":null,\\\"IsExport\\\":true,\\\"DocName\\\":\\\"Block\\\"},{\\\"Name\\\":\\\"Full\\\",\\\"Type\\\":\\\"string\\\",\\\"Tag\\\":\\\"\\\",\\\"Comments\\\":null,\\\"IsExport\\\":true,\\\"DocName\\\":\\\"Full\\\"}],\\\"Comments\\\":null,\\\"Methods\\\":null,\\\"IsExport\\\":false}»\",\"Tag\":\"\",\"Comments\":null,\"IsExport\":true,\"DocName\":\"Addr\"}],\"Comments\":null,\"Methods\":null,\"IsExport\":false}»", 129 IsExport: true, 130 }).Type, ShouldEqual, v3.ObjectT) 131 132 SchemaNames = []string{"User"} 133 So(SchemaOf(astutils.FieldMeta{ 134 Name: "user", 135 Type: "User", 136 IsExport: true, 137 }).Ref, ShouldEqual, "#/components/schemas/User") 138 139 So(SchemaOf(astutils.FieldMeta{ 140 Name: "user", 141 Type: "vo.User", 142 IsExport: true, 143 }).Ref, ShouldEqual, "#/components/schemas/User") 144 145 Enums = map[string]astutils.EnumMeta{ 146 "KeyboardLayout": { 147 Name: "KeyboardLayout", 148 Values: []string{ 149 "UNKNOWN", 150 "QWERTZ", 151 }, 152 }, 153 } 154 So(SchemaOf(astutils.FieldMeta{ 155 Name: "layout", 156 Type: "KeyboardLayout", 157 IsExport: true, 158 }).Enum, ShouldResemble, []interface{}{ 159 "UNKNOWN", 160 "QWERTZ", 161 }) 162 163 So(SchemaOf(astutils.FieldMeta{ 164 Name: "any", 165 Type: "Any", 166 IsExport: true, 167 }), ShouldEqual, v3.Any) 168 }) 169 } 170 171 func TestRefAddDoc(t *testing.T) { 172 Convey("Description should be equal to doc", t, func() { 173 SchemaNames = []string{"User"} 174 schema := SchemaOf(astutils.FieldMeta{ 175 Name: "user", 176 Type: "User", 177 IsExport: true, 178 }) 179 doc := "This is a struct for user field" 180 RefAddDoc(schema, doc) 181 So(strings.TrimSpace(Schemas["User"].Description), ShouldEqual, doc) 182 }) 183 } 184 185 func TestIsBuiltin(t *testing.T) { 186 Convey("Test IsBuiltIn", t, func() { 187 So(IsBuiltin(astutils.FieldMeta{ 188 Name: "age", 189 Type: "int", 190 IsExport: true, 191 }), ShouldBeTrue) 192 193 So(IsBuiltin(astutils.FieldMeta{ 194 Name: "books", 195 Type: "[]string", 196 IsExport: true, 197 }), ShouldBeTrue) 198 199 So(IsBuiltin(astutils.FieldMeta{ 200 Name: "data", 201 Type: "map[string]string", 202 IsExport: true, 203 }), ShouldBeFalse) 204 }) 205 } 206 207 func TestIsEnum(t *testing.T) { 208 Convey("Test IsEnum", t, func() { 209 So(IsEnum(astutils.FieldMeta{ 210 Name: "age", 211 Type: "int", 212 IsExport: true, 213 }), ShouldBeFalse) 214 215 Enums = map[string]astutils.EnumMeta{ 216 "KeyboardLayout": { 217 Name: "KeyboardLayout", 218 Values: []string{ 219 "UNKNOWN", 220 "QWERTZ", 221 }, 222 }, 223 } 224 So(IsEnum(astutils.FieldMeta{ 225 Name: "layout", 226 Type: "KeyboardLayout", 227 IsExport: true, 228 }), ShouldBeTrue) 229 }) 230 } 231 232 func TestElementType(t *testing.T) { 233 Convey("Test ElementType", t, func() { 234 So(ElementType("[]int"), ShouldEqual, "int") 235 So(ElementType("...int"), ShouldEqual, "int") 236 }) 237 } 238 239 func TestIsOptional(t *testing.T) { 240 Convey("Test IsOptional", t, func() { 241 So(IsOptional("*[]int"), ShouldBeTrue) 242 So(IsOptional("...int"), ShouldBeTrue) 243 So(IsOptional("int"), ShouldBeFalse) 244 }) 245 } 246 247 func TestIsSlice(t *testing.T) { 248 Convey("Test IsOptional", t, func() { 249 So(IsSlice("*[]int"), ShouldBeTrue) 250 So(IsSlice("...int"), ShouldBeTrue) 251 So(IsSlice("int"), ShouldBeFalse) 252 }) 253 }