-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
placeholder_test.go
52 lines (46 loc) · 1.19 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
package pgq
import (
"strings"
"testing"
)
func TestDollar(t *testing.T) {
t.Parallel()
sql := "x = ? AND y = ?"
s, err := dollarPlaceholder(sql)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if want := "x = $1 AND y = $2"; s != want {
t.Errorf("expected %q, got %q instead", want, s)
}
}
func TestPlaceholders(t *testing.T) {
t.Parallel()
got := Placeholders(2)
if want := "?,?"; got != want {
t.Errorf("expected %q, got %q instead", want, got)
}
}
func TestEscapeDollar(t *testing.T) {
t.Parallel()
sql := "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE \"data\" -> 'tags' ??| array['?'] AND enabled = ?"
s, err := dollarPlaceholder(sql)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE \"data\" -> 'tags' ?| array['$1'] AND enabled = $2"
if s != want {
t.Errorf("expected %q, got %q instead", want, 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)
}