forked from brianvoe/gofakeit
/
sql_test.go
167 lines (132 loc) · 3.38 KB
/
sql_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
package gofakeit
import (
"fmt"
"math/rand"
"testing"
)
func TestMultiSQLInsert(t *testing.T) {
Seed(11)
res, _ := SQLInsert(&SQLOptions{
Table: "People",
EntryCount: 3,
Fields: []Field{
{Name: "first_name", Function: "firstname"},
{Name: "last_name", Function: "lastname"},
{Name: "age", Function: "number", Params: MapParams{"min": {"1"}, "max": {"99"}}},
},
})
fmt.Println(string(res))
// Output:
// INSERT INTO People VALUES ('Markus', 'Moen', 21), ('Anibal', 'Kozey', 60), ('Sylvan', 'Mraz', 59);
}
func TestMultiSQLInsertJSON(t *testing.T) {
Seed(12)
AddFuncLookup("jsonperson", Info{
Category: "custom",
Description: "random JSON of a person",
Example: `{"first_name":"Bob", "last_name":"Jones"}`,
Output: "[]byte",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
v, _ := JSON(&JSONOptions{
Type: "object",
RowCount: 1,
Fields: []Field{
{Name: "first_name", Function: "firstname"},
{Name: "last_name", Function: "lastname"},
},
})
return v, nil
},
})
type RandomPerson struct {
FakePerson []byte `fake:{jsonperson}"`
}
var f RandomPerson
Struct(&f)
fmt.Printf("%s\n", (f.FakePerson))
}
func TestMultiSQLInsertFloat(t *testing.T) {
Seed(11)
res, _ := SQLInsert(&SQLOptions{
Table: "People",
EntryCount: 3,
Fields: []Field{
{Name: "first_name", Function: "firstname"},
{Name: "last_name", Function: "lastname"},
{Name: "balance", Function: "float32"},
},
})
fmt.Println(string(res))
// Output:
// INSERT INTO People VALUES ('Markus', 'Moen', 2.7766049e+38), ('Anibal', 'Kozey', 3.8815667e+37), ('Sylvan', 'Mraz', 1.6268478e+38);
}
func TestMultiSQLInsertAutoincrement(t *testing.T) {
Seed(11)
res, _ := SQLInsert(&SQLOptions{
Table: "People",
EntryCount: 3,
Fields: []Field{
{Name: "id", Function: "autoincrement"},
{Name: "first_name", Function: "firstname"},
{Name: "last_name", Function: "lastname"},
{Name: "age", Function: "number", Params: MapParams{"min": {"1"}, "max": {"99"}}},
},
})
fmt.Println(string(res))
// Output:
// INSERT INTO People VALUES (1, 'Markus', 'Moen', 21), (2, 'Anibal', 'Kozey', 60), (3, 'Sylvan', 'Mraz', 59);
}
func TestSignleSQLInsert(t *testing.T) {
Seed(114)
res, _ := SQLInsert(&SQLOptions{
Table: "People",
EntryCount: 1,
Fields: []Field{
{Name: "first_name", Function: "firstname"},
{Name: "last_name", Function: "lastname"},
},
})
fmt.Println(string(res))
// Output:
// INSERT INTO People VALUES ('Colt', 'Koss');
}
func TestSQLInsertNoFields(t *testing.T) {
Seed(114)
_, err := SQLInsert(&SQLOptions{
Table: "People",
EntryCount: 1,
Fields: []Field{},
})
if err != nil {
fmt.Println(err.Error())
}
// Output:
// must pass fields in order to generate SQL queries
}
func TestSQLInsertNilFields(t *testing.T) {
Seed(114)
_, err := SQLInsert(&SQLOptions{
Table: "People",
EntryCount: 1,
})
if err != nil {
fmt.Println(err.Error())
}
// Output:
// must pass fields in order to generate SQL queries
}
func TestSQLInsertNilTable(t *testing.T) {
Seed(11)
_, err := SQLInsert(&SQLOptions{
EntryCount: 3,
Fields: []Field{
{Name: "first_name", Function: "firstname"},
{Name: "last_name", Function: "lastname"},
},
})
if err != nil {
fmt.Println(err.Error())
}
// Output:
// must provide table name to generate SQL
}