-
Notifications
You must be signed in to change notification settings - Fork 2
/
insert_test.go
90 lines (79 loc) · 2.28 KB
/
insert_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
package sol
import "testing"
func TestInsert(t *testing.T) {
expect := NewTester(t, &defaultDialect{})
// By default, an INSERT without values will assume a single entry
expect.SQL(
contacts.Insert(),
`INSERT INTO contacts (id, user_id, key, value) VALUES ($1, $2, $3, $4)`,
nil, nil, nil, nil,
)
expect.SQL(
Insert(users.C("name"), users.C("password")),
`INSERT INTO users (name, password) VALUES ($1, $2)`,
nil, nil,
)
// Use structs
admin := user{Name: "admin", Email: "[email protected]"}
expect.SQL(
users.Insert().Values(admin),
`INSERT INTO users (email, name) VALUES ($1, $2)`,
"[email protected]", "admin",
)
expect.SQL(
users.Insert().Values(&admin),
`INSERT INTO users (email, name) VALUES ($1, $2)`,
"[email protected]", "admin",
)
exampleUsers := []user{
admin,
user{Name: "client", Email: "[email protected]"},
}
expect.SQL(
users.Insert().Values(exampleUsers),
`INSERT INTO users (email, name) VALUES ($1, $2), ($3, $4)`,
"[email protected]", "admin", "[email protected]", "client",
)
expect.SQL(
users.Insert().Values(&exampleUsers),
`INSERT INTO users (email, name) VALUES ($1, $2), ($3, $4)`,
"[email protected]", "admin", "[email protected]", "client",
)
// Use sql.Values
expect.SQL(
users.Insert().Values(Values{"id": 1, "name": "user"}),
`INSERT INTO users (id, name) VALUES ($1, $2)`,
1, "user",
)
github := Values{"UserID": 1, "KEY": "github"}
expect.SQL(
contacts.Insert().Values(github),
`INSERT INTO contacts (user_id, key) VALUES ($1, $2)`,
1, "github",
)
expect.SQL(
contacts.Insert().Values(&github),
`INSERT INTO contacts (user_id, key) VALUES ($1, $2)`,
1, "github",
)
exampleContacts := []Values{
github,
Values{"UserID": 1, "KEY": "bitbucket"},
}
expect.SQL(
contacts.Insert().Values(exampleContacts),
`INSERT INTO contacts (user_id, key) VALUES ($1, $2), ($3, $4)`,
1, "github", 1, "bitbucket",
)
expect.SQL(
contacts.Insert().Values(&exampleContacts),
`INSERT INTO contacts (user_id, key) VALUES ($1, $2), ($3, $4)`,
1, "github", 1, "bitbucket",
)
// Handle errors
expect.Error(Insert())
expect.Error(users.Insert().Values("a"))
expect.Error(users.Insert().Values([]int{1}))
expect.Error(users.Insert().Values([]struct{}{}))
expect.Error(users.Insert().Values(nil))
}