-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgen_repeated_test.go
More file actions
239 lines (209 loc) · 7.34 KB
/
gen_repeated_test.go
File metadata and controls
239 lines (209 loc) · 7.34 KB
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
package fauxrpc_test
import (
"testing"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"github.com/brianvoe/gofakeit/v7"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/sudorandom/fauxrpc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"
testv1 "github.com/sudorandom/fauxrpc/private/gen/test/v1"
)
// A custom stub finder to control message generation for testing uniqueness.
type uniqueTestStubFinder struct {
callCount int
stubs []protoreflect.ProtoMessage
}
func (f *uniqueTestStubFinder) FindStub(name protoreflect.FullName, faker *gofakeit.Faker) protoreflect.ProtoMessage {
if len(f.stubs) == 0 {
return nil
}
// Cycle through the stubs to create duplicates
stub := f.stubs[f.callCount%len(f.stubs)]
f.callCount++
return stub
}
func TestRepeated(t *testing.T) {
md := testv1.File_test_v1_test_proto.Messages().ByName("ParameterValues")
repeatedStringField := md.Fields().ByName("enum_list") // Using an existing repeated field
require.NotNil(t, repeatedStringField)
msg := dynamicpb.NewMessage(md)
t.Run("simple repeated field", func(t *testing.T) {
opts := fauxrpc.GenOptions{MaxDepth: 5, Faker: gofakeit.New(0)}
val := fauxrpc.Repeated(msg, repeatedStringField, opts)
require.NotNil(t, val)
assert.GreaterOrEqual(t, val.List().Len(), 0)
assert.LessOrEqual(t, val.List().Len(), 4)
})
t.Run("min_items rule", func(t *testing.T) {
minItems := uint64(5)
fd := createFieldDescriptorWithConstraints(repeatedStringField, &validate.FieldRules{
Type: &validate.FieldRules_Repeated{
Repeated: &validate.RepeatedRules{
MinItems: &minItems,
},
},
})
opts := fauxrpc.GenOptions{MaxDepth: 5, Faker: gofakeit.New(0)}
val := fauxrpc.Repeated(msg, fd, opts)
require.NotNil(t, val)
assert.GreaterOrEqual(t, val.List().Len(), int(minItems))
})
t.Run("max_items rule", func(t *testing.T) {
maxItems := uint64(2)
fd := createFieldDescriptorWithConstraints(repeatedStringField, &validate.FieldRules{
Type: &validate.FieldRules_Repeated{
Repeated: &validate.RepeatedRules{
MaxItems: &maxItems,
},
},
})
opts := fauxrpc.GenOptions{MaxDepth: 5, Faker: gofakeit.New(0)}
val := fauxrpc.Repeated(msg, fd, opts)
require.NotNil(t, val)
assert.LessOrEqual(t, val.List().Len(), int(maxItems))
})
t.Run("min_items and max_items rules", func(t *testing.T) {
minItems := uint64(3)
maxItems := uint64(7)
fd := createFieldDescriptorWithConstraints(repeatedStringField, &validate.FieldRules{
Type: &validate.FieldRules_Repeated{
Repeated: &validate.RepeatedRules{
MinItems: &minItems,
MaxItems: &maxItems,
},
},
})
opts := fauxrpc.GenOptions{MaxDepth: 5, Faker: gofakeit.New(0)}
val := fauxrpc.Repeated(msg, fd, opts)
require.NotNil(t, val)
assert.GreaterOrEqual(t, val.List().Len(), int(minItems))
assert.LessOrEqual(t, val.List().Len(), int(maxItems))
})
t.Run("unique rule for primitive types", func(t *testing.T) {
unique := true
minItems := uint64(5) // Ensure enough items to test uniqueness
fd := createFieldDescriptorWithConstraints(repeatedStringField, &validate.FieldRules{
Type: &validate.FieldRules_Repeated{
Repeated: &validate.RepeatedRules{
Unique: &unique,
MinItems: &minItems,
},
},
})
opts := fauxrpc.GenOptions{MaxDepth: 5, Faker: gofakeit.New(0)}
val := fauxrpc.Repeated(msg, fd, opts)
require.NotNil(t, val)
// Collect all generated values and check for uniqueness
generatedValues := make(map[any]struct{})
for i := range val.List().Len() {
v := val.List().Get(i).Interface()
_, found := generatedValues[v]
assert.False(t, found, "Duplicate value found: %v", v)
generatedValues[v] = struct{}{}
}
})
t.Run("unique rule for message types", func(t *testing.T) {
md := testv1.File_test_v1_test_proto.Messages().ByName("AllTypes")
require.NotNil(t, md)
repeatedMsgField := md.Fields().ByName("msg_list")
require.NotNil(t, repeatedMsgField)
unique := true
// We have 2 unique stubs, so we should only be able to generate 2 unique items.
// We request between 5 and 10 items.
minItems := uint64(5)
maxItems := uint64(10)
fd := createFieldDescriptorWithConstraints(repeatedMsgField, &validate.FieldRules{
Type: &validate.FieldRules_Repeated{
Repeated: &validate.RepeatedRules{
Unique: &unique,
MinItems: &minItems,
MaxItems: &maxItems,
},
},
})
msg := dynamicpb.NewMessage(md)
stub1 := testv1.AllTypes_builder{StringValue: proto.String("stub1")}.Build()
stub2 := testv1.AllTypes_builder{StringValue: proto.String("stub2")}.Build()
stubFinder := &uniqueTestStubFinder{
stubs: []protoreflect.ProtoMessage{stub1, stub2},
}
opts := fauxrpc.GenOptions{
MaxDepth: 5,
Faker: gofakeit.New(0),
StubFinder: stubFinder,
}
val := fauxrpc.Repeated(msg, fd, opts)
require.NotNil(t, val)
list := val.List()
// Because the stub finder only produces 2 unique items, the generator should
// only be able to create a list with 2 items, despite MinItems being 5.
// This is because it will fail to generate more unique items after exhausting the stubs.
assert.Equal(t, 2, list.Len())
// Verify that the two items are indeed the two unique stubs
generatedValues := make(map[string]struct{})
for i := range list.Len() {
item := list.Get(i).Message().Interface().(*testv1.AllTypes)
key := item.GetStringValue()
_, found := generatedValues[key]
assert.False(t, found, "Duplicate value found: %v", key)
generatedValues[key] = struct{}{}
}
assert.Contains(t, generatedValues, "stub1")
assert.Contains(t, generatedValues, "stub2")
})
t.Run("Items rules", func(t *testing.T) {
md := testv1.File_test_v1_test_proto.Messages().ByName("AllTypes")
require.NotNil(t, md)
repeatedStringField := md.Fields().ByName("string_list")
require.NotNil(t, repeatedStringField)
msg := dynamicpb.NewMessage(md)
constVal := "test_value"
minItems := uint64(1)
fd := createFieldDescriptorWithConstraints(repeatedStringField, &validate.FieldRules{
Type: &validate.FieldRules_Repeated{
Repeated: &validate.RepeatedRules{
MinItems: &minItems,
Items: &validate.FieldRules{
Type: &validate.FieldRules_String_{
String_: &validate.StringRules{
Const: &constVal,
},
},
},
},
},
})
opts := fauxrpc.GenOptions{MaxDepth: 5, Faker: gofakeit.New(0)}
val := fauxrpc.Repeated(msg, fd, opts)
require.NotNil(t, val)
list := val.List()
assert.Greater(t, list.Len(), 0)
for i := range list.Len() {
assert.Equal(t, constVal, list.Get(i).String())
}
})
}
func TestRepeatedEnum(t *testing.T) {
md := testv1.File_test_v1_test_proto.Messages().ByName("RepeatedEnumTest")
require.NotNil(t, md)
opts := fauxrpc.GenOptions{MaxDepth: 5}
getField := func(fieldName string) protoreflect.FieldDescriptor {
fd := md.Fields().ByName(protoreflect.Name(fieldName))
require.NotNil(t, fd, "field %s not found", fieldName)
return fd
}
t.Run("repeated_enum_in", func(t *testing.T) {
fd := getField("repeated_enum_in")
msg := &testv1.RepeatedEnumTest{}
val := fauxrpc.Repeated(msg.ProtoReflect(), fd, opts)
require.NotNil(t, val)
list := val.List()
for i := range list.Len() {
item := list.Get(i)
assert.Contains(t, []protoreflect.EnumNumber{2, 3}, item.Enum())
}
})
}