forked from nauyey/factory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategies_test.go
435 lines (408 loc) · 11.1 KB
/
strategies_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package factory_test
import (
"errors"
"fmt"
"testing"
"time"
. "github.com/nauyey/factory"
"github.com/nauyey/factory/def"
)
type testUser struct {
ID int64
Name string
NickName string
Age int32
Country string
BirthTime time.Time
Now time.Time
Blogs []*testBlog
}
type testBlog struct {
ID int64
Title string
Content string
AuthorID int64
Author *testUser
}
type testComment struct {
ID int64
Text string
BlogID int64
UserID int64
Blog *testBlog
User *testUser
}
type relation struct {
Author *testUser
}
type testCommentary struct {
ID int64
Title string
Content string
AuthorID int64
R *relation
Comment *testComment
}
func TestBuild(t *testing.T) {
// define factory
var birthTime, _ = time.Parse("2006-01-02T15:04:05.000Z", "2000-11-19T00:00:00.000Z")
var now, _ = time.Parse("2006-01-02T15:04:05.000Z", "2017-11-19T00:00:00.000Z")
userFactory := def.NewFactory(testUser{}, "",
def.Field("Name", "test name"),
def.SequenceField("ID", 1, func(n int64) (interface{}, error) {
return n, nil
}),
def.Field("Now", now),
def.Field("BirthTime", birthTime),
def.DynamicField("Age", func(model interface{}) (interface{}, error) {
user, ok := model.(*testUser)
if !ok {
return nil, errors.New("invalid type of model in DynamicFieldValue function")
}
return int32(user.Now.Sub(user.BirthTime).Hours() / (24 * 365)), nil
}),
def.Trait("Chinese",
def.Field("Name", "小明"),
def.Field("Country", "China"),
),
def.Trait("teenager",
def.Field("Name", "少年小明"),
def.Field("NickName", "Young Ming"),
def.Field("Age", int32(16)),
),
def.Trait("a year latter",
def.SequenceField("Age", 1, func(n int64) (interface{}, error) {
return n, nil
}),
),
def.AfterBuild(func(model interface{}) error {
fmt.Println("AfterBuild...")
fmt.Println(model)
return nil
}),
)
// Test default factory
user := &testUser{}
err := Build(userFactory).To(user)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
checkUser(t, "Test default factory",
&testUser{
ID: 1,
Name: "test name",
NickName: "",
Age: 17,
Country: "",
},
user,
)
// Test Build with Field
user = &testUser{}
err = Build(userFactory,
WithField("Name", "Little Baby"),
WithField("Age", int32(3)),
).To(user)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
checkUser(t, "Test Build with Field",
&testUser{
ID: 2,
Name: "Little Baby",
NickName: "",
Age: 3,
Country: "",
},
user,
)
// Test Build with Trait
user = &testUser{}
err = Build(userFactory, WithTraits("Chinese")).To(user)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
checkUser(t, "Test Build with Trait",
&testUser{
ID: 3,
Name: "小明",
NickName: "",
Age: 17,
Country: "China",
},
user,
)
// Test Build with multi Traits
user = &testUser{}
err = Build(userFactory, WithTraits("Chinese", "teenager")).To(user)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
checkUser(t, "Test Build with multi Traits",
&testUser{
ID: 4,
Name: "少年小明",
NickName: "Young Ming",
Age: 16,
Country: "China",
},
user,
)
// Test Build with multi Traits and Field
user = &testUser{}
err = Build(userFactory,
WithTraits("Chinese", "teenager"),
WithField("Name", "中本聪明"),
).To(user)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
checkUser(t, "Test Build with multi Traits",
&testUser{
ID: 5,
Name: "中本聪明",
NickName: "Young Ming",
Age: 16,
Country: "China",
},
user,
)
}
func TestBuildWithAssociation(t *testing.T) {
// define user factory
userFactory := def.NewFactory(testUser{}, "",
def.Field("Name", "test name"),
def.SequenceField("ID", 1, func(n int64) (interface{}, error) {
return n, nil
}),
)
// define blog factory
blogFactory := def.NewFactory(testBlog{}, "",
def.SequenceField("ID", 1, func(n int64) (interface{}, error) {
return n, nil
}),
def.DynamicField("Title", func(blog interface{}) (interface{}, error) {
blogInstance, ok := blog.(*testBlog)
if !ok {
return nil, fmt.Errorf("set field Title failed")
}
return fmt.Sprintf("Blog Title %d", blogInstance.ID), nil
}),
def.Association("Author", "AuthorID", "ID", userFactory,
def.Field("Name", "blog author name"),
),
)
// Test Build with association
blog := &testBlog{}
err := Build(blogFactory, WithField("Content", "Blog content")).To(blog)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
checkBlog(t, "Test Build with association",
&testBlog{
ID: 1,
Title: "Blog Title 1",
Content: "Blog content",
AuthorID: 1,
Author: &testUser{
ID: 1,
Name: "blog author name",
NickName: "",
Age: 0,
Country: "",
},
},
blog,
)
}
func TestBuildOneToManyAssociation(t *testing.T) {
// define blog factory
blogFactory := def.NewFactory(testBlog{}, "",
def.SequenceField("ID", 1, func(n int64) (interface{}, error) {
return n, nil
}),
def.DynamicField("Title", func(blog interface{}) (interface{}, error) {
blogInstance, ok := blog.(*testBlog)
if !ok {
return nil, fmt.Errorf("set field Title failed")
}
return fmt.Sprintf("Blog Title %d", blogInstance.ID), nil
}),
)
// define user factory
userFactory := def.NewFactory(testUser{}, "",
def.Field("Name", "test one-to-many name"),
def.SequenceField("ID", 1, func(n int64) (interface{}, error) {
return n, nil
}),
def.AfterBuild(func(user interface{}) error {
author, _ := user.(*testUser)
author.Blogs = []*testBlog{}
return BuildSlice(blogFactory, 10,
WithField("AuthorID", author.ID),
WithField("Author", author),
).To(&author.Blogs)
}),
)
// Test Build one-to-many association
user := &testUser{}
err := Build(userFactory).To(user)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
if len(user.Blogs) != 10 {
t.Fatalf("Build one-to-many association failed with len(Blogs)=%d, want len(Blogs)=10", len(user.Blogs))
}
for i, blog := range user.Blogs {
checkBlog(t, "Test Build one-to-many association",
&testBlog{
ID: int64(i) + 1,
Title: fmt.Sprintf("Blog Title %d", i+1),
AuthorID: user.ID,
Author: &testUser{
ID: user.ID,
Name: "test one-to-many name",
},
},
blog,
)
}
}
func TestBuildWithChainedField(t *testing.T) {
// define user factory
userFactory := def.NewFactory(testUser{}, "",
def.Field("Name", "test name"),
def.SequenceField("ID", 1, func(n int64) (interface{}, error) {
return n, nil
}),
)
// define commentary factory
commentaryFactory := def.NewFactory(testCommentary{}, "",
def.SequenceField("ID", 1, func(n int64) (interface{}, error) {
return n, nil
}),
def.DynamicField("Title", func(commentaryIfac interface{}) (interface{}, error) {
commentary, ok := commentaryIfac.(*testCommentary)
if !ok {
return nil, fmt.Errorf("set field Title failed")
}
return fmt.Sprintf("Blog Title %d", commentary.ID), nil
}),
def.Association("R.Author", "AuthorID", "ID", userFactory,
def.Field("Name", "commentary author name"),
),
def.Trait("with comment text",
def.Field("Comment.Text", "chain comment text"),
),
)
// Test Build with chained field
commentary := &testCommentary{}
err := Build(commentaryFactory, WithTraits("with comment text")).To(commentary)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
if commentary.Comment.Text != "chain comment text" {
t.Fatalf("Build failed with chained field with Comment.Text=%s, want Comment.Text=\"chain comment text\"", commentary.Comment.Text)
}
// Test Build Associations with chained field
commentary = &testCommentary{}
err = Build(commentaryFactory).To(commentary)
if err != nil {
t.Fatalf("Build failed with error: %v", err)
}
if commentary.R == nil {
t.Fatalf("Build failed with chained field with R=nil")
}
if commentary.R.Author == nil {
t.Fatalf("Build failed with chained field with R.Author=nil")
}
if commentary.R.Author.Name != "commentary author name" {
t.Errorf("Build failed with chained field with R.Author.Name=%s, want R.Author.Name = \"commentary author name\"", commentary.R.Author.Name)
}
}
func TestBuildSlice(t *testing.T) {
// define user factory
userFactory := def.NewFactory(testUser{}, "",
def.Field("Name", "test name"),
def.SequenceField("ID", 1, func(n int64) (interface{}, error) {
return n, nil
}),
)
// test build []*Type slice
users := []*testUser{}
err := BuildSlice(userFactory, 3, WithField("Name", "test build slice name")).To(&users)
if err != nil {
t.Fatalf("BuildSlice failed with err=%v", err)
}
if len(users) != 3 {
t.Fatalf("BuildSlice failed with len(users)=%d, want len(users)=3", len(users))
}
for i, user := range users {
checkUser(t, "Test BuildSlice",
&testUser{
ID: int64(i) + 1,
Name: "test build slice name",
},
user,
)
}
// test build []Type slice
users2 := []testUser{}
err = BuildSlice(userFactory, 3, WithField("Name", "test build slice name")).To(&users2)
if err != nil {
t.Fatalf("BuildSlice failed with err=%v", err)
}
if len(users2) != 3 {
t.Fatalf("BuildSlice failed with len(users2)=%d, want len(users2)=3", len(users2))
}
for i, user := range users2 {
checkUser(t, "Test BuildSlice",
&testUser{
ID: int64(i) + 4,
Name: "test build slice name",
},
&user,
)
}
// test build with count=0
users = []*testUser{}
err = BuildSlice(userFactory, 0, WithField("Name", "test build slice name")).To(&users)
if err != nil {
t.Fatalf("BuildSlice failed with err=%v", err)
}
if len(users) != 0 {
t.Fatalf("BuildSlice failed with len(users)=%d, want len(users)=0", len(users))
}
}
func checkUser(t *testing.T, name string, expect *testUser, got *testUser) {
if got.ID != expect.ID {
t.Errorf("Case %s: failed with ID=%d, want ID=%d", name, got.ID, expect.ID)
}
if got.Name != expect.Name {
t.Errorf("Case %s: failed with Name=%s, want Name=%s", name, got.Name, expect.Name)
}
if got.NickName != expect.NickName {
t.Errorf("Case %s: failed with NickName=%s, want NickName=%s", name, got.NickName, expect.NickName)
}
if got.Age != expect.Age {
t.Errorf("Case %s: failed with Age=%d, want Age=%d", name, got.Age, expect.Age)
}
if got.Country != expect.Country {
t.Errorf("Case %s: failed with Country=%s, want Country=%s", name, got.Country, expect.Country)
}
}
func checkBlog(t *testing.T, name string, expect *testBlog, got *testBlog) {
if got.ID != expect.ID {
t.Errorf("Case %s: failed with ID=%d, want ID=%d", name, got.ID, expect.ID)
}
if got.Title != expect.Title {
t.Errorf("Case %s: failed with Title=%s, want Title=%s", name, got.Title, expect.Title)
}
if got.Content != expect.Content {
t.Errorf("Case %s: failed with Content=%s, want Content=%s", name, got.Content, expect.Content)
}
if got.AuthorID != expect.AuthorID {
t.Errorf("Case %s: failed with AuthorID=%d, want AuthorID=%d", name, got.AuthorID, expect.AuthorID)
}
checkUser(t, name, expect.Author, got.Author)
}