forked from elgris/sqrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_test.go
126 lines (99 loc) · 2.53 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
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
package sqrl
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInsertBuilderToSql(t *testing.T) {
b := Insert("").
Prefix("WITH prefix AS ?", 0).
Into("a").
Options("DELAYED", "IGNORE").
Columns("b", "c").
Values(1, 2).
Values(3, Expr("? + 1", 4)).
Suffix("RETURNING ?", 5)
sql, args, err := b.ToSQL()
assert.NoError(t, err)
expectedSql :=
"WITH prefix AS ? " +
"INSERT DELAYED IGNORE INTO a (b,c) VALUES (?,?),(?,? + 1) " +
"RETURNING ?"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{0, 1, 2, 3, 4, 5}
assert.Equal(t, expectedArgs, args)
}
func TestInsertBuilderToSqlErr(t *testing.T) {
_, _, err := Insert("").Values(1).ToSQL()
assert.Error(t, err)
_, _, err = Insert("x").ToSQL()
assert.Error(t, err)
}
func TestInsertBuilderPlaceholders(t *testing.T) {
b := Insert("test").Values(1, 2)
sql, _, _ := b.PlaceholderFormat(Question).ToSQL()
assert.Equal(t, "INSERT INTO test VALUES (?,?)", sql)
sql, _, _ = b.PlaceholderFormat(Dollar).ToSQL()
assert.Equal(t, "INSERT INTO test VALUES ($1,$2)", sql)
}
func TestInsertBuilderRunners(t *testing.T) {
db := &DBStub{}
b := Insert("test").Values(1).RunWith(db)
expectedSql := "INSERT INTO test VALUES (?)"
b.Exec()
assert.Equal(t, expectedSql, db.LastExecSql)
b.ExecContext(context.TODO())
assert.Equal(t, expectedSql, db.LastExecSql)
}
func TestInsertBuilderNoRunner(t *testing.T) {
b := Insert("test").Values(1)
_, err := b.Exec()
assert.Equal(t, ErrRunnerNotSet, err)
_, err = b.ExecContext(context.TODO())
assert.Equal(t, ErrRunnerNotSet, err)
}
func TestInsertBuilderSetMap(t *testing.T) {
b := Insert("table").SetMap(Eq{"field1": 1})
sql, args, err := b.ToSQL()
assert.NoError(t, err)
expectedSql := "INSERT INTO table (field1) VALUES (?)"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{1}
assert.Equal(t, expectedArgs, args)
}
func BenchmarkInsertSetMap(b *testing.B) {
m := map[string]interface{}{
"test": 3,
"test2": 3,
"test3": 3,
"test4": 3,
"test5": 3,
"test6": 3,
"test7": 3,
"test8": 3,
"test9": 3,
"test10": 3}
for n := 0; n < b.N; n++ {
Insert("table").SetMap(m)
}
}
func BenchmarkInsertToSQL(b *testing.B) {
qb := Insert("test").
Prefix("Awesome Prefix").
Into("temp").
SetMap(map[string]interface{}{
"test": 3,
"test2": 3,
"test3": 3,
"test4": 3,
"test5": 3,
"test6": 3,
"test7": 3,
"test8": 3,
"test9": 3,
"test10": 3}).
Suffix("Awesome Suffix")
for n := 0; n < b.N; n++ {
qb.ToSQL()
}
}