From 16c61c66578be7008cc2b68250d58b5aecc0a018 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Thu, 21 Apr 2022 21:49:09 +0800 Subject: [PATCH] chore: Embed unit test data (#1812) * Embed unit test data * Add testdata Co-authored-by: anqiansong --- .gitignore | 1 - tools/goctl/api/gogen/gen_test.go | 356 +++--------------- .../gogen/testdata/anonymous_annotation.api | 12 + .../goctl/api/gogen/testdata/ap_ino_info.api | 14 + .../api/gogen/testdata/api_has_middleware.api | 15 + .../api/gogen/testdata/api_has_no_request.api | 4 + tools/goctl/api/gogen/testdata/api_jwt.api | 16 + .../testdata/api_jwt_with_middleware.api | 17 + .../api/gogen/testdata/api_route_test.api | 16 + .../gogen/testdata/has_comment_api_test.api | 20 + .../api/gogen/testdata/has_import_api.api | 16 + .../testdata/has_inline_no_exist_test.api | 16 + tools/goctl/api/gogen/testdata/import_api.api | 3 + .../api/gogen/testdata/invalid_api_file.api | 14 + .../api/gogen/testdata/nest_type_api.api | 10 + .../api/gogen/testdata/no_struct_tag_api.api | 20 + .../api/gogen/testdata/test_api_template.api | 36 ++ .../testdata/test_multi_service_template.api | 27 ++ tools/goctl/api/parser/g4/test/ast_test.go | 9 +- tools/goctl/api/parser/parser_test.go | 4 +- tools/goctl/api/parser/testdata/test.api | 18 + tools/goctl/model/sql/command/command_test.go | 4 +- .../goctl/model/sql/command/testdata/user.sql | 16 + tools/goctl/model/sql/gen/gen_test.go | 4 +- tools/goctl/model/sql/gen/testdata/user.sql | 14 + tools/goctl/model/sql/parser/parser_test.go | 6 +- .../goctl/model/sql/parser/testdata/user.sql | 15 + 27 files changed, 388 insertions(+), 315 deletions(-) create mode 100644 tools/goctl/api/gogen/testdata/anonymous_annotation.api create mode 100644 tools/goctl/api/gogen/testdata/ap_ino_info.api create mode 100644 tools/goctl/api/gogen/testdata/api_has_middleware.api create mode 100644 tools/goctl/api/gogen/testdata/api_has_no_request.api create mode 100644 tools/goctl/api/gogen/testdata/api_jwt.api create mode 100644 tools/goctl/api/gogen/testdata/api_jwt_with_middleware.api create mode 100644 tools/goctl/api/gogen/testdata/api_route_test.api create mode 100644 tools/goctl/api/gogen/testdata/has_comment_api_test.api create mode 100644 tools/goctl/api/gogen/testdata/has_import_api.api create mode 100644 tools/goctl/api/gogen/testdata/has_inline_no_exist_test.api create mode 100644 tools/goctl/api/gogen/testdata/import_api.api create mode 100644 tools/goctl/api/gogen/testdata/invalid_api_file.api create mode 100644 tools/goctl/api/gogen/testdata/nest_type_api.api create mode 100644 tools/goctl/api/gogen/testdata/no_struct_tag_api.api create mode 100644 tools/goctl/api/gogen/testdata/test_api_template.api create mode 100644 tools/goctl/api/gogen/testdata/test_multi_service_template.api create mode 100644 tools/goctl/api/parser/testdata/test.api create mode 100644 tools/goctl/model/sql/command/testdata/user.sql create mode 100644 tools/goctl/model/sql/gen/testdata/user.sql create mode 100644 tools/goctl/model/sql/parser/testdata/user.sql diff --git a/.gitignore b/.gitignore index 4d34eb8d..50158305 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,6 @@ # for test purpose **/adhoc -**/testdata # gitlab ci .cache diff --git a/tools/goctl/api/gogen/gen_test.go b/tools/goctl/api/gogen/gen_test.go index 4b1fbbbd..b1a283a1 100644 --- a/tools/goctl/api/gogen/gen_test.go +++ b/tools/goctl/api/gogen/gen_test.go @@ -1,6 +1,7 @@ package gogen import ( + _ "embed" goformat "go/format" "io/ioutil" "os" @@ -10,314 +11,45 @@ import ( "github.com/stretchr/testify/assert" "github.com/zeromicro/go-zero/tools/goctl/api/parser" + "github.com/zeromicro/go-zero/tools/goctl/rpc/execx" + "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) -const testApiTemplate = ` -info( - title: doc title - desc: "> - doc description first part, - doc description second part<" - version: 1.0 +var ( + //go:embed testdata/test_api_template.api + testApiTemplate string + //go:embed testdata/test_multi_service_template.api + testMultiServiceTemplate string + //go:embed testdata/ap_ino_info.api + apiNoInfo string + //go:embed testdata/invalid_api_file.api + invalidApiFile string + //go:embed testdata/anonymous_annotation.api + anonymousAnnotation string + //go:embed testdata/api_has_middleware.api + apiHasMiddleware string + //go:embed testdata/api_jwt.api + apiJwt string + //go:embed testdata/api_jwt_with_middleware.api + apiJwtWithMiddleware string + //go:embed testdata/api_has_no_request.api + apiHasNoRequest string + //go:embed testdata/api_route_test.api + apiRouteTest string + //go:embed testdata/has_comment_api_test.api + hasCommentApiTest string + //go:embed testdata/has_inline_no_exist_test.api + hasInlineNoExistTest string + //go:embed testdata/import_api.api + importApi string + //go:embed testdata/has_import_api.api + hasImportApi string + //go:embed testdata/no_struct_tag_api.api + noStructTagApi string + //go:embed testdata/nest_type_api.api + nestTypeApi string ) -// TODO: test -// { -type Request struct { // TODO: test - // TODO - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` // } -} // TODO: test - -// TODO: test -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} - -@server( - // C0 - group: greet/s1 -) -// C1 -service A-api { - // C2 - @server( // C3 - handler: GreetHandler - ) - get /greet/from/:name(Request) returns (Response) // hello - - // C4 - @handler NoResponseHandler // C5 - get /greet/get(Request) -} -` - -const testMultiServiceTemplate = ` -info( - title: doc title - desc: doc description first part - version: 1.0 -) - -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} - -service A-api { - @server( - handler: GreetHandler - ) - get /greet/from/:name(Request) returns (Response) -} - -service A-api { - @server( - handler: NoResponseHandler - ) - get /greet/get(Request) -} -` - -const apiNoInfo = ` -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} - -service A-api { - @server( - handler: GreetHandler - ) - get /greet/from/:name(Request) returns (Response) -} -` - -const invalidApiFile = ` -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} - -service A-api - @server( - handler: GreetHandler - ) - get /greet/from/:name(Request) returns (Response) -} -` - -const anonymousAnnotation = ` -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} - -service A-api { - @handler GreetHandler - get /greet/from/:name(Request) returns (Response) -} -` - -const apiHasMiddleware = ` -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} - -@server( - middleware: TokenValidate -) -service A-api { - @handler GreetHandler - get /greet/from/:name(Request) returns (Response) -} -` - -const apiJwt = ` -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} - -@server( - jwt: Auth - signature: true -) -service A-api { - @handler GreetHandler - get /greet/from/:name(Request) returns (Response) -} -` - -const apiJwtWithMiddleware = ` -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} - -@server( - jwt: Auth - jwtTransition: Trans - middleware: TokenValidate -) -service A-api { - @handler GreetHandler - get /greet/from/:name(Request) returns (Response) -} -` - -const apiHasNoRequest = ` -service A-api { - @handler GreetHandler - post /greet/ping () -} -` - -const apiRouteTest = ` -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` -} -service A-api { - @handler NormalHandler - get /greet/from/:name(Request) returns (Response) - @handler NoResponseHandler - get /greet/from/:sex(Request) - @handler NoRequestHandler - get /greet/from/request returns (Response) - @handler NoRequestNoResponseHandler - get /greet/from -} -` - -const hasCommentApiTest = ` -type Inline struct { - -} - -type Request struct { - Inline - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` // name in path -} - -type Response struct { - Message string ` + "`" + `json:"msg"` + "`" + ` // message -} - -service A-api { - @doc ("helloworld") - @server( - handler: GreetHandler - ) - get /greet/from/:name(Request) returns (Response) -} -` - -const hasInlineNoExistTest = ` - -type Request struct { - Inline - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` // message -} - -service A-api { - @doc ("helloworld") - @server( - handler: GreetHandler - ) - get /greet/from/:name(Request) returns (Response) -} -` - -const importApi = ` -type ImportData struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -` - -const hasImportApi = ` -import "importApi.api" - -type Request struct { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type Response struct { - Message string ` + "`" + `json:"message"` + "`" + ` // message -} - -service A-api { - @server( - handler: GreetHandler - ) - get /greet/from/:name(Request) returns (Response) -} -` - -const noStructTagApi = ` -type Request { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` -} - -type XXX {} - -type ( - Response { - Message string ` + "`" + `json:"message"` + "`" + ` - } - - A {} - - B struct {} -) - -service A-api { - @handler GreetHandler - get /greet/from/:name(Request) returns (Response) -} -` - -const nestTypeApi = ` -type Request { - Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` - XXX struct { - } -} - -service A-api { - @handler GreetHandler - get /greet/from/:name(Request) -} -` - func TestParser(t *testing.T) { filename := "greet.api" err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm) @@ -537,8 +269,15 @@ func validate(t *testing.T, api string) { } func validateWithCamel(t *testing.T, api, camel string) { - dir := t.TempDir() - err := DoGenProject(api, dir, camel) + dir := "workspace" + defer func() { + os.RemoveAll(dir) + }() + err := pathx.MkdirIfNotExist(dir) + assert.Nil(t, err) + err = initMod(dir) + assert.Nil(t, err) + err = DoGenProject(api, dir, camel) assert.Nil(t, err) filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if strings.HasSuffix(path, ".go") { @@ -550,6 +289,11 @@ func validateWithCamel(t *testing.T, api, camel string) { }) } +func initMod(mod string) error { + _, err := execx.Run("go mod init "+mod, mod) + return err +} + func validateCode(code string) error { _, err := goformat.Source([]byte(code)) return err diff --git a/tools/goctl/api/gogen/testdata/anonymous_annotation.api b/tools/goctl/api/gogen/testdata/anonymous_annotation.api new file mode 100644 index 00000000..3a3399d4 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/anonymous_annotation.api @@ -0,0 +1,12 @@ +type Request struct { + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` +} + +service A-api { + @handler GreetHandler + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/ap_ino_info.api b/tools/goctl/api/gogen/testdata/ap_ino_info.api new file mode 100644 index 00000000..efc01bde --- /dev/null +++ b/tools/goctl/api/gogen/testdata/ap_ino_info.api @@ -0,0 +1,14 @@ +type Request struct { + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` +} + +service A-api { + @server( + handler: GreetHandler + ) + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/api_has_middleware.api b/tools/goctl/api/gogen/testdata/api_has_middleware.api new file mode 100644 index 00000000..e126c005 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/api_has_middleware.api @@ -0,0 +1,15 @@ +type Request struct { + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` +} + +@server( + middleware: TokenValidate +) +service A-api { + @handler GreetHandler + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/api_has_no_request.api b/tools/goctl/api/gogen/testdata/api_has_no_request.api new file mode 100644 index 00000000..d1fdf0ec --- /dev/null +++ b/tools/goctl/api/gogen/testdata/api_has_no_request.api @@ -0,0 +1,4 @@ +service A-api { + @handler GreetHandler + post /greet/ping () +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/api_jwt.api b/tools/goctl/api/gogen/testdata/api_jwt.api new file mode 100644 index 00000000..f73feaf0 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/api_jwt.api @@ -0,0 +1,16 @@ +type Request struct { + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` +} + +@server( + jwt: Auth + signature: true +) +service A-api { + @handler GreetHandler + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/api_jwt_with_middleware.api b/tools/goctl/api/gogen/testdata/api_jwt_with_middleware.api new file mode 100644 index 00000000..5af90b14 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/api_jwt_with_middleware.api @@ -0,0 +1,17 @@ +type Request struct { + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` +} + +@server( + jwt: Auth + jwtTransition: Trans + middleware: TokenValidate +) +service A-api { + @handler GreetHandler + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/api_route_test.api b/tools/goctl/api/gogen/testdata/api_route_test.api new file mode 100644 index 00000000..691461e4 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/api_route_test.api @@ -0,0 +1,16 @@ +type Request struct { + Name string `path:"name,options=you|me"` +} +type Response struct { + Message string `json:"message"` +} +service A-api { + @handler NormalHandler + get /greet/from/:name(Request) returns (Response) + @handler NoResponseHandler + get /greet/from/:sex(Request) + @handler NoRequestHandler + get /greet/from/request returns (Response) + @handler NoRequestNoResponseHandler + get /greet/from +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/has_comment_api_test.api b/tools/goctl/api/gogen/testdata/has_comment_api_test.api new file mode 100644 index 00000000..3bd89f8e --- /dev/null +++ b/tools/goctl/api/gogen/testdata/has_comment_api_test.api @@ -0,0 +1,20 @@ +type Inline struct { + +} + +type Request struct { + Inline + Name string `path:"name,options=you|me"` // name in path +} + +type Response struct { + Message string `json:"msg"` // message +} + +service A-api { + @doc ("helloworld") + @server( + handler: GreetHandler + ) + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/has_import_api.api b/tools/goctl/api/gogen/testdata/has_import_api.api new file mode 100644 index 00000000..6710280e --- /dev/null +++ b/tools/goctl/api/gogen/testdata/has_import_api.api @@ -0,0 +1,16 @@ +import "importApi.api" + +type Request struct { + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` // message +} + +service A-api { + @server( + handler: GreetHandler + ) + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/has_inline_no_exist_test.api b/tools/goctl/api/gogen/testdata/has_inline_no_exist_test.api new file mode 100644 index 00000000..114f3323 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/has_inline_no_exist_test.api @@ -0,0 +1,16 @@ +type Request struct { + Inline + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` // message +} + +service A-api { + @doc ("helloworld") + @server( + handler: GreetHandler + ) + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/import_api.api b/tools/goctl/api/gogen/testdata/import_api.api new file mode 100644 index 00000000..b663997d --- /dev/null +++ b/tools/goctl/api/gogen/testdata/import_api.api @@ -0,0 +1,3 @@ +type ImportData struct { + Name string `path:"name,options=you|me"` +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/invalid_api_file.api b/tools/goctl/api/gogen/testdata/invalid_api_file.api new file mode 100644 index 00000000..46674970 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/invalid_api_file.api @@ -0,0 +1,14 @@ +type Request struct { + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` +} + +service A-api + @server( + handler: GreetHandler + ) + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/nest_type_api.api b/tools/goctl/api/gogen/testdata/nest_type_api.api new file mode 100644 index 00000000..5333f360 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/nest_type_api.api @@ -0,0 +1,10 @@ +type Request { + Name string `path:"name,options=you|me"` + XXX struct { + } +} + +service A-api { + @handler GreetHandler + get /greet/from/:name(Request) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/no_struct_tag_api.api b/tools/goctl/api/gogen/testdata/no_struct_tag_api.api new file mode 100644 index 00000000..95bfacd7 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/no_struct_tag_api.api @@ -0,0 +1,20 @@ +type Request { + Name string `path:"name,options=you|me"` +} + +type XXX {} + +type ( + Response { + Message string `json:"message"` + } + + A {} + + B struct {} +) + +service A-api { + @handler GreetHandler + get /greet/from/:name(Request) returns (Response) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/test_api_template.api b/tools/goctl/api/gogen/testdata/test_api_template.api new file mode 100644 index 00000000..c03ac937 --- /dev/null +++ b/tools/goctl/api/gogen/testdata/test_api_template.api @@ -0,0 +1,36 @@ +info( + title: doc title +desc: "> +doc description first part, +doc description second part<" +version: 1.0 +) + +// TODO: test +// { +type Request struct { // TODO: test + // TODO + Name string `path:"name,options=you|me"` // } +} // TODO: test + +// TODO: test +type Response struct { + Message string `json:"message"` +} + +@server( + // C0 + group: greet/s1 +) + // C1 +service A-api { + // C2 + @server( // C3 + handler: GreetHandler + ) + get /greet/from/:name(Request) returns (Response) // hello + + // C4 + @handler NoResponseHandler // C5 + get /greet/get(Request) +} \ No newline at end of file diff --git a/tools/goctl/api/gogen/testdata/test_multi_service_template.api b/tools/goctl/api/gogen/testdata/test_multi_service_template.api new file mode 100644 index 00000000..eb716aeb --- /dev/null +++ b/tools/goctl/api/gogen/testdata/test_multi_service_template.api @@ -0,0 +1,27 @@ +info( + title: doc title +desc: doc description first part +version: 1.0 +) + +type Request struct { + Name string `path:"name,options=you|me"` +} + +type Response struct { + Message string `json:"message"` +} + +service A-api { + @server( + handler: GreetHandler + ) + get /greet/from/:name(Request) returns (Response) +} + +service A-api { + @server( + handler: NoResponseHandler + ) + get /greet/get(Request) +} \ No newline at end of file diff --git a/tools/goctl/api/parser/g4/test/ast_test.go b/tools/goctl/api/parser/g4/test/ast_test.go index a6a26418..cbd23717 100644 --- a/tools/goctl/api/parser/g4/test/ast_test.go +++ b/tools/goctl/api/parser/g4/test/ast_test.go @@ -1,7 +1,7 @@ package test import ( - "io/ioutil" + _ "embed" "testing" "github.com/stretchr/testify/assert" @@ -9,16 +9,17 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api" ) +//go:embed apis/test.api +var testApi string + var parser = ast.NewParser(ast.WithParserPrefix("test.api"), ast.WithParserDebug()) func TestApi(t *testing.T) { fn := func(p *api.ApiParserParser, visitor *ast.ApiVisitor) interface{} { return p.Api().Accept(visitor) } - content, err := ioutil.ReadFile("./apis/test.api") - assert.Nil(t, err) - v, err := parser.Accept(fn, string(content)) + v, err := parser.Accept(fn, testApi) assert.Nil(t, err) api := v.(*ast.Api) body := &ast.Body{ diff --git a/tools/goctl/api/parser/parser_test.go b/tools/goctl/api/parser/parser_test.go index 272c32d4..1bf05b1c 100644 --- a/tools/goctl/api/parser/parser_test.go +++ b/tools/goctl/api/parser/parser_test.go @@ -1,13 +1,15 @@ package parser import ( + _ "embed" "testing" "github.com/stretchr/testify/assert" "github.com/zeromicro/go-zero/tools/goctl/api/spec" ) -var testApi = "// syntax doc\nsyntax = \"v1\" // syntax comment\n\n// type doc\ntype Request {\n\tName string `path:\"name,options=you|me\"`\n}\n\ntype Response {\n\tMessage string `json:\"message\"`\n}\n\n// service doc\nservice greet-api {\n\t// handler doc\n\t@handler GreetHandler // handler comment\n\tget /from/:name(Request) returns (Response);\n}" +//go:embed testdata/test.api +var testApi string func TestParseContent(t *testing.T) { sp, err := ParseContent(testApi) diff --git a/tools/goctl/api/parser/testdata/test.api b/tools/goctl/api/parser/testdata/test.api new file mode 100644 index 00000000..3d4ae55d --- /dev/null +++ b/tools/goctl/api/parser/testdata/test.api @@ -0,0 +1,18 @@ + // syntax doc + syntax = "v1" // syntax comment + + // type doc + type Request { + Name string `path:"name,options=you|me"` + } + + type Response { + Message string `json:"message"` + } + + // service doc + service greet-api { + // handler doc + @handler GreetHandler // handler comment + get /from/:name(Request) returns (Response); + } \ No newline at end of file diff --git a/tools/goctl/model/sql/command/command_test.go b/tools/goctl/model/sql/command/command_test.go index 5d59655e..ee363213 100644 --- a/tools/goctl/model/sql/command/command_test.go +++ b/tools/goctl/model/sql/command/command_test.go @@ -1,6 +1,7 @@ package command import ( + _ "embed" "io/ioutil" "os" "path/filepath" @@ -13,7 +14,8 @@ import ( ) var ( - sql = "-- 用户表 --\nCREATE TABLE `user` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',\n `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',\n `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',\n `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',\n `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `name_index` (`name`),\n UNIQUE KEY `mobile_index` (`mobile`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;\n\n" + //go:embed testdata/user.sql + sql string cfg = &config.Config{ NamingFormat: "gozero", } diff --git a/tools/goctl/model/sql/command/testdata/user.sql b/tools/goctl/model/sql/command/testdata/user.sql new file mode 100644 index 00000000..f1ab9280 --- /dev/null +++ b/tools/goctl/model/sql/command/testdata/user.sql @@ -0,0 +1,16 @@ +-- 用户表 -- +CREATE TABLE `user` +( + `id` bigint(10) NOT NULL AUTO_INCREMENT, + `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称', + `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码', + `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号', + `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开', + `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称', + `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `name_index` (`name`), + UNIQUE KEY `mobile_index` (`mobile`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + diff --git a/tools/goctl/model/sql/gen/gen_test.go b/tools/goctl/model/sql/gen/gen_test.go index 8c228bcd..b7d8e0fe 100644 --- a/tools/goctl/model/sql/gen/gen_test.go +++ b/tools/goctl/model/sql/gen/gen_test.go @@ -2,6 +2,7 @@ package gen import ( "database/sql" + _ "embed" "io/ioutil" "os" "path" @@ -20,7 +21,8 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) -var source = "CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL,\n `class` bigint NOT NULL,\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;" +//go:embed testdata/user.sql +var source string func TestCacheModel(t *testing.T) { logx.Disable() diff --git a/tools/goctl/model/sql/gen/testdata/user.sql b/tools/goctl/model/sql/gen/testdata/user.sql new file mode 100644 index 00000000..dddbc09a --- /dev/null +++ b/tools/goctl/model/sql/gen/testdata/user.sql @@ -0,0 +1,14 @@ +CREATE TABLE `test_user` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL, + `class` bigint NOT NULL, + `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, + `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `mobile_unique` (`mobile`), + UNIQUE KEY `class_name_unique` (`class`,`name`), + KEY `create_index` (`create_time`), + KEY `name_index` (`name`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; \ No newline at end of file diff --git a/tools/goctl/model/sql/parser/parser_test.go b/tools/goctl/model/sql/parser/parser_test.go index a1809093..b5907f34 100644 --- a/tools/goctl/model/sql/parser/parser_test.go +++ b/tools/goctl/model/sql/parser/parser_test.go @@ -1,6 +1,7 @@ package parser import ( + _ "embed" "io/ioutil" "path/filepath" "testing" @@ -30,9 +31,12 @@ func TestParseSelect(t *testing.T) { assert.Equal(t, 0, len(tables)) } +//go:embed testdata/user.sql +var user string + func TestParseCreateTable(t *testing.T) { sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") - err := ioutil.WriteFile(sqlFile, []byte("CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL comment '手\\t机 号',\n `class` bigint NOT NULL comment '班级',\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL comment '姓\n 名',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP comment '创建\\r时间',\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"), 0o777) + err := ioutil.WriteFile(sqlFile, []byte(user), 0o777) assert.Nil(t, err) tables, err := Parse(sqlFile, "go_zero") diff --git a/tools/goctl/model/sql/parser/testdata/user.sql b/tools/goctl/model/sql/parser/testdata/user.sql new file mode 100644 index 00000000..66749931 --- /dev/null +++ b/tools/goctl/model/sql/parser/testdata/user.sql @@ -0,0 +1,15 @@ +CREATE TABLE `test_user` +( + `id` bigint NOT NULL AUTO_INCREMENT, + `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL comment '手\t机 号', + `class` bigint NOT NULL comment '班级', + `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL comment '姓 + 名', + `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP comment '创建\r时间', + `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `mobile_unique` (`mobile`), + UNIQUE KEY `class_name_unique` (`class`,`name`), + KEY `create_index` (`create_time`), + KEY `name_index` (`name`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; \ No newline at end of file