forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_test.go
131 lines (111 loc) · 3 KB
/
csv_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
package gofakeit
import (
"fmt"
"strings"
"testing"
)
func ExampleCSV_array() {
Seed(11)
value, err := CSV(&CSVOptions{
RowCount: 3,
Fields: []Field{
{Name: "id", Function: "autoincrement"},
{Name: "first_name", Function: "firstname"},
{Name: "last_name", Function: "lastname"},
{Name: "password", Function: "password", Params: MapParams{"special": {"false"}}},
},
})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output: id,first_name,last_name,password
// 1,Sonny,Stiedemann,8nwf0o3sBXcR
// 2,Verda,Brakus,3beWLpq75Lua
// 3,Jules,Cremin,Uu38J14Y8W82
}
func ExampleFaker_CSV_array() {
f := New(11)
value, err := f.CSV(&CSVOptions{
RowCount: 3,
Fields: []Field{
{Name: "id", Function: "autoincrement"},
{Name: "first_name", Function: "firstname"},
{Name: "last_name", Function: "lastname"},
{Name: "password", Function: "password", Params: MapParams{"special": {"false"}}},
},
})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output: id,first_name,last_name,password
// 1,Sonny,Stiedemann,8nwf0o3sBXcR
// 2,Verda,Brakus,3beWLpq75Lua
// 3,Jules,Cremin,Uu38J14Y8W82
}
func TestCSVLookup(t *testing.T) {
faker := New(0)
info := GetFuncLookup("csv")
m := MapParams{
"rowcount": {"10"},
"fields": {
`{"name":"id","function":"autoincrement"}`,
`{"name":"first_name","function":"firstname"}`,
`{"name":"password","function":"password","params":{"special":["false"],"length":["20"]}}`,
`{"name":"address","function":"address"}`,
`{
"name":"json",
"function":"json",
"params":{
"type":"object",
"fields":[
{"name":"id","function":"autoincrement"},
{"name":"first_name","function":"firstname"},
{"name":"last_name","function":"lastname"},
{"name":"password","function":"password","params":{"special":"false","length":"20"}}
]
}
}`,
},
}
output, err := info.Generate(faker, &m, info)
if err != nil {
t.Fatal(err.Error())
}
value := string(output.([]byte))
// Check that value has the correct number of rows via new line characters plus 1 for the header
if strings.Count(value, "\n") != 11 {
t.Error("Expected 10+1(header row) rows, got", strings.Count(value, "\n")+1)
}
// t.Fatal(fmt.Sprintf("%s", value.([]byte)))
}
func TestCSVNoOptions(t *testing.T) {
Seed(11)
// if CSVOptions is nil -> get a random CSVOptions
_, err := CSV(nil)
if err != nil {
t.Fatal(err.Error())
}
}
func BenchmarkCSVLookup100(b *testing.B) {
faker := New(0)
for i := 0; i < b.N; i++ {
info := GetFuncLookup("csv")
m := MapParams{
"rowcount": {"100"},
"fields": {
`{"name":"id","function":"autoincrement"}`,
`{"name":"first_name","function":"firstname"}`,
`{"name":"last_name","function":"lastname"}`,
`{"name":"password","function":"password"}`,
`{"name":"description","function":"paragraph"}`,
`{"name":"created_at","function":"date"}`,
},
}
_, err := info.Generate(faker, &m, info)
if err != nil {
b.Fatal(err.Error())
}
}
}