forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup_test.go
453 lines (395 loc) · 12 KB
/
lookup_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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package gofakeit
import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"testing"
)
func Example_custom() {
Seed(11)
AddFuncLookup("friendname", Info{
Category: "custom",
Description: "Random friend name",
Example: "bill",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return RandomString([]string{"bill", "bob", "sally"}), nil
},
})
type Foo struct {
FriendName string `fake:"{friendname}"`
}
var f Foo
Struct(&f)
fmt.Printf("%s", f.FriendName)
// Output: bill
}
func Example_custom_with_params() {
Seed(11)
AddFuncLookup("jumbleword", Info{
Category: "jumbleword",
Description: "Take a word and jumple it up",
Example: "loredlowlh",
Output: "string",
Params: []Param{
{Field: "word", Type: "int", Description: "Word you want to jumble"},
},
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
word, err := info.GetString(m, "word")
if err != nil {
return nil, err
}
split := strings.Split(word, "")
ShuffleStrings(split)
return strings.Join(split, ""), nil
},
})
type Foo struct {
JumbleWord string `fake:"{jumbleword:helloworld}"`
}
var f Foo
Struct(&f)
fmt.Printf("%s", f.JumbleWord)
// Output: loredlowlh
}
func TestMapParamsGet(t *testing.T) {
mapParams := MapParams{
"name": []string{"bill"},
}
// Test get string
val := mapParams.Get("name")
if val[0] != "bill" {
t.Fatalf("Expected bill but got %s", val)
}
// Test get from field that doesnt exist
val = mapParams.Get("age")
if len(val) != 0 {
t.Fatalf("Expected empty slice but got %s", val)
}
}
func TestMapParamsMarshalJSON(t *testing.T) {
mp := NewMapParams()
mp.Add("name", "billy")
mp.Add("field", `{name: "billy"}`)
// Test marshal into JSON
jsonData, err := json.Marshal(mp)
if err != nil {
t.Fatal(err)
}
// t.Fatal(string(jsonData))
// Unmarshal into MapParams
var mapData MapParams
err = json.Unmarshal(jsonData, &mapData)
if err != nil {
t.Fatal(err)
}
// t.Fatalf("%+v", mapData)
}
func TestMapParamsValueUnmarshalJSON(t *testing.T) {
mapParamStr := `{"name":["billy","mister"],"nickname":"big boy","age":[10],"weight":200}`
// Test unmarshal into MapParams
var mapData MapParams
err := json.Unmarshal([]byte(mapParamStr), &mapData)
if err != nil {
t.Fatal(err)
}
// Check to make sure we have the right data
if len(mapData) != 4 {
t.Fatalf("Expected 4 params, got %d", len(mapData))
}
if len(mapData["name"]) != 2 {
t.Fatalf("Expected 2 names, got %d", len(mapData["name"]))
} else {
if mapData["name"][0] != "billy" {
t.Fatalf("Expected billy, got %s", mapData["name"][0])
}
if mapData["name"][1] != "mister" {
t.Fatalf("Expected mister, got %s", mapData["name"][1])
}
}
if len(mapData["nickname"]) != 1 {
t.Fatalf("Expected 1 nickname, got %d", len(mapData["nickname"]))
} else {
if mapData["nickname"][0] != "big boy" {
t.Fatalf("Expected first value of nickname to be big boy but got %s", mapData["nickname"][0])
}
}
if len(mapData["age"]) != 1 {
t.Fatalf("Expected 1 age, got %d", len(mapData["age"]))
} else {
if mapData["age"][0] != "10" {
t.Fatalf("Expected first value of age to be 10 but got %s", mapData["age"][0])
}
}
if len(mapData["weight"]) != 1 {
t.Fatalf("Expected 1 weight, got %d", len(mapData["weight"]))
} else {
if mapData["weight"][0] != "200" {
t.Fatalf("Expected first value of weight to be 200 but got %s", mapData["weight"][0])
}
}
}
func TestLookupChecking(t *testing.T) {
faker := New(0)
for field, info := range FuncLookups {
var mapData MapParams
if info.Params != nil && len(info.Params) != 0 {
// Make sure mapdata is set
if mapData == nil {
mapData = make(MapParams)
}
// Loop through params and add fields to mapdata
for _, p := range info.Params {
if p.Default != "" {
// If p.Type is []uint, then we need to convert it to []string
if strings.HasPrefix(p.Type, "[") {
// Remove [] from type
defaultClean := p.Default[1 : len(p.Default)-1]
mapData[p.Field] = strings.Split(defaultClean, ",")
} else {
mapData[p.Field] = []string{p.Default}
}
continue
}
switch p.Type {
case "bool":
mapData[p.Field] = []string{fmt.Sprintf("%v", Bool())}
case "string":
mapData[p.Field] = []string{Letter()}
case "uint":
mapData[p.Field] = []string{fmt.Sprintf("%v", Uint16())}
case "int":
mapData[p.Field] = []string{fmt.Sprintf("%v", Int16())}
case "float":
mapData[p.Field] = []string{fmt.Sprintf("%v", Float32())}
case "[]string":
mapData[p.Field] = []string{Letter(), Letter(), Letter(), Letter()}
case "[]int":
mapData[p.Field] = []string{fmt.Sprintf("%d", Int8()), fmt.Sprintf("%d", Int8()), fmt.Sprintf("%d", Int8()), fmt.Sprintf("%d", Int8())}
case "[]uint":
mapData[p.Field] = []string{fmt.Sprintf("%d", Uint8()), fmt.Sprintf("%d", Uint8()), fmt.Sprintf("%d", Uint8()), fmt.Sprintf("%d", Uint8())}
case "[]float":
mapData[p.Field] = []string{fmt.Sprintf("%v", Float32()), fmt.Sprintf("%v", Float32()), fmt.Sprintf("%v", Float32()), fmt.Sprintf("%v", Float32())}
case "[]Field":
mapData[p.Field] = []string{`{"name":"first_name","function":"firstname"}`}
default:
t.Fatalf("Looking for %s but switch case doesnt have it", p.Type)
}
}
}
_, err := info.Generate(faker.Rand, &mapData, &info)
if err != nil {
t.Fatalf("%s failed - Err: %s - Data: %v", field, err, mapData)
}
}
}
// Make sure all lookups have specific fields
func TestLookupCheckFields(t *testing.T) {
for field, info := range FuncLookups {
if info.Display == "" {
t.Fatalf("%s is missing a display", field)
}
if info.Category == "" {
t.Fatalf("%s is missing a category", field)
}
if info.Output == "" {
t.Fatalf("%s is misssing output", field)
}
// Check params
if info.Params != nil {
for _, p := range info.Params {
if p.Field == "" {
t.Fatalf("Field %s param %s is missing a field", field, p.Field)
}
if p.Display == "" {
t.Fatalf("Field %s param %s is missing a display", field, p.Field)
}
if p.Type == "" {
t.Fatalf("Field %s param %s is missing a type", field, p.Field)
}
if p.Description == "" {
t.Fatalf("Field %s param %s is missing a description", field, p.Field)
}
}
}
}
}
func TestLookupRemove(t *testing.T) {
funcName := "friendname"
AddFuncLookup(funcName, Info{
Category: "custom",
Description: "Random friend name",
Example: "bill",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return RandomString([]string{"bill", "bob", "sally"}), nil
},
})
info := GetFuncLookup(funcName)
if info == nil {
t.Fatal("Could not find lookup")
}
RemoveFuncLookup(funcName)
info = GetFuncLookup(funcName)
if info != nil {
t.Fatal("Got info when I shouldn't have")
}
}
func TestLookupCalls(t *testing.T) {
faker := New(0)
for _, info := range FuncLookups {
mapData := make(MapParams)
// If parameters are required build it
if info.Params != nil && len(info.Params) != 0 {
// Loop through params and add fields to mapdata
for _, p := range info.Params {
if p.Default != "" {
// If p.Type is []uint, then we need to convert it to []string
if strings.HasPrefix(p.Type, "[") {
// Remove [] from type
defaultClean := p.Default[1 : len(p.Default)-1]
// Split on comma
defaultSplit := strings.Split(defaultClean, ",")
// Loop through defaultSplit and add to mapData
for _, s := range defaultSplit {
mapData.Add(p.Field, s)
}
} else {
mapData.Add(p.Field, p.Default)
}
continue
}
switch p.Type {
case "bool":
mapData.Add(p.Field, fmt.Sprintf("%v", Bool()))
case "string":
mapData.Add(p.Field, Letter())
case "uint":
mapData.Add(p.Field, fmt.Sprintf("%v", Uint16()))
case "int":
mapData.Add(p.Field, fmt.Sprintf("%v", Int16()))
case "float":
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
case "[]string":
mapData.Add(p.Field, Letter())
mapData.Add(p.Field, Letter())
mapData.Add(p.Field, Letter())
mapData.Add(p.Field, Letter())
case "[]int":
mapData.Add(p.Field, fmt.Sprintf("%d", Int8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Int8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Int8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Int8()))
case "[]uint":
mapData.Add(p.Field, fmt.Sprintf("%d", Uint8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Uint8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Uint8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Uint8()))
case "[]float":
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
case "[]Field":
mapData.Add(p.Field, `{"name":"first_name","function":"firstname"}`)
default:
t.Fatalf("Looking for %s but switch case doesnt have it", p.Type)
}
}
}
_, err := info.Generate(faker.Rand, &mapData, &info)
if err != nil {
t.Fatal(err)
}
}
}
func TestLookupCallsErrorParams(t *testing.T) {
// Look through lookups and empty defaults values to help with tests
for funcName, info := range FuncLookups {
if info.Params != nil || len(info.Params) != 0 {
params := []Param{}
for _, p := range info.Params {
p.Default = ""
params = append(params, p)
}
info.Params = params
AddFuncLookup(funcName, info)
}
}
// Initiate new faker
faker := New(0)
for funcName, info := range FuncLookups {
// If parameters are not required skip it. We are only testing params
if info.Params == nil || len(info.Params) == 0 {
continue
}
// Loop through each param and mark each one fail
for i := 0; i < len(info.Params); i++ {
mapData := NewMapParams()
skip := false
currentEmptyParam := ""
// Loop through params and add fields to mapdata
for ii, p := range info.Params {
// If the length is equal to the param loop
// purposly not add that field to trigger an error
if i == ii {
currentEmptyParam = p.Field
// If param is optional skip it
if p.Optional {
skip = true
}
continue
}
if p.Default != "" {
mapData.Add(p.Field, p.Default)
continue
}
switch p.Type {
case "bool":
mapData.Add(p.Field, fmt.Sprintf("%v", Bool()))
case "string":
mapData.Add(p.Field, Letter())
case "uint":
mapData.Add(p.Field, fmt.Sprintf("%v", Uint16()))
case "int":
mapData.Add(p.Field, fmt.Sprintf("%v", Int16()))
case "float":
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
case "[]string":
mapData.Add(p.Field, Letter())
mapData.Add(p.Field, Letter())
mapData.Add(p.Field, Letter())
mapData.Add(p.Field, Letter())
case "[]int":
mapData.Add(p.Field, fmt.Sprintf("%d", Int8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Int8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Int8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Int8()))
case "[]uint":
mapData.Add(p.Field, fmt.Sprintf("%d", Uint8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Uint8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Uint8()))
mapData.Add(p.Field, fmt.Sprintf("%d", Uint8()))
case "[]float":
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
case "[]Field":
mapData.Add(p.Field, `{"name":"first_name","function":"firstname"}`)
default:
t.Fatalf("Looking for %s but switch case doesnt have it", p.Type)
}
}
if !skip {
_, err := info.Generate(faker.Rand, mapData, &info)
if err == nil {
t.Error(funcName+" should have failed on param", currentEmptyParam)
}
}
}
}
// Reset lookup functions back
initLookup()
}