-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfaker_test.go
161 lines (142 loc) · 3.37 KB
/
faker_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
package faker
import (
"encoding/json"
"net"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testFakerStruct struct {
A int
B string
C *string
D time.Time
E any
F json.Number
}
func TestFaker(t *testing.T) {
a := testFakerStruct{}
if err := FakeObject(&a); err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, a.A)
assert.NotEmpty(t, a.B)
assert.NotEmpty(t, a.C)
assert.NotEmpty(t, a.D)
assert.Empty(t, a.E) // empty interfaces are not faked
assert.Equal(t, json.Number("123456789"), a.F) // json numbers should be numbers
}
type customType string
type fakerStructWithCustomType struct {
A customType
B map[string]customType
C map[customType]string
D []customType
}
func TestFakerWithCustomType(t *testing.T) {
a := fakerStructWithCustomType{}
if err := FakeObject(&a); err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, a.A)
assert.NotEmpty(t, a.B)
assert.NotEmpty(t, a.C)
assert.NotEmpty(t, a.D)
}
func TestFakerWithCustomTypePreserve(t *testing.T) {
a := fakerStructWithCustomType{
A: "A",
B: map[string]customType{"b": "B"},
C: map[customType]string{"c": "C"},
D: []customType{"D", "D2"},
}
if err := FakeObject(&a); err != nil {
t.Fatal(err)
}
assert.EqualValues(t, "A", a.A)
assert.EqualValues(t, map[string]customType{"b": "B"}, a.B)
assert.EqualValues(t, map[customType]string{"c": "C"}, a.C)
assert.EqualValues(t, []customType{"D", "D2"}, a.D)
}
type complexType struct {
IPAddress net.IP
IPAddresses []net.IP
PtrIPAddress *net.IP
PtrIPAddresses []*net.IP
NestedComplex struct {
IPAddress net.IP
}
}
func TestFakerCanFakeNetIP(t *testing.T) {
a := complexType{}
if err := FakeObject(&a); err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, a.IPAddress)
assert.Equal(t, "1.1.1.1", a.IPAddress.String())
assert.Equal(t, 1, len(a.IPAddresses))
assert.Equal(t, "1.1.1.1", a.IPAddresses[0].String())
assert.NotEmpty(t, a.PtrIPAddress)
assert.Equal(t, "1.1.1.1", a.PtrIPAddress.String())
assert.Equal(t, 1, len(a.PtrIPAddresses))
assert.Equal(t, "1.1.1.1", a.PtrIPAddresses[0].String())
assert.NotEmpty(t, a.NestedComplex.IPAddress)
assert.Equal(t, "1.1.1.1", a.NestedComplex.IPAddress.String())
}
func TestMaxDepth(t *testing.T) {
// Struct with nested structs
a := struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A struct {
A string
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}{}
sink := &testLogSink{}
testLogger := zerolog.New(sink)
require.NoError(t, FakeObject(&a, WithLogger(testLogger)), "max depth reached")
require.Equal(t, 1, len(sink.getLogs()))
require.Contains(t, sink.getLogs()[0], "max_depth reached")
}
type testLogSink struct {
logs []string
}
func (sink *testLogSink) Write(p []byte) (n int, err error) {
sink.logs = append(sink.logs, string(p))
return len(p), nil
}
func (sink *testLogSink) getLogs() []string {
return sink.logs
}