This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
recipe_funcs.go
503 lines (472 loc) · 17.6 KB
/
recipe_funcs.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package testfixture
import (
errs "github.com/pkg/errors"
)
// A RecipeFunction tells the test fixture to create n objects of a given kind.
// You can pass in customize-entity-functions in order to manipulate the objects
// before they get created.
type RecipeFunction func(fxt *TestFixture) error
const checkStr = "expected at least %d \"%s\" objects but found only %d"
func (fxt *TestFixture) deps(fns ...RecipeFunction) error {
if !fxt.isolatedCreation {
for _, fn := range fns {
if err := fn(fxt); err != nil {
return errs.Wrap(err, "failed to setup dependency")
}
}
}
return nil
}
// CustomizeIdentityFunc is directly compatible with CustomizeEntityFunc
// but it can only be used for the Identites() recipe-function.
type CustomizeIdentityFunc CustomizeEntityFunc
// Identities tells the test fixture to create at least n identity objects.
//
// If called multiple times with differently n's, the biggest n wins. All
// customize-entitiy-functions fns from all calls will be respected when
// creating the test fixture.
//
// Here's an example how you can create 42 identites and give them a numbered
// user name like "John Doe 0", "John Doe 1", and so forth:
// Identities(42, func(fxt *TestFixture, idx int) error{
// fxt.Identities[idx].Username = "Jane Doe " + strconv.FormatInt(idx, 10)
// return nil
// })
// Notice that the index idx goes from 0 to n-1 and that you have to manually
// lookup the object from the test fixture. The identity object referenced by
// fxt.Identities[idx]
// is guaranteed to be ready to be used for creation. That means, you don't
// necessarily have to touch it to avoid unique key violation for example. This
// is totally optional.
func Identities(n int, fns ...CustomizeIdentityFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.Identities)
if l < n {
return errs.Errorf(checkStr, n, kindIdentities, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
return fxt.setupInfo(n, kindIdentities, customFuncs...)
}
}
// CustomizeSpaceFunc is directly compatible with CustomizeEntityFunc
// but it can only be used for the Spaces() recipe-function.
type CustomizeSpaceFunc CustomizeEntityFunc
// Spaces tells the test fixture to create at least n space objects. See also
// the Identities() function for more general information on n and fns.
//
// When called in NewFixture() this function will call also call
// Identities(1)
// but with NewFixtureIsolated(), no other objects will be created.
func Spaces(n int, fns ...CustomizeSpaceFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.Spaces)
if l < n {
return errs.Errorf(checkStr, n, kindSpaces, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindSpaces, customFuncs...); err != nil {
return err
}
return fxt.deps(Identities(1))
}
}
// CustomizeIterationFunc is directly compatible with
// CustomizeEntityFunc but it can only be used for the Iterations()
// recipe-function.
type CustomizeIterationFunc CustomizeEntityFunc
// Iterations tells the test fixture to create at least n iteration objects. See
// also the Identities() function for more general information on n and fns.
//
// When called in NewFixture() this function will call also call
// Spaces(1)
// but with NewFixtureIsolated(), no other objects will be created.
func Iterations(n int, fns ...CustomizeIterationFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.Iterations)
if l < n {
return errs.Errorf(checkStr, n, kindIterations, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindIterations, customFuncs...); err != nil {
return err
}
return fxt.deps(Spaces(1))
}
}
// CustomizeAreaFunc is directly compatible with CustomizeEntityFunc but
// it can only be used for the Areas() recipe-function.
type CustomizeAreaFunc CustomizeEntityFunc
// Areas tells the test fixture to create at least n area objects. See
// also the Identities() function for more general information on n and fns.
//
// When called in NewFixture() this function will call also call
// Spaces(1)
// but with NewFixtureIsolated(), no other objects will be created.
func Areas(n int, fns ...CustomizeAreaFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.Areas)
if l < n {
return errs.Errorf(checkStr, n, kindAreas, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindAreas, customFuncs...); err != nil {
return err
}
return fxt.deps(Spaces(1))
}
}
// CustomizeCodebaseFunc is directly compatible with CustomizeEntityFunc
// but it can only be used for the Codebases() recipe-function.
type CustomizeCodebaseFunc CustomizeEntityFunc
// Codebases tells the test fixture to create at least n codebase objects. See
// also the Identities() function for more general information on n and fns.
//
// When called in NewFixture() this function will call also call
// Spaces(1)
// but with NewFixtureIsolated(), no other objects will be created.
func Codebases(n int, fns ...CustomizeCodebaseFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.Codebases)
if l < n {
return errs.Errorf(checkStr, n, kindCodebases, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindCodebases, customFuncs...); err != nil {
return err
}
return fxt.deps(Spaces(1))
}
}
// CustomizeWorkItemFunc is directly compatible with CustomizeEntityFunc
// but it can only be used for the WorkItems() recipe-function.
type CustomizeWorkItemFunc CustomizeEntityFunc
// WorkItems tells the test fixture to create at least n work item objects. See
// also the Identities() function for more general information on n and fns.
//
// When called in NewFixture() this function will call also call
// Spaces(1)
// WorkItemTypes(1)
// Identities(1)
// but with NewFixtureIsolated(), no other objects will be created.
//
// Notice that the Number field of a work item is only set after the work item
// has been created, so any changes you make to
// fxt.WorkItems[idx].Number
// will have no effect.
func WorkItems(n int, fns ...CustomizeWorkItemFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.WorkItems)
if l < n {
return errs.Errorf(checkStr, n, kindWorkItems, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindWorkItems, customFuncs...); err != nil {
return err
}
return fxt.deps(Spaces(1), WorkItemTypes(1), Identities(1))
}
}
// CustomizeCommentFunc is directly compatible with CustomizeEntityFunc
// but it can only be used for the Comments() recipe-function.
type CustomizeCommentFunc CustomizeEntityFunc
// Comments tells the test fixture to create at least n comment objects. See
// also the Identities() function for more general information on n and fns.
//
// When called in NewFixture() this function will call also call
// Identities(1)
// WorkItems(1)
// but with NewFixtureIsolated(), no other objects will be created.
func Comments(n int, fns ...CustomizeWorkItemFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.Comments)
if l < n {
return errs.Errorf(checkStr, n, kindComments, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindComments, customFuncs...); err != nil {
return err
}
return fxt.deps(WorkItems(1), Identities(1))
}
}
// CustomizeWorkItemTypeFunc is directly compatible with
// CustomizeEntityFunc but it can only be used for the WorkItemTypes()
// recipe-function.
type CustomizeWorkItemTypeFunc CustomizeEntityFunc
// WorkItemTypes tells the test fixture to create at least n work item type
// objects. See also the Identities() function for more general information on n
// and fns.
//
// When called in NewFixture() this function will call also call
// Spaces(1)
// but with NewFixtureIsolated(), no other objects will be created.
//
// The work item type that we create for each of the n instances is always the
// same and it tries to be compatible with the planner item work item type by
// specifying the same fields.
func WorkItemTypes(n int, fns ...CustomizeWorkItemTypeFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.WorkItemTypes)
if l < n {
return errs.Errorf(checkStr, n, kindWorkItemTypes, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindWorkItemTypes, customFuncs...); err != nil {
return err
}
return fxt.deps(Spaces(1))
}
}
// CustomizeWorkItemLinkTypeFunc is directly compatible with
// CustomizeEntityFunc but it can only be used for the WorkItemLinkTypes()
// recipe-function.
type CustomizeWorkItemLinkTypeFunc CustomizeEntityFunc
// WorkItemLinkTypes tells the test fixture to create at least n work item link
// type objects. See also the Identities() function for more general information
// on n and fns.
//
// When called in NewFixture() this function will call also call
// Spaces(1)
// WorkItemLinkCategories(1)
// but with NewFixtureIsolated(), no other objects will be created.
//
// We've created these helper functions that you should have a look at if you
// want to implement your own re-usable customize-entity-functions:
// SetTopologies(topology ...link.Topology)
// The topology functions above are neat because you don't have to write a full
// function function yourself.
//
// By default a call to
// WorkItemLinkTypes(1)
// equals
// WorkItemLinkTypes(1, SetTopologies(link.TopologyTree))
// because we automatically set the topology for each link type to be "tree".
func WorkItemLinkTypes(n int, fns ...CustomizeWorkItemLinkTypeFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.WorkItemLinkTypes)
if l < n {
return errs.Errorf(checkStr, n, kindWorkItemLinkTypes, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindWorkItemLinkTypes, customFuncs...); err != nil {
return err
}
return fxt.deps(Spaces(1), WorkItemLinkCategories(1))
}
}
// CustomizeWorkItemLinkCategoryFunc is directly compatible with
// CustomizeEntityFunc but it can only be used for the
// WorkItemLinkCategories() recipe-function.
type CustomizeWorkItemLinkCategoryFunc CustomizeEntityFunc
// WorkItemLinkCategories tells the test fixture to create at least n work item
// link category objects. See also the Identities() function for more general
// information on n and fns.
//
// No other objects will be created.
func WorkItemLinkCategories(n int, fns ...CustomizeWorkItemLinkCategoryFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.WorkItemLinkCategories)
if l < n {
return errs.Errorf(checkStr, n, kindWorkItemLinkCategories, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
return fxt.setupInfo(n, kindWorkItemLinkCategories, customFuncs...)
}
}
// CustomizeWorkItemLinkFunc is directly compatible with
// CustomizeEntityFunc but it can only be used for the WorkItemLinks()
// recipe-function.
type CustomizeWorkItemLinkFunc CustomizeEntityFunc
// WorkItemLinks tells the test fixture to create at least n work item link
// objects. See also the Identities() function for more general information
// on n and fns.
//
// When called in NewFixture() this function will call also call
// WorkItemLinkTypes(1)
// WorkItems(2*n)
// but with NewFixtureIsolated(), no other objects will be created.
//
// Notice, that we will create two times the number of work items of your
// requested links. The way those links will be created can for sure be
// influenced using a customize-entity-function; but by default we create each
// link between two distinct work items. That means, no link will include the
// same work item.
func WorkItemLinks(n int, fns ...CustomizeWorkItemLinkFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.normalLinkCreation = true
if fxt.customLinkCreation {
return errs.New("When you have WorkItemLinksCustom() in your recipe you may no longer use WorkItemLinks().")
}
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.WorkItemLinks)
if l < n {
return errs.Errorf(checkStr, n, kindWorkItemLinks, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindWorkItemLinks, customFuncs...); err != nil {
return err
}
return fxt.deps(WorkItemLinkTypes(1), WorkItems(2*n))
}
}
// WorkItemLinksCustom behaves the same ways as WorkItemLinks but it makes no
// assumption on the number of work items that are going to be created. That
// means you can request and arbitrary number of links but you are also
// responsible of creating the the work items that you want to use in your
// links.
//
// NOTE: Normal WorkItemLinks creates twice the amount of work items as links.
func WorkItemLinksCustom(n int, fns ...CustomizeWorkItemLinkFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.customLinkCreation = true
if fxt.normalLinkCreation {
return errs.New("When you have WorkItemLinks() in your recipe you may no longer use WorkItemLinksCustom().")
}
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.WorkItemLinks)
if l < n {
return errs.Errorf(checkStr, n, kindWorkItemLinks, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindWorkItemLinks, customFuncs...); err != nil {
return err
}
return fxt.deps(WorkItemLinkTypes(1) /*, WorkItems(2*n)*/)
}
}
// CustomizeLabelFunc is directly compatible with CustomizeEntityFunc
// but it can only be used for the Labels() recipe-function.
type CustomizeLabelFunc CustomizeEntityFunc
// Labels tells the test fixture to create at least n label objects. See
// also the Identities() function for more general information on n and fns.
//
// When called in NewFixture() this function will call also call
// Spaces(1)
// but with NewFixtureIsolated(), no other objects will be created.
func Labels(n int, fns ...CustomizeLabelFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.Labels)
if l < n {
return errs.Errorf(checkStr, n, kindLabels, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindLabels, customFuncs...); err != nil {
return err
}
return fxt.deps(Spaces(1))
}
}
// CustomizeTrackerFunc is directly compatible with CustomizeEntityFunc
// but it can only be used for the Trackers() recipe-function.
type CustomizeTrackerFunc CustomizeEntityFunc
// Trackers tells the test fixture to create at least n tracker objects. See
// also the Identities() function for more general information on n and fns.
func Trackers(n int, fns ...CustomizeTrackerFunc) RecipeFunction {
return func(fxt *TestFixture) error {
fxt.checkFuncs = append(fxt.checkFuncs, func() error {
l := len(fxt.Trackers)
if l < n {
return errs.Errorf(checkStr, n, kindTrackers, l)
}
return nil
})
// Convert fns to []CustomizeEntityFunc
customFuncs := make([]CustomizeEntityFunc, len(fns))
for idx := range fns {
customFuncs[idx] = CustomizeEntityFunc(fns[idx])
}
if err := fxt.setupInfo(n, kindTrackers, customFuncs...); err != nil {
return err
}
return fxt.deps()
}
}