github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/cmd/internal/openapi/v3/codegen/client/go_test.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/stretchr/testify/assert" 7 v3 "github.com/unionj-cloud/go-doudou/toolkit/openapi/v3" 8 "github.com/unionj-cloud/go-doudou/toolkit/pathutils" 9 "os" 10 "path" 11 "path/filepath" 12 "strings" 13 "testing" 14 ) 15 16 func Test_genGoVo(t *testing.T) { 17 testdir := pathutils.Abs("../testdata") 18 api := loadAPI(path.Join(testdir, "petstore3.json")) 19 genGoVo(api.Components.Schemas, filepath.Join(testdir, "test", "vo.go"), "test") 20 } 21 22 func Test_genGoVo_clean(t *testing.T) { 23 testdir := pathutils.Abs("../testdata") 24 api := loadAPI(path.Join(testdir, "test5.json")) 25 genGoVo(api.Components.Schemas, filepath.Join(testdir, "test", "vo.go"), "test") 26 } 27 28 func Test_genGoVo_Omit(t *testing.T) { 29 testdir := pathutils.Abs("../testdata") 30 api := loadAPI(path.Join(testdir, "petstore3.json")) 31 omitempty = true 32 genGoVo(api.Components.Schemas, filepath.Join(testdir, "test", "vo.go"), "test") 33 } 34 35 func Test_genGoHttp(t *testing.T) { 36 testdir := pathutils.Abs("../testdata") 37 api := loadAPI(path.Join(testdir, "petstore3.json")) 38 schemas = api.Components.Schemas 39 requestBodies = api.Components.RequestBodies 40 svcmap := make(map[string]map[string]v3.Path) 41 for endpoint, path := range api.Paths { 42 svcname := strings.Split(strings.Trim(endpoint, "/"), "/")[0] 43 if value, exists := svcmap[svcname]; exists { 44 value[endpoint] = path 45 } else { 46 svcmap[svcname] = make(map[string]v3.Path) 47 svcmap[svcname][endpoint] = path 48 } 49 } 50 51 for svcname, paths := range svcmap { 52 genGoHTTP(paths, svcname, filepath.Join(testdir, "test"), "", "test") 53 } 54 } 55 56 func Test_genGoHttp1(t *testing.T) { 57 testdir := pathutils.Abs("../testdata") 58 api := loadAPI(path.Join(testdir, "test1.json")) 59 schemas = api.Components.Schemas 60 requestBodies = api.Components.RequestBodies 61 svcmap := make(map[string]map[string]v3.Path) 62 for endpoint, path := range api.Paths { 63 svcname := strings.Split(strings.Trim(endpoint, "/"), "/")[0] 64 if value, exists := svcmap[svcname]; exists { 65 value[endpoint] = path 66 } else { 67 svcmap[svcname] = make(map[string]v3.Path) 68 svcmap[svcname][endpoint] = path 69 } 70 } 71 72 for svcname, paths := range svcmap { 73 genGoHTTP(paths, svcname, filepath.Join(testdir, "test"), "", "test") 74 } 75 } 76 77 func Test_genGoHttp2(t *testing.T) { 78 testdir := pathutils.Abs("../testdata") 79 api := loadAPI(path.Join(testdir, "test2.json")) 80 schemas = api.Components.Schemas 81 requestBodies = api.Components.RequestBodies 82 svcmap := make(map[string]map[string]v3.Path) 83 for endpoint, path := range api.Paths { 84 svcname := strings.Split(strings.Trim(endpoint, "/"), "/")[0] 85 if value, exists := svcmap[svcname]; exists { 86 value[endpoint] = path 87 } else { 88 svcmap[svcname] = make(map[string]v3.Path) 89 svcmap[svcname][endpoint] = path 90 } 91 } 92 93 for svcname, paths := range svcmap { 94 genGoHTTP(paths, svcname, filepath.Join(testdir, "test"), "", "test") 95 } 96 } 97 98 func Test_genGoHttp3(t *testing.T) { 99 testdir := pathutils.Abs("../testdata") 100 api := loadAPI(path.Join(testdir, "test3.json")) 101 schemas = api.Components.Schemas 102 requestBodies = api.Components.RequestBodies 103 svcmap := make(map[string]map[string]v3.Path) 104 for endpoint, path := range api.Paths { 105 svcname := strings.Split(strings.Trim(endpoint, "/"), "/")[0] 106 if value, exists := svcmap[svcname]; exists { 107 value[endpoint] = path 108 } else { 109 svcmap[svcname] = make(map[string]v3.Path) 110 svcmap[svcname][endpoint] = path 111 } 112 } 113 114 for svcname, paths := range svcmap { 115 genGoHTTP(paths, svcname, filepath.Join(testdir, "test"), "", "test") 116 } 117 } 118 119 func Test_genGoHttp4(t *testing.T) { 120 api := loadAPI("https://petstore3.swagger.io/api/v3/openapi.json") 121 schemas = api.Components.Schemas 122 requestBodies = api.Components.RequestBodies 123 svcmap := make(map[string]map[string]v3.Path) 124 for endpoint, path := range api.Paths { 125 svcname := strings.Split(strings.Trim(endpoint, "/"), "/")[0] 126 if value, exists := svcmap[svcname]; exists { 127 value[endpoint] = path 128 } else { 129 svcmap[svcname] = make(map[string]v3.Path) 130 svcmap[svcname][endpoint] = path 131 } 132 } 133 134 for svcname, paths := range svcmap { 135 genGoHTTP(paths, svcname, filepath.Join(pathutils.Abs("../testdata"), "test"), "", "test") 136 } 137 } 138 139 func Test_genGoHttp5(t *testing.T) { 140 testdir := pathutils.Abs("../testdata") 141 api := loadAPI(path.Join(testdir, "test5.json")) 142 schemas = api.Components.Schemas 143 requestBodies = api.Components.RequestBodies 144 svcmap := make(map[string]map[string]v3.Path) 145 for endpoint, path := range api.Paths { 146 svcname := strings.Split(strings.Trim(endpoint, "/"), "/")[0] 147 if value, exists := svcmap[svcname]; exists { 148 value[endpoint] = path 149 } else { 150 svcmap[svcname] = make(map[string]v3.Path) 151 svcmap[svcname][endpoint] = path 152 } 153 } 154 155 for svcname, paths := range svcmap { 156 genGoHTTP(paths, svcname, filepath.Join(testdir, "test"), "", "test") 157 } 158 } 159 160 func Test_loadApiPanic(t *testing.T) { 161 assert.Panics(t, func() { 162 loadAPI("notexists.json") 163 }) 164 } 165 166 func Test_loadApiJsonUnmarshalPanic(t *testing.T) { 167 assert.Panics(t, func() { 168 loadAPI("../testdata/test4.json") 169 }) 170 } 171 172 func Test_genGoHttp_Omit(t *testing.T) { 173 testdir := pathutils.Abs("../testdata") 174 api := loadAPI(path.Join(testdir, "petstore3.json")) 175 omitempty = true 176 schemas = api.Components.Schemas 177 requestBodies = api.Components.RequestBodies 178 svcmap := make(map[string]map[string]v3.Path) 179 for endpoint, path := range api.Paths { 180 svcname := strings.Split(strings.Trim(endpoint, "/"), "/")[0] 181 if value, exists := svcmap[svcname]; exists { 182 value[endpoint] = path 183 } else { 184 svcmap[svcname] = make(map[string]v3.Path) 185 svcmap[svcname][endpoint] = path 186 } 187 } 188 189 for svcname, paths := range svcmap { 190 genGoHTTP(paths, svcname, filepath.Join(testdir, "test"), "", "test") 191 } 192 } 193 194 func Example1() { 195 a := []int{1, 2, 3} 196 ret, _ := json.Marshal(a) 197 fmt.Println(string(ret)) 198 199 var _ret []int 200 b := `[1,2,3]` 201 err := json.Unmarshal([]byte(b), &_ret) 202 if err != nil { 203 panic(err) 204 } 205 fmt.Println(_ret) 206 // Output: 207 // [1,2,3] 208 //[1 2 3] 209 } 210 211 func Example2() { 212 a := [][]float64{{1.0, 2.4, 3.7}, {1.0, 2.4, 3.7}, {1.0, 2.4, 3.7}} 213 ret, _ := json.Marshal(a) 214 fmt.Println(string(ret)) 215 216 var _ret [][]float64 217 b := `[[1,2.4,3.7],[1,2.4,3.7],[1,2.4,3.7]]` 218 err := json.Unmarshal([]byte(b), &_ret) 219 if err != nil { 220 panic(err) 221 } 222 fmt.Println(_ret) 223 // Output: 224 // [[1,2.4,3.7],[1,2.4,3.7],[1,2.4,3.7]] 225 //[[1 2.4 3.7] [1 2.4 3.7] [1 2.4 3.7]] 226 } 227 228 func Example3() { 229 a := 15 230 ret, _ := json.Marshal(a) 231 fmt.Println(string(ret)) 232 233 var _ret int 234 b := `15` 235 err := json.Unmarshal([]byte(b), &_ret) 236 if err != nil { 237 panic(err) 238 } 239 fmt.Println(_ret) 240 // Output: 241 // 15 242 //15 243 244 } 245 246 func Example4() { 247 a := "a normal string" 248 ret, _ := json.Marshal(a) 249 fmt.Println(string(ret)) 250 251 var _ret string 252 b := `"a normal string"` 253 err := json.Unmarshal([]byte(b), &_ret) 254 if err != nil { 255 panic(err) 256 } 257 fmt.Println(_ret) 258 // Output: 259 // "a normal string" 260 //a normal string 261 } 262 263 func Example5() { 264 a := []string{"a normal string"} 265 ret, _ := json.Marshal(a) 266 fmt.Println(string(ret)) 267 268 var _ret []string 269 b := `["a normal string"]` 270 err := json.Unmarshal([]byte(b), &_ret) 271 if err != nil { 272 panic(err) 273 } 274 fmt.Println(_ret) 275 // Output: 276 // ["a normal string"] 277 //[a normal string] 278 } 279 280 func Test_toMethod(t *testing.T) { 281 type args struct { 282 endpoint string 283 } 284 tests := []struct { 285 name string 286 args args 287 want string 288 }{ 289 { 290 name: "", 291 args: args{ 292 endpoint: "/apps/a32(34/~name/{id}", 293 }, 294 want: "AppsA3234NameId", 295 }, 296 { 297 name: "", 298 args: args{ 299 endpoint: "/678/9apps/a32(34/~name/{id}", 300 }, 301 want: "AppsA3234NameId6789", 302 }, 303 } 304 for _, tt := range tests { 305 t.Run(tt.name, func(t *testing.T) { 306 if got := toMethod(tt.args.endpoint); got != tt.want { 307 t.Errorf("toMethod() = %v, want %v", got, tt.want) 308 } 309 }) 310 } 311 } 312 313 func Test_httpMethod(t *testing.T) { 314 type args struct { 315 method string 316 } 317 tests := []struct { 318 name string 319 args args 320 want string 321 }{ 322 { 323 name: "", 324 args: args{ 325 method: "CreateUser", 326 }, 327 want: "POST", 328 }, 329 { 330 name: "", 331 args: args{ 332 method: "GetUserInfo", 333 }, 334 want: "GET", 335 }, 336 } 337 for _, tt := range tests { 338 t.Run(tt.name, func(t *testing.T) { 339 if got := httpMethod(tt.args.method); got != tt.want { 340 t.Errorf("httpMethod() = %v, want %v", got, tt.want) 341 } 342 }) 343 } 344 } 345 346 func TestGenGoClient(t *testing.T) { 347 dir := "../testdata/testclient" 348 defer func(path string) { 349 _ = os.RemoveAll(path) 350 }(dir) 351 assert.NotPanics(t, func() { 352 GenGoClient(dir, "../testdata/petstore3.json", true, "", "client") 353 }) 354 } 355 356 func Test_operation2Method(t *testing.T) { 357 type args struct { 358 endpoint string 359 httpMethod string 360 operation *v3.Operation 361 gparams []v3.Parameter 362 } 363 tests := []struct { 364 name string 365 args args 366 wantErr bool 367 }{ 368 { 369 name: "", 370 args: args{ 371 endpoint: "/test/operation2Mthod/{pid}", 372 httpMethod: "GET", 373 operation: &v3.Operation{ 374 Tags: []string{"test"}, 375 Summary: "This is only for test", 376 Description: "Description for test", 377 OperationID: "TestOperation2Mthod", 378 Parameters: []v3.Parameter{}, 379 RequestBody: &v3.RequestBody{ 380 Description: "This is a description", 381 Content: &v3.Content{ 382 FormURL: &v3.MediaType{ 383 Schema: &v3.Schema{ 384 Type: "object", 385 Properties: map[string]*v3.Schema{ 386 "id": v3.Int64, 387 "name": v3.String, 388 "score": v3.Float64, 389 "isBoy": v3.Bool, 390 }, 391 }, 392 }, 393 }, 394 Required: true, 395 }, 396 Responses: &v3.Responses{ 397 Resp200: &v3.Response{ 398 Description: "this is a response", 399 Content: &v3.Content{ 400 JSON: &v3.MediaType{ 401 Schema: &v3.Schema{ 402 Type: "object", 403 Properties: map[string]*v3.Schema{ 404 "code": v3.Int, 405 "data": v3.String, 406 "err": v3.String, 407 }, 408 }, 409 }, 410 }, 411 }, 412 }, 413 }, 414 gparams: []v3.Parameter{ 415 { 416 Name: "companyId", 417 In: v3.InQuery, 418 Description: "company ID", 419 Required: true, 420 Schema: v3.Int64, 421 }, 422 { 423 Name: "name", 424 In: v3.InQuery, 425 Description: "user name", 426 Required: true, 427 Deprecated: false, 428 Schema: v3.String, 429 }, 430 { 431 Name: "pid", 432 In: v3.InPath, 433 Description: "project Id", 434 Required: false, 435 Deprecated: false, 436 Schema: v3.Int64, 437 }, 438 { 439 Name: "token", 440 In: v3.InHeader, 441 Description: "user token", 442 Required: true, 443 Deprecated: false, 444 Schema: v3.String, 445 }, 446 }, 447 }, 448 wantErr: false, 449 }, 450 { 451 name: "", 452 args: args{ 453 endpoint: "/test/operation2Mthod/{pid}", 454 httpMethod: "GET", 455 operation: &v3.Operation{ 456 Tags: []string{"test"}, 457 Summary: "This is only for test", 458 Description: "Description for test", 459 OperationID: "TestOperation2Mthod", 460 Parameters: []v3.Parameter{}, 461 RequestBody: &v3.RequestBody{ 462 Description: "This is a description", 463 Content: &v3.Content{ 464 FormData: &v3.MediaType{ 465 Schema: &v3.Schema{ 466 Type: "object", 467 Properties: map[string]*v3.Schema{ 468 "id": v3.Int64, 469 "name": v3.String, 470 "score": v3.Float64, 471 "isBoy": v3.Bool, 472 "photoes": v3.FileArray, 473 "doc": v3.File, 474 }, 475 }, 476 }, 477 }, 478 Required: true, 479 }, 480 Responses: &v3.Responses{ 481 Resp200: &v3.Response{ 482 Description: "this is a response", 483 Content: &v3.Content{ 484 JSON: &v3.MediaType{ 485 Schema: &v3.Schema{ 486 Type: "object", 487 Properties: map[string]*v3.Schema{ 488 "code": v3.Int, 489 "data": v3.String, 490 "err": v3.String, 491 }, 492 }, 493 }, 494 }, 495 }, 496 }, 497 }, 498 }, 499 wantErr: false, 500 }, 501 { 502 name: "", 503 args: args{ 504 endpoint: "/test/operation2Mthod/{pid}", 505 httpMethod: "GET", 506 operation: &v3.Operation{ 507 Tags: []string{"test"}, 508 Summary: "This is only for test", 509 Description: "Description for test", 510 OperationID: "TestOperation2Mthod", 511 Parameters: []v3.Parameter{}, 512 RequestBody: &v3.RequestBody{ 513 Description: "This is a description", 514 Content: &v3.Content{ 515 FormData: &v3.MediaType{ 516 Schema: &v3.Schema{ 517 Type: "object", 518 Properties: map[string]*v3.Schema{ 519 "id": v3.Int64, 520 "name": v3.String, 521 "score": v3.Float64, 522 "isBoy": v3.Bool, 523 "photoes": v3.FileArray, 524 "doc": v3.File, 525 }, 526 }, 527 }, 528 }, 529 Required: true, 530 }, 531 Responses: &v3.Responses{ 532 Resp200: &v3.Response{ 533 Description: "this is a response", 534 Content: &v3.Content{ 535 Stream: &v3.MediaType{ 536 Schema: v3.File, 537 }, 538 }, 539 }, 540 }, 541 }, 542 }, 543 wantErr: false, 544 }, 545 { 546 name: "", 547 args: args{ 548 endpoint: "/test/operation2Mthod/{pid}", 549 httpMethod: "GET", 550 operation: &v3.Operation{ 551 Tags: []string{"test"}, 552 Summary: "This is only for test", 553 Description: "Description for test", 554 OperationID: "TestOperation2Mthod", 555 Parameters: []v3.Parameter{}, 556 RequestBody: &v3.RequestBody{ 557 Description: "This is a description", 558 Content: &v3.Content{ 559 TextPlain: &v3.MediaType{ 560 Schema: &v3.Schema{ 561 Type: "object", 562 Properties: map[string]*v3.Schema{ 563 "id": v3.Int64, 564 "name": v3.String, 565 "score": v3.Float64, 566 "isBoy": v3.Bool, 567 }, 568 }, 569 }, 570 }, 571 Required: true, 572 }, 573 Responses: &v3.Responses{ 574 Resp200: &v3.Response{ 575 Description: "this is a response", 576 Content: &v3.Content{ 577 TextPlain: &v3.MediaType{ 578 Schema: &v3.Schema{ 579 Type: "object", 580 Properties: map[string]*v3.Schema{ 581 "code": v3.Int, 582 "data": v3.String, 583 "err": v3.String, 584 }, 585 }, 586 }, 587 }, 588 }, 589 }, 590 }, 591 }, 592 wantErr: false, 593 }, 594 } 595 for _, tt := range tests { 596 t.Run(tt.name, func(t *testing.T) { 597 assert.NotPanics(t, func() { 598 _, _ = operation2Method(tt.args.endpoint, tt.args.httpMethod, tt.args.operation, tt.args.gparams) 599 }) 600 }) 601 } 602 } 603 604 func Test_genGoVo_api(t *testing.T) { 605 testdir := pathutils.Abs("../testdata") 606 api := loadAPI(path.Join(testdir, "api-docs.json")) 607 schemas = api.Components.Schemas 608 requestBodies = api.Components.RequestBodies 609 svcmap := make(map[string]map[string]v3.Path) 610 for endpoint, path := range api.Paths { 611 svcname := strings.Split(strings.Trim(endpoint, "/"), "/")[0] 612 if value, exists := svcmap[svcname]; exists { 613 value[endpoint] = path 614 } else { 615 svcmap[svcname] = make(map[string]v3.Path) 616 svcmap[svcname][endpoint] = path 617 } 618 } 619 620 for svcname, paths := range svcmap { 621 genGoHTTP(paths, svcname, filepath.Join(testdir, "test"), "", "test") 622 } 623 } 624 625 func Test_genGoVoJava(t *testing.T) { 626 testdir := pathutils.Abs("../testdata") 627 api := loadAPI(path.Join(testdir, "api-docs.json")) 628 genGoVo(api.Components.Schemas, filepath.Join(testdir, "test", "vo.go"), "test") 629 }