forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_test.go
66 lines (54 loc) · 2.11 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
package qb
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestInsert(t *testing.T) {
sqlite := NewDialect("sqlite3")
sqlite.SetEscaping(true)
mysql := NewDialect("mysql")
mysql.SetEscaping(true)
postgres := NewDialect("postgres")
postgres.SetEscaping(true)
usersTable := Table(
"users",
Column("id", Varchar().Size(36)),
Column("email", Varchar().Unique()),
)
var statement *Stmt
ins := Insert(usersTable).Values(map[string]interface{}{
"id": "9883cf81-3b56-4151-ae4e-3903c5bc436d",
"email": "[email protected]",
})
statement = ins.Build(sqlite)
assert.Contains(t, statement.SQL(), "INSERT INTO users")
assert.Contains(t, statement.SQL(), "id", "email")
assert.Contains(t, statement.SQL(), "VALUES(?, ?)")
assert.Contains(t, statement.Bindings(), "9883cf81-3b56-4151-ae4e-3903c5bc436d")
assert.Contains(t, statement.Bindings(), "[email protected]")
statement = ins.Build(mysql)
assert.Contains(t, statement.SQL(), "INSERT INTO `users`")
assert.Contains(t, statement.SQL(), "`id`", "`email`")
assert.Contains(t, statement.SQL(), "VALUES(?, ?)")
assert.Contains(t, statement.Bindings(), "9883cf81-3b56-4151-ae4e-3903c5bc436d")
assert.Contains(t, statement.Bindings(), "[email protected]")
statement = ins.Build(postgres)
assert.Contains(t, statement.SQL(), "INSERT INTO \"users\"")
assert.Contains(t, statement.SQL(), "\"id\"", "\"email\"")
assert.Contains(t, statement.SQL(), "VALUES($1, $2)")
assert.Contains(t, statement.Bindings(), "9883cf81-3b56-4151-ae4e-3903c5bc436d")
assert.Contains(t, statement.Bindings(), "[email protected]")
postgres.Reset()
statement = Insert(usersTable).
Values(map[string]interface{}{
"id": "9883cf81-3b56-4151-ae4e-3903c5bc436d",
"email": "[email protected]",
}).
Returning("id", "email").
Build(postgres)
assert.Contains(t, statement.SQL(), "INSERT INTO \"users\"")
assert.Contains(t, statement.SQL(), "\"id\"", "\"email\"")
assert.Contains(t, statement.SQL(), "VALUES($1, $2)")
assert.Contains(t, statement.SQL(), "RETURNING \"id\", \"email\";")
assert.Contains(t, statement.Bindings(), "9883cf81-3b56-4151-ae4e-3903c5bc436d", "[email protected]")
}