forked from googleapis/gapic-generator-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgengrpc_test.go
126 lines (115 loc) · 3.4 KB
/
gengrpc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gengapic
import (
"bytes"
"path/filepath"
"testing"
conf "github.com/googleapis/gapic-generator-go/internal/grpc_service_config"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
"github.com/googleapis/gapic-generator-go/internal/txtdiff"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/genproto/googleapis/api/serviceconfig"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
duration "google.golang.org/protobuf/types/known/durationpb"
)
func TestServiceRenaming(t *testing.T) {
inputType := &descriptorpb.DescriptorProto{
Name: proto.String("InputType"),
}
outputType := &descriptorpb.DescriptorProto{
Name: proto.String("OutputType"),
}
metadataType := &descriptorpb.DescriptorProto{
Name: proto.String("MetadataType"),
}
file := &descriptorpb.FileDescriptorProto{
Package: proto.String("my.pkg"),
Options: &descriptorpb.FileOptions{
GoPackage: proto.String("mypackage"),
},
}
serv := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("Foo"),
}
var g generator
g.imports = map[pbinfo.ImportSpec]bool{}
g.opts = &options{
pkgName: "pkg",
transports: []transport{grpc},
}
cpb := &conf.ServiceConfig{
MethodConfig: []*conf.MethodConfig{
{
Name: []*conf.MethodConfig_Name{
{
Service: "my.pkg.Foo",
},
},
Timeout: &duration.Duration{Seconds: 10},
},
},
}
data, err := protojson.Marshal(cpb)
if err != nil {
t.Error(err)
}
in := bytes.NewReader(data)
g.grpcConf, err = conf.New(in)
if err != nil {
t.Error(err)
}
commonTypes(&g)
for _, typ := range []*descriptorpb.DescriptorProto{
inputType, outputType, metadataType,
} {
g.descInfo.Type[".my.pkg."+*typ.Name] = typ
g.descInfo.ParentFile[typ] = file
}
g.descInfo.ParentFile[serv] = file
g.descInfo.ParentElement = map[pbinfo.ProtoType]pbinfo.ProtoType{}
g.serviceConfig = &serviceconfig.Service{
Publishing: &annotations.Publishing{
LibrarySettings: []*annotations.ClientLibrarySettings{
{
GoSettings: &annotations.GoSettings{
RenamedServices: map[string]string{"Foo": "Bar"},
},
},
},
},
}
m := &descriptorpb.MethodDescriptorProto{
Name: proto.String("Baz"),
InputType: proto.String(".my.pkg.InputType"),
OutputType: proto.String(emptyType),
}
serv.Method = []*descriptorpb.MethodDescriptorProto{
m,
}
g.descInfo.ParentFile[serv] = file
g.descInfo.ParentElement[m] = serv
imp, err := g.descInfo.ImportSpec(serv)
if err != nil {
t.Fatal(err)
}
// Test the generation of the client boilerplate and single gRPC method rename.
g.grpcClientInit(serv, "Bar", imp, false)
if err := g.genGRPCMethod("Bar", serv, m); err != nil {
t.Fatal(err)
}
txtdiff.Diff(t, g.pt.String(), filepath.Join("testdata", "service_rename.want"))
}