-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfacto_test.go
252 lines (206 loc) · 4.94 KB
/
facto_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
package facto_test
import (
"fmt"
"sync"
"testing"
"github.com/gofrs/uuid"
"github.com/wawandco/facto"
)
func TestBuild(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
userProduct := facto.Build(UserFactory).(user)
if userProduct.Name != "Wawandco" {
t.Errorf("expected '%s' but got '%s'", "Wawandco", userProduct.Name)
}
})
t.Run("Dependent", func(t *testing.T) {
event := facto.Build(EventFactory).(event)
if event.Name != "CLICK" {
t.Errorf("expected '%s' but got '%s'", "CLICK", event.Name)
}
if event.User.Name != "Wawandco" {
t.Errorf("expected '%s' but got '%s'", "Wawandco", event.User.Name)
}
})
t.Run("FakeAndBuildN", func(t *testing.T) {
c := facto.Build(CompanyFactory).(company)
if c.Name == "" {
t.Errorf("should have set the Name")
}
if c.Address == "" {
t.Errorf("should have set the Address")
}
if c.ContactEmail == "" {
t.Errorf("should have set the ContactEmail")
}
if len(c.Users) != 5 {
t.Errorf("should have set 5 users, set %v", len(c.Users))
}
for _, u := range c.Users {
if u.Name != "" {
continue
}
t.Errorf("should have set the Name for User %v", u)
}
})
t.Run("NamedUUID", func(t *testing.T) {
c := facto.Build(DepartmentFactory).(department)
if c.CompanyID.String() != c.Company.ID.String() {
t.Errorf("companyID should match")
}
})
}
func TestBuildFakeData(t *testing.T) {
user := facto.Build(OtherUserFactory).(OtherUser)
if user.FirstName == "" {
t.Errorf("should have set the FirstName")
}
if user.LastName == "" {
t.Errorf("should have set the LastName")
}
if user.Email == "" {
t.Errorf("should have set the Email")
}
if user.Company == "" {
t.Errorf("should have set the Company")
}
if user.Address == "" {
t.Errorf("should have set the Address")
}
}
func Test_BuildN(t *testing.T) {
usersProduct := facto.BuildN(UserNFactory, 5).([]user)
for i := 0; i < 5; i++ {
if fmt.Sprintf("Wawandco %d", i) != usersProduct[i].Name {
t.Errorf("expected '%s' but got '%s'", fmt.Sprintf("Wawandco %d", i), usersProduct[i].Name)
}
}
}
func Test_Build_Concurrently(t *testing.T) {
tcases := []struct {
factoryName string
factory facto.Factory
expected string
}{
{
factoryName: "UserNumberOne",
factory: func(f facto.Helper) facto.Product {
u := user{
Name: "Wawandco",
}
return facto.Product(u)
},
expected: "Wawandco",
},
{
factoryName: "UserNumberTwo",
factory: func(f facto.Helper) facto.Product {
u := user{
Name: "Wawandco 2",
}
return facto.Product(u)
},
expected: "Wawandco 2",
},
{
factoryName: "UserNumberThree",
factory: func(f facto.Helper) facto.Product {
u := user{
Name: fmt.Sprintf("Wawandco %d", f.Index),
}
return facto.Product(u)
},
expected: "Wawandco 0",
},
}
var wgbuild sync.WaitGroup
for i := range tcases {
wgbuild.Add(1)
gr := func(name string, factory facto.Factory, expected string, index int) {
defer wgbuild.Done()
userProduct, ok := facto.Build(factory).(user)
if !ok {
t.Fatalf("Should have got user but got %v", userProduct)
}
if expected != userProduct.Name {
t.Errorf("expected '%s' but got '%s' in '%s'", expected, userProduct.Name, fmt.Sprintf("case %d", i+1))
}
}
go gr(tcases[i].factoryName, tcases[i].factory, tcases[i].expected, i)
}
wgbuild.Wait()
}
type user struct {
Name string
}
type event struct {
Name string
User user
}
type company struct {
ID uuid.UUID
Name string
Address string
ContactEmail string
Users []user
}
type department struct {
Name string
CompanyID uuid.UUID
Company company
}
type OtherUser struct {
FirstName string
LastName string
Email string
Company string
Address string
}
func UserNFactory(f facto.Helper) facto.Product {
u := user{
Name: fmt.Sprintf("Wawandco %d", f.Index),
}
return facto.Product(u)
}
func UserFactory(f facto.Helper) facto.Product {
u := user{
Name: "Wawandco",
}
return facto.Product(u)
}
func OtherUserFactory(f facto.Helper) facto.Product {
u := OtherUser{
FirstName: f.Faker.FirstName(),
LastName: f.Faker.LastName(),
Email: f.Faker.Email(),
Company: f.Faker.Company(),
Address: f.Faker.Address().Address,
}
return facto.Product(u)
}
func EventFactory(f facto.Helper) facto.Product {
u := event{
Name: "CLICK",
User: facto.Build(UserFactory).(user),
}
return facto.Product(u)
}
func CompanyFactory(h facto.Helper) facto.Product {
u := company{
ID: h.NamedUUID("company_id"),
Name: h.Faker.Name(),
Address: h.Faker.Address().Address,
ContactEmail: h.Faker.Email(),
// Building N Users
Users: facto.BuildN(UserFactory, 5).([]user),
}
return facto.Product(u)
}
func DepartmentFactory(f facto.Helper) facto.Product {
u := department{
Name: "Technology",
CompanyID: f.NamedUUID("company_id"),
Company: facto.Build(CompanyFactory).(company),
}
return facto.Product(u)
}