Skip to content

Commit 7219478

Browse files
authored
feat(tests): More faker options (#287)
This is so that we can remove most of the `cloudquery/faker` usage. See cloudquery/cloudquery#2726
1 parent 93fe5a6 commit 7219478

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

faker/faker.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ import (
55
"math/rand"
66
"reflect"
77
"time"
8+
9+
"github.com/rs/zerolog"
810
)
911

1012
type faker struct {
1113
maxDepth int
14+
logger zerolog.Logger
1215
}
1316

14-
type Option func(*faker)
17+
var errEFaceNotAllowed = fmt.Errorf("interface{} not allowed")
1518

1619
func (f faker) getFakedValue(a interface{}) (reflect.Value, error) {
1720
t := reflect.TypeOf(a)
1821
if t == nil {
19-
return reflect.Value{}, fmt.Errorf("interface{} not allowed")
22+
return reflect.Value{}, errEFaceNotAllowed
2023
}
2124
f.maxDepth--
2225
if f.maxDepth < 0 {
@@ -52,9 +55,12 @@ func (f faker) getFakedValue(a interface{}) (reflect.Value, error) {
5255
}
5356
val, err := f.getFakedValue(v.Field(i).Interface())
5457
if err != nil {
55-
fmt.Println(err)
58+
if err == errEFaceNotAllowed { // skip empty interfaces
59+
continue
60+
}
61+
62+
f.logger.Err(err).Str("field_name", v.Type().Field(i).Name).Msg("faker: error while faking struct")
5663
continue
57-
// return reflect.Value{}, err
5864
}
5965
val = val.Convert(v.Field(i).Type())
6066
v.Field(i).Set(val)
@@ -126,6 +132,7 @@ func (f faker) getFakedValue(a interface{}) (reflect.Value, error) {
126132
if err != nil {
127133
return reflect.Value{}, err
128134
}
135+
key = key.Convert(t.Key())
129136

130137
valueInstance := reflect.New(t.Elem()).Elem().Interface()
131138
val, err := f.getFakedValue(valueInstance)
@@ -142,12 +149,6 @@ func (f faker) getFakedValue(a interface{}) (reflect.Value, error) {
142149
}
143150
}
144151

145-
func WithMaxDepth(depth int) Option {
146-
return func(f *faker) {
147-
f.maxDepth = depth
148-
}
149-
}
150-
151152
func FakeObject(obj interface{}, opts ...Option) error {
152153
reflectType := reflect.TypeOf(obj)
153154

@@ -160,6 +161,7 @@ func FakeObject(obj interface{}, opts ...Option) error {
160161
}
161162
f := &faker{
162163
maxDepth: 12,
164+
logger: zerolog.Nop(),
163165
}
164166
for _, o := range opts {
165167
o(f)

faker/faker_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
11
package faker
22

33
import (
4-
"fmt"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
type testFakerStruct struct {
910
A int
1011
B string
1112
C *string
13+
E interface{}
1214
}
1315

1416
func TestFaker(t *testing.T) {
1517
a := testFakerStruct{}
1618
if err := FakeObject(&a); err != nil {
1719
t.Fatal(err)
1820
}
19-
fmt.Println(a)
21+
assert.NotEmpty(t, a.A)
22+
assert.NotEmpty(t, a.B)
23+
assert.NotEmpty(t, a.C)
24+
assert.Empty(t, a.E) // empty interfaces are not faked
25+
}
26+
27+
type customType string
28+
29+
type fakerStructWithCustomType struct {
30+
A customType
31+
B map[string]customType
32+
C map[customType]string
33+
D []customType
34+
}
35+
36+
func TestFakerWithCustomType(t *testing.T) {
37+
a := fakerStructWithCustomType{}
38+
if err := FakeObject(&a); err != nil {
39+
t.Fatal(err)
40+
}
41+
assert.NotEmpty(t, a.A)
42+
assert.NotEmpty(t, a.B)
43+
assert.NotEmpty(t, a.C)
44+
assert.NotEmpty(t, a.D)
2045
}

faker/options.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package faker
2+
3+
import "github.com/rs/zerolog"
4+
5+
type Option func(*faker)
6+
7+
func WithMaxDepth(depth int) Option {
8+
return func(f *faker) {
9+
f.maxDepth = depth
10+
}
11+
}
12+
13+
func WithLogger(logger zerolog.Logger) Option {
14+
return func(f *faker) {
15+
f.logger = logger
16+
}
17+
}

0 commit comments

Comments
 (0)