Skip to content

Commit

Permalink
Add some additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kujtim committed Oct 4, 2017
1 parent 84d43cb commit ebfca0d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 25 deletions.
18 changes: 10 additions & 8 deletions generator/add_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
108 changes: 92 additions & 16 deletions generator/add_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand All @@ -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
Expand Down Expand Up @@ -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"),
},
),
},
},
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,44 @@ package generator
import (
"path"

"runtime"

"github.com/kujtimiihoxha/kit/parser"
"github.com/kujtimiihoxha/kit/utils"
"github.com/spf13/viper"
)

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)

Expand Down

0 comments on commit ebfca0d

Please sign in to comment.