-
Notifications
You must be signed in to change notification settings - Fork 466
/
placeholder_test.go
67 lines (55 loc) · 1.96 KB
/
placeholder_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
package squirrel
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestQuestion(t *testing.T) {
sql := "x = ? AND y = ?"
s, _ := Question.ReplacePlaceholders(sql)
assert.Equal(t, sql, s)
}
func TestDollar(t *testing.T) {
sql := "x = ? AND y = ?"
s, _ := Dollar.ReplacePlaceholders(sql)
assert.Equal(t, "x = $1 AND y = $2", s)
}
func TestColon(t *testing.T) {
sql := "x = ? AND y = ?"
s, _ := Colon.ReplacePlaceholders(sql)
assert.Equal(t, "x = :1 AND y = :2", s)
}
func TestAtp(t *testing.T) {
sql := "x = ? AND y = ?"
s, _ := AtP.ReplacePlaceholders(sql)
assert.Equal(t, "x = @p1 AND y = @p2", s)
}
func TestPlaceholders(t *testing.T) {
assert.Equal(t, Placeholders(2), "?,?")
}
func TestEscapeDollar(t *testing.T) {
sql := "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE \"data\" -> 'tags' ??| array['?'] AND enabled = ?"
s, _ := Dollar.ReplacePlaceholders(sql)
assert.Equal(t, "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE \"data\" -> 'tags' ?| array['$1'] AND enabled = $2", s)
}
func TestEscapeColon(t *testing.T) {
sql := "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE \"data\" -> 'tags' ??| array['?'] AND enabled = ?"
s, _ := Colon.ReplacePlaceholders(sql)
assert.Equal(t, "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE \"data\" -> 'tags' ?| array[':1'] AND enabled = :2", s)
}
func TestEscapeAtp(t *testing.T) {
sql := "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE \"data\" -> 'tags' ??| array['?'] AND enabled = ?"
s, _ := AtP.ReplacePlaceholders(sql)
assert.Equal(t, "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE \"data\" -> 'tags' ?| array['@p1'] AND enabled = @p2", s)
}
func BenchmarkPlaceholdersArray(b *testing.B) {
var count = b.N
placeholders := make([]string, count)
for i := 0; i < count; i++ {
placeholders[i] = "?"
}
var _ = strings.Join(placeholders, ",")
}
func BenchmarkPlaceholdersStrings(b *testing.B) {
Placeholders(b.N)
}