-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathpopulate_util_test.go
279 lines (272 loc) · 8.7 KB
/
populate_util_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package restful_test
import (
"errors"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"trpc.group/trpc-go/trpc-go/restful"
"trpc.group/trpc-go/trpc-go/testdata/restful/helloworld"
)
func TestPopulateMessage(t *testing.T) {
for _, test := range []struct {
msg proto.Message
fieldPath []string
values []string
want proto.Message
wantErr bool
desc string
}{
{
msg: &helloworld.HelloRequest{},
fieldPath: nil,
values: nil,
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test empty fieldpath or fieldvalues",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"foobar"},
values: []string{"anything"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test field name not found",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"repeatedEnumValue", "x"},
values: []string{"anything"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test wrong fieldpath",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"name"},
values: []string{"name1", "name2"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test populate field with multi values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"mapped_string_value"},
values: []string{"key", "value", "anything"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test populate map field with wrong len of values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"mapped_enum_value"},
values: []string{"key", "value"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test populate map field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"enum_value"},
values: []string{"11"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse enum field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"enum_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse enum field with non numeric values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"primitive_bool_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse bool type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"primitive_int32_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse int32 type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"primitive_int64_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse int64 type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"primitive_uint32_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse uint32 type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"primitive_uint64_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse uint64 type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"primitive_float_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse float type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"primitive_double_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse double type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"primitive_bytes_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse bytes type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"mask_value"},
values: []string{"a.b.c,d.e.f"},
want: &helloworld.HelloRequest{
MaskValue: &fieldmaskpb.FieldMask{Paths: []string{"a.b.c", "d.e.f"}},
},
wantErr: false,
desc: "populating message test parse fieldmask type field",
},
} {
err := restful.PopulateMessage(test.msg, test.fieldPath, test.values)
if test.wantErr {
require.NotNil(t, err, test.desc)
} else {
require.Nil(t, err, test.desc)
require.Equal(t, "", cmp.Diff(test.want, test.msg, protocmp.Transform()), test.desc)
}
}
}
func TestPopulateMessageWrappedField(t *testing.T) {
for _, test := range []struct {
msg proto.Message
fieldPath []string
values []string
want proto.Message
wantErr bool
desc string
}{
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"wrapped_bool_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse wrapped bool type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"wrapped_float_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse wrapped float type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"wrapped_double_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse wrapped double type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"wrapped_int32_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse wrapped int32 type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"wrapped_int64_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse wrapped int64 type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"wrapped_uint32_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse wrapped uint32 type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"wrapped_uint64_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse wrapped uint64 type field with invalid values",
},
{
msg: &helloworld.HelloRequest{},
fieldPath: []string{"wrapped_bytes_value"},
values: []string{"foo"},
want: &helloworld.HelloRequest{},
wantErr: true,
desc: "populating message test parse wrapped bytes type field with invalid values",
},
} {
err := restful.PopulateMessage(test.msg, test.fieldPath, test.values)
if test.wantErr {
require.NotNil(t, err, test.desc)
} else {
require.Nil(t, err, test.desc)
require.True(t, reflect.DeepEqual(test.want, test.msg), test.desc)
}
}
}
func TestPopulateMessageTraverseNotFound(t *testing.T) {
msg := &helloworld.HelloRequest{}
fieldPath := []string{"foobar"}
values := []string{"anything"}
err := restful.PopulateMessage(msg, fieldPath, values)
require.True(t, errors.Is(err, restful.ErrTraverseNotFound))
}