forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupsert_test.go
78 lines (66 loc) · 2.72 KB
/
upsert_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
package qb
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestUpsert(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()),
Column("created_at", Timestamp().NotNull()),
PrimaryKey("id"),
)
var statement *Stmt
ups := Upsert(usersTable).Values(map[string]interface{}{
"id": "9883cf81-3b56-4151-ae4e-3903c5bc436d",
"email": "[email protected]",
"created_at": time.Now().UTC().String(),
})
statement = ups.Build(sqlite)
assert.Contains(t, statement.SQL(), "REPLACE INTO users")
assert.Contains(t, statement.SQL(), "id", "email", "created_at")
assert.Contains(t, statement.SQL(), "VALUES(?, ?, ?)")
assert.Contains(t, statement.Bindings(), "9883cf81-3b56-4151-ae4e-3903c5bc436d")
assert.Contains(t, statement.Bindings(), "[email protected]")
statement = ups.Build(mysql)
assert.Contains(t, statement.SQL(), "INSERT INTO `users`")
assert.Contains(t, statement.SQL(), "`id`", "`email`", "`created_at`")
assert.Contains(t, statement.SQL(), "VALUES(?, ?, ?)")
assert.Contains(t, statement.SQL(), "ON DUPLICATE KEY UPDATE")
assert.Contains(t, statement.SQL(), "`id` = ?", "`email` = ?", "`created_at` = ?")
assert.Contains(t, statement.Bindings(), "9883cf81-3b56-4151-ae4e-3903c5bc436d")
assert.Contains(t, statement.Bindings(), "[email protected]")
statement = ups.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, $3)")
assert.Contains(t, statement.SQL(), "ON CONFLICT", "DO UPDATE SET")
assert.Contains(t, statement.Bindings(), "9883cf81-3b56-4151-ae4e-3903c5bc436d")
assert.Contains(t, statement.Bindings(), "[email protected]")
assert.Equal(t, len(statement.Bindings()), 6)
postgres.Reset()
statement = Upsert(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(), "ON CONFLICT")
assert.Contains(t, statement.SQL(), "DO UPDATE SET")
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")
assert.Contains(t, statement.Bindings(), "[email protected]")
assert.Equal(t, len(statement.Bindings()), 4)
}