From ebfca0d08f46e488f488ab897b4608dc41076688 Mon Sep 17 00:00:00 2001 From: Kujtim Date: Wed, 4 Oct 2017 14:25:57 +0200 Subject: [PATCH] Add some additional tests --- generator/add_transport.go | 18 +++--- generator/add_transport_test.go | 108 +++++++++++++++++++++++++++----- generator/generator_test.go | 23 ++++++- 3 files changed, 124 insertions(+), 25 deletions(-) diff --git a/generator/add_transport.go b/generator/add_transport.go index b78fe0e..3f5681a 100644 --- a/generator/add_transport.go +++ b/generator/add_transport.go @@ -61,7 +61,7 @@ func (g *GenerateTransport) Generate() (err error) { if v == g.transport { break } else if n == len(SupportedTransports)-1 { - return errors.New(fmt.Sprintf("transport `%s` not supported", g.transport)) + return errors.New(fmt.Sprintf("transport `%s` not supported", g.transport)) } } if b, err := g.fs.Exists(g.filePath); err != nil { @@ -113,12 +113,12 @@ func (g *GenerateTransport) Generate() (err error) { } logrus.Warn("===============================================================") logrus.Warn("The GRPC implementation is not finished you need to update your") - logrus.Warn("the service proto buffer and run the compile script.") + logrus.Warn(" service proto buffer and run the compile script.") logrus.Warn("---------------------------------------------------------------") logrus.Warn("You also need to implement the Encoders and Decoders!") logrus.Warn("===============================================================") default: - logrus.Warn("This transport type is not yet implemented") + return errors.New("this transport type is not yet implemented") } return } @@ -675,11 +675,13 @@ func (g *generateGRPCTransportProto) Generate() (err error) { if viper.GetString("gk_folder") != "" { g.pbFilePath = path.Join(viper.GetString("gk_folder"), g.pbFilePath) } - cmd := exec.Command("protoc", g.pbFilePath, "--go_out=plugins=grpc:.") - cmd.Stdout = os.Stdout - err = cmd.Run() - if err != nil { - return err + if !viper.GetBool("gk_testing") { + cmd := exec.Command("protoc", g.pbFilePath, "--go_out=plugins=grpc:.") + cmd.Stdout = os.Stdout + err = cmd.Run() + if err != nil { + return err + } } if b, e := g.fs.Exists(g.compileFilePath); e != nil { return e diff --git a/generator/add_transport_test.go b/generator/add_transport_test.go index 0967bba..59584d5 100644 --- a/generator/add_transport_test.go +++ b/generator/add_transport_test.go @@ -187,6 +187,68 @@ type TestService interface{ methods: []string{}, }, wantErr: true, + }, { + name: "Test if grpc create successful", + fields: fields{ + BaseGenerator: func() BaseGenerator { + b := BaseGenerator{} + b.srcFile = jen.NewFilePath("") + b.InitPg() + f := fs.NewDefaultFs("") + f.MkdirAll("test/pkg/service") + f.WriteFile("test/pkg/service/service.go", `package service + import "context" + type TestService interface{ + Foo(ctx context.Context, a string)(a int, err error) + }`, true) + b.fs = f + return b + }(), + name: "test", + file: func() *parser.File { + f := fs.Get() + s, _ := f.ReadFile("test/pkg/service/service.go") + fl, _ := parser.NewFileParser().Parse([]byte(s)) + return fl + }(), + interfaceName: "TestService", + transport: "grpc", + filePath: "test/pkg/service/service.go", + destPath: "test/pkg/service", + methods: []string{}, + }, + wantErr: false, + }, { + name: "Test if http create successful", + fields: fields{ + BaseGenerator: func() BaseGenerator { + b := BaseGenerator{} + b.srcFile = jen.NewFilePath("") + b.InitPg() + f := fs.NewDefaultFs("") + f.MkdirAll("test/pkg/service") + f.WriteFile("test/pkg/service/service.go", `package service + import "context" + type TestService interface{ + Foo(ctx context.Context, a string)(a int, err error) + }`, true) + b.fs = f + return b + }(), + name: "test", + file: func() *parser.File { + f := fs.Get() + s, _ := f.ReadFile("test/pkg/service/service.go") + fl, _ := parser.NewFileParser().Parse([]byte(s)) + return fl + }(), + interfaceName: "TestService", + transport: "http", + filePath: "test/pkg/service/service.go", + destPath: "test/pkg/service", + methods: []string{}, + }, + wantErr: false, }, } for _, tt := range tests { @@ -356,7 +418,7 @@ func TestGenerateTransport_removeBadMethods(t *testing.T) { want []parser.Method }{ { - name: "Test if service interface is not found", + name: "Test if it does not remove wanted methods", fields: fields{ BaseGenerator: func() BaseGenerator { b := BaseGenerator{} @@ -408,7 +470,7 @@ func TestGenerateTransport_removeBadMethods(t *testing.T) { }, }, { - name: "Test if service interface is not found", + name: "Test if it removes unwanted methods", fields: fields{ BaseGenerator: func() BaseGenerator { b := BaseGenerator{} @@ -555,7 +617,7 @@ func TestGenerateTransport_removeUnwantedMethods(t *testing.T) { ), }, }, { - name: "Test if only selected methods are generated", + name: "Test if only ot does not do anything if no methods selected", fields: fields{ BaseGenerator: func() BaseGenerator { b := BaseGenerator{} @@ -567,6 +629,7 @@ func TestGenerateTransport_removeUnwantedMethods(t *testing.T) { import "context" type TestService interface { Foo(ctx context.Context,a int)(r string, err error) + Bar(ctx context.Context,a int)(r string, err error) } `, true) b.fs = f return b @@ -604,6 +667,19 @@ func TestGenerateTransport_removeUnwantedMethods(t *testing.T) { parser.NewNameType("err", "error"), }, ), + parser.NewMethod( + "Bar", + parser.NamedTypeValue{}, + "", + []parser.NamedTypeValue{ + parser.NewNameType("ctx", "context.Context"), + parser.NewNameType("a", "int"), + }, + []parser.NamedTypeValue{ + parser.NewNameType("r", "string"), + parser.NewNameType("err", "error"), + }, + ), }, }, } @@ -639,7 +715,7 @@ func Test_newGenerateHTTPTransport(t *testing.T) { args args want Gen }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -667,7 +743,7 @@ func Test_generateHTTPTransport_Generate(t *testing.T) { fields fields wantErr bool }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -701,7 +777,7 @@ func Test_newGenerateHTTPTransportBase(t *testing.T) { args args want Gen }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -730,7 +806,7 @@ func Test_generateHTTPTransportBase_Generate(t *testing.T) { fields fields wantErr bool }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -764,7 +840,7 @@ func Test_newGenerateGRPCTransportProto(t *testing.T) { args args want Gen }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -793,7 +869,7 @@ func Test_generateGRPCTransportProto_Generate(t *testing.T) { fields fields wantErr bool }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -834,7 +910,7 @@ func Test_generateGRPCTransportProto_getService(t *testing.T) { fields fields want *proto.Service }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -874,7 +950,7 @@ func Test_generateGRPCTransportProto_generateRequestResponse(t *testing.T) { name string fields fields }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -916,7 +992,7 @@ func Test_generateGRPCTransportProto_getServiceRPC(t *testing.T) { fields fields args args }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -949,7 +1025,7 @@ func Test_newGenerateGRPCTransportBase(t *testing.T) { args args want Gen }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -978,7 +1054,7 @@ func Test_generateGRPCTransportBase_Generate(t *testing.T) { fields fields wantErr bool }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1012,7 +1088,7 @@ func Test_newGenerateGRPCTransport(t *testing.T) { args args want Gen }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1040,7 +1116,7 @@ func Test_generateGRPCTransport_Generate(t *testing.T) { fields fields wantErr bool }{ - // TODO: Add test cases. + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/generator/generator_test.go b/generator/generator_test.go index 41d396e..7a5a40c 100644 --- a/generator/generator_test.go +++ b/generator/generator_test.go @@ -3,6 +3,8 @@ package generator import ( "path" + "runtime" + "github.com/kujtimiihoxha/kit/parser" "github.com/kujtimiihoxha/kit/utils" "github.com/spf13/viper" @@ -10,16 +12,35 @@ import ( func setDefaults() { viper.SetDefault("gk_service_path_format", path.Join("%s", "pkg", "service")) - viper.SetDefault("gk_cmd_path_format", path.Join("%s", "cmd", "service")) + viper.SetDefault("gk_cmd_service_path_format", path.Join("%s", "cmd", "service")) + viper.SetDefault("gk_cmd_path_format", path.Join("%s", "cmd")) viper.SetDefault("gk_endpoint_path_format", path.Join("%s", "pkg", "endpoint")) viper.SetDefault("gk_http_path_format", path.Join("%s", "pkg", "http")) viper.SetDefault("gk_http_client_path_format", path.Join("%s", "client", "http")) + viper.SetDefault("gk_grpc_client_path_format", path.Join("%s", "client", "grpc")) viper.SetDefault("gk_client_cmd_path_format", path.Join("%s", "cmd", "client")) + viper.SetDefault("gk_grpc_path_format", path.Join("%s", "pkg", "grpc")) + viper.SetDefault("gk_grpc_pb_path_format", path.Join("%s", "pkg", "grpc", "pb")) + viper.SetDefault("gk_service_file_name", "service.go") viper.SetDefault("gk_service_middleware_file_name", "middleware.go") + viper.SetDefault("gk_endpoint_base_file_name", "endpoint_gen.go") viper.SetDefault("gk_endpoint_file_name", "endpoint.go") viper.SetDefault("gk_endpoint_middleware_file_name", "middleware.go") viper.SetDefault("gk_http_file_name", "handler.go") + viper.SetDefault("gk_http_base_file_name", "handler_gen.go") + viper.SetDefault("gk_cmd_base_file_name", "service_gen.go") + viper.SetDefault("gk_cmd_svc_file_name", "service.go") + viper.SetDefault("gk_http_client_file_name", "http.go") + viper.SetDefault("gk_grpc_client_file_name", "grpc.go") + viper.SetDefault("gk_grpc_pb_file_name", "%s.proto") + viper.SetDefault("gk_grpc_base_file_name", "handler_gen.go") + viper.SetDefault("gk_grpc_file_name", "handler.go") + if runtime.GOOS == "windows" { + viper.SetDefault("gk_grpc_compile_file_name", "compile.bat") + } else { + viper.SetDefault("gk_grpc_compile_file_name", "compile.sh") + } viper.SetDefault("gk_service_struct_prefix", "basic") viper.Set("gk_testing", true)