-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
generate_test.go
440 lines (374 loc) · 12 KB
/
generate_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
package gofakeit
import (
"fmt"
"regexp"
"strings"
"testing"
)
func TestGenerate(t *testing.T) {
output := ""
var err error
numTests := 1000
for i := 0; i < numTests; i++ {
output, err = Generate("{firstname} {lastname} {email} #?#?#?")
if err != nil {
t.Error(err)
}
if strings.Contains(output, "{") || strings.Contains(output, "}") {
t.Error("Output should not contain either { or }. Output: ", output)
}
}
}
func TestGenerate_Sub(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
Seed(11)
output, err := Generate("{randomstring:[{firstname},{lastname}]}")
if err != nil {
t.Error(err)
}
if output == "" {
t.Error("Output was empty")
}
})
t.Run("Complex", func(t *testing.T) {
Seed(11)
output, err := Generate("{randomstring:[{randomstring:[{firstname},{lastname}]},{randomstring:[{firstname},{lastname}]}]}")
if err != nil {
t.Error(err)
}
if output == "" {
t.Error("Output was empty")
}
})
}
func ExampleGenerate() {
Seed(11)
genStr, _ := Generate("{firstname} {lastname} {email} #?#?#?")
fmt.Println(genStr)
genStr, _ = Generate("{sentence:3}")
fmt.Println(genStr)
genStr, _ = Generate("{shuffleints:[1,2,3]}")
fmt.Println(genStr)
genStr, _ = Generate("{randomint:[1,2,3,-4]}")
fmt.Println(genStr)
genStr, _ = Generate("{randomuint:[1,2,3,4]}")
fmt.Println(genStr)
genStr, _ = Generate("{number:1,50}")
fmt.Println(genStr)
genStr, _ = Generate("{shufflestrings:[key:value,int:string,1:2,a:b]}")
fmt.Println(genStr)
// Output: Mollie Legros vanceschroeder@turner.com 8K8b1M
// Something am elsewhere.
// [3 2 1]
// 3
// 3
// 18
// [key:value a:b int:string 1:2]
}
func ExampleFaker_Generate() {
f := New(11)
genStr, _ := f.Generate("{firstname} {lastname} {email} #?#?#?")
fmt.Println(genStr)
genStr, _ = f.Generate("{sentence:3}")
fmt.Println(genStr)
genStr, _ = f.Generate("{shuffleints:[1,2,3]}")
fmt.Println(genStr)
genStr, _ = f.Generate("{randomint:[1,2,3,-4]}")
fmt.Println(genStr)
genStr, _ = f.Generate("{randomuint:[1,2,3,4]}")
fmt.Println(genStr)
genStr, _ = f.Generate("{number:1,50}")
fmt.Println(genStr)
genStr, _ = f.Generate("{shufflestrings:[key:value,int:string,1:2,a:b]}")
fmt.Println(genStr)
// Output: Mollie Legros vanceschroeder@turner.com 8K8b1M
// Something am elsewhere.
// [3 2 1]
// 3
// 3
// 18
// [key:value a:b int:string 1:2]
}
func BenchmarkGenerate(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Generate("{firstname} {lastname} {email} #?#?#?")
}
})
b.Run("Complex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Generate("{randomstring:[{randomstring:[{firstname},{lastname}]},{randomstring:[{firstname},{lastname}]}]}")
}
})
}
func ExampleFixedWidth() {
Seed(11)
value, err := FixedWidth(&FixedWidthOptions{
RowCount: 3,
Fields: []Field{
{Name: "Name", Function: "{firstname} {lastname}"},
{Name: "Email", Function: "email"},
{Name: "Password", Function: "password", Params: MapParams{"special": {"false"}}},
{Name: "Age", Function: "{number:1,100}"},
},
})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output: Name Email Password Age
// Sonny Stiedemann mollielegros@wilderman.org 3T8l34B5F0eb 62
// Cody Donnelly stevebins@robel.io 3fnu2C12baqe 9
// Julius Farrell tomasaullrich@hane.name e8W8SJGZi1Y4 77
}
func ExampleFixedWidth_default() {
Seed(11)
value, err := FixedWidth(nil)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output: Name Email Password
// Russ Donnelly missourigrimes@rohan.io N3xh07881HKv
// Cole Leannon evanadams@marquardt.com n2fja08VHMgw
// Darien Rempel thorajerde@cormier.com ZhMPq5v8z6iP
// Kaitlyn Wilderman constancenienow@smitham.io MkoTT5v5Mv6Q
// Rahul Turner irvingbins@lemke.io 8Wy0UV016u88
// April Robel beaulahmayert@kautzer.net ykgR5bO83YSa
// Tomasa Waters lelandwaelchi@beahan.net k68Ew58F9a0V
// Sunny Hane dawnbaumbach@mcdermott.biz AbUjm6x5dwd8
// Hilma Jast lavernerobel@bailey.io MyY49BLUWKCh
}
func ExampleFixedWidth_noHeader() {
Seed(11)
value, err := FixedWidth(&FixedWidthOptions{
RowCount: 3,
Fields: []Field{
{Name: "", Function: "{firstname} {lastname}"},
{Name: "", Function: "email"},
{Name: "", Function: "password", Params: MapParams{"special": {"false"}}},
{Name: "", Function: "{number:1,100}"},
},
})
if err != nil {
fmt.Println(err)
}
fmt.Println(value)
// Output: Sonny Stiedemann mollielegros@wilderman.org 3T8l34B5F0eb 62
// Cody Donnelly stevebins@robel.io 3fnu2C12baqe 9
// Julius Farrell tomasaullrich@hane.name e8W8SJGZi1Y4 77
}
func ExampleFaker_FixedWidth() {
f := New(11)
value, err := f.FixedWidth(&FixedWidthOptions{
RowCount: 3,
Fields: []Field{
{Name: "Name", Function: "{firstname} {lastname}"},
{Name: "Email", Function: "email"},
{Name: "Password", Function: "password", Params: MapParams{"special": {"false"}}},
{Name: "Age", Function: "{number:1,100}"},
},
})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output: Name Email Password Age
// Sonny Stiedemann mollielegros@wilderman.org 3T8l34B5F0eb 62
// Cody Donnelly stevebins@robel.io 3fnu2C12baqe 9
// Julius Farrell tomasaullrich@hane.name e8W8SJGZi1Y4 77
}
func TestFixedWidthLookup(t *testing.T) {
faker := New(0)
info := GetFuncLookup("fixed_width")
m := MapParams{
"rowcount": {"10"},
"fields": {
`{"name":"Name","function":"{firstname} {lastname}"}`,
`{"name":"Email","function":"email"}`,
`{"name":"Password","function":"password","params":{"special":["false"],"length":["20"]}}`,
`{"name":"Age","function":"{number:1,100}"}`,
},
}
output, err := info.Generate(faker, &m, info)
if err != nil {
t.Fatal(err.Error())
}
value := string(output.(string))
// Check the rumber of rows by counting the newlines, end of file has no newline
if strings.Count(value, "\n") != 10 {
t.Error("Expected 10+1(header row footer) rows, got", strings.Count(value, "\n"))
}
}
func TestFixedWidthNoOptions(t *testing.T) {
Seed(11)
_, err := FixedWidth(nil)
if err != nil {
t.Fatal(err.Error())
}
}
func BenchmarkFixedWidthLookup100(b *testing.B) {
faker := New(0)
for i := 0; i < b.N; i++ {
info := GetFuncLookup("fixed_width")
m := MapParams{
"rowcount": {"100"},
"fields": {
// `{"name":"Name","function":"{firstname} {lastname}"}`,
`{"name":"Email","function":"email"}`,
`{"name":"Password","function":"password","params":{"special":["false"],"length":["20"]}}`,
`{"name":"Age","function":"{number:1,100}"}`,
},
}
_, err := info.Generate(faker, &m, info)
if err != nil {
// Stack trace
b.Fatal(err.Error())
}
}
}
func ExampleRegex() {
Seed(11)
fmt.Println(Regex("[abcdef]{5}"))
fmt.Println(Regex("[[:upper:]]{5}"))
fmt.Println(Regex("(hello|world|whats|up)"))
fmt.Println(Regex(`^[a-z]{5,10}@[a-z]{5,10}\.(com|net|org)$`))
// Output: ffbbd
// GSNYV
// up
// tyyxi@kshtyia.net
}
func ExampleFaker_Regex() {
f := New(11)
fmt.Println(f.Regex("[abcdef]{5}"))
fmt.Println(f.Regex("[[:upper:]]{5}"))
fmt.Println(f.Regex("(hello|world|whats|up)"))
fmt.Println(f.Regex(`^[a-z]{5,10}@[a-z]{5,10}\.(com|net|org)$`))
// Output: ffbbd
// GSNYV
// up
// tyyxi@kshtyia.net
}
var regexes = []struct{ test string }{
{`^\d+$`},
{`\D{3}`},
{`Z{2,5}`},
{`[^1]{3,5}`},
{`(ab|bc)def`},
{`((123)?){3}`},
{`[^abcdef]{5}`},
{`[a-zA-Z]{100}`},
{`[[:upper:]]{5}`},
{`[^0-5a-z\s]{5}`},
{`123[0-2]+.*\w{3}`},
{`(hello|world|whats|up)`},
{`^\d{1,2}[/](1[0-2]|[1-9])[/]((19|20)\d{2})$`},
{`^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])$`},
{"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"},
}
func TestRegex(t *testing.T) {
for _, r := range regexes {
for i := 0; i < 100; i++ {
// Try to compile regexTest
regCompile, err := regexp.Compile(r.test)
if err != nil {
t.Fatal("Error compiling. regex: ", r.test, " failed to compile:", err)
}
// Generate string and test it matches the regex syntax
reg := Regex(r.test)
if !regCompile.MatchString(reg) {
t.Error("Generated data does not match regex. Regex: ", r.test, " output: ", reg)
}
}
}
}
func TestRegex_Struct(t *testing.T) {
Seed(11)
type Reggy struct {
Str1 string `fake:"{regex:^\\d+$}"`
Str2 string `fake:"{regex:\\D{3}}"`
Str3 string `fake:"{regex:Z{2,5}}"`
Str4 string `fake:"{regex:[^1]{3,5}}"`
Str5 string `fake:"{regex:(ab|bc)def}"`
Str6 string `fake:"{regex:((123)?){3}}"`
Str7 string `fake:"{regex:[^abcdef]{5}}"`
Str8 string `fake:"{regex:[a-zA-Z]{10}}"`
Str9 string `fake:"{regex:[[:upper:]]{5}}"`
Str10 string `fake:"{regex:[^0-5a-z\\s]{5}}"`
Str11 string `fake:"{regex:123[0-2]+.*\\w{3}}"`
Str12 string `fake:"{regex:(hello|world|whats|up)}"`
Str13 string `fake:"{regex:^\\d{1,2}[/](1[0-2]|[1-9])[/]((19|20)\\d{2})$}"`
}
rg := Reggy{}
Struct(&rg)
// Test Str1 matches regex
if !regexp.MustCompile(`^\d+$`).MatchString(rg.Str1) {
t.Errorf("Str1 should match regex: ^\\d+$ got: %s", rg.Str1)
}
if !regexp.MustCompile(`\D{3}`).MatchString(rg.Str2) {
t.Errorf("Str2 should match regex: \\D{3} got: %s", rg.Str2)
}
if !regexp.MustCompile(`Z{2,5}`).MatchString(rg.Str3) {
t.Errorf("Str3 should match regex: Z{2,5} got: %s", rg.Str3)
}
if !regexp.MustCompile(`[^1]{3,5}`).MatchString(rg.Str4) {
t.Errorf("Str4 should match regex: [^1]{3,5} got: %s", rg.Str4)
}
if !regexp.MustCompile(`(ab|bc)def`).MatchString(rg.Str5) {
t.Errorf("Str5 should match regex: (ab|bc)def got: %s", rg.Str5)
}
if !regexp.MustCompile(`((123)?){3}`).MatchString(rg.Str6) {
t.Errorf("Str6 should match regex: ((123)?){3} got: %s", rg.Str6)
}
if !regexp.MustCompile(`[^abcdef]{5}`).MatchString(rg.Str7) {
t.Errorf("Str7 should match regex: [^abcdef]{5} got: %s", rg.Str7)
}
if !regexp.MustCompile(`[a-zA-Z]{10}`).MatchString(rg.Str8) {
t.Errorf("Str8 should match regex: [a-zA-Z]{10} got: %s", rg.Str8)
}
if !regexp.MustCompile(`[[:upper:]]{5}`).MatchString(rg.Str9) {
t.Errorf("Str9 should match regex: [[:upper:]]{5} got: %s", rg.Str9)
}
if !regexp.MustCompile(`[^0-5a-z\s]{5}`).MatchString(rg.Str10) {
t.Errorf("Str10 should match regex: [^0-5a-z\\s]{5} got: %s", rg.Str10)
}
if !regexp.MustCompile(`123[0-2]+.*\w{3}`).MatchString(rg.Str11) {
t.Errorf("Str11 should match regex: 123[0-2]+.*\\w{3} got: %s", rg.Str11)
}
if !regexp.MustCompile(`(hello|world|whats|up)`).MatchString(rg.Str12) {
t.Errorf("Str12 should match regex: (hello|world|whats|up) got: %s", rg.Str12)
}
if !regexp.MustCompile(`^\d{1,2}[/](1[0-2]|[1-9])[/]((19|20)\d{2})$`).MatchString(rg.Str13) {
t.Errorf("Str13 should match regex: ^\\d{1,2}[/](1[0-2]|[1-9])[/]((19|20)\\d{2})$ got: %s", rg.Str13)
}
}
func BenchmarkRegex(b *testing.B) {
for i := 0; i < b.N; i++ {
Regex(`(hello|world|whats|up`)
}
}
func BenchmarkRegexEmail(b *testing.B) {
for i := 0; i < b.N; i++ {
Regex("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
}
}
func ExampleMap() {
Seed(11)
fmt.Println(Map())
// Output: map[a:map[that:could] above:[sometimes vast whom us scarcely these huh] am:map[how:9310653] depend:map[hey:5402877] out:381794.78 since:916659.94 these:map[frequently:2314010]]
}
func ExampleFaker_Map() {
f := New(11)
fmt.Println(f.Map())
// Output: map[a:map[that:could] above:[sometimes vast whom us scarcely these huh] am:map[how:9310653] depend:map[hey:5402877] out:381794.78 since:916659.94 these:map[frequently:2314010]]
}
func TestMap(t *testing.T) {
for i := 0; i < 100; i++ {
Map()
}
}
func BenchmarkMap(b *testing.B) {
for i := 0; i < b.N; i++ {
Map()
}
}