-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind_test.go
More file actions
127 lines (105 loc) · 2.64 KB
/
bind_test.go
File metadata and controls
127 lines (105 loc) · 2.64 KB
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
127
package pg_test
import (
"testing"
// Packages
"github.com/mutablelogic/go-pg"
"github.com/stretchr/testify/assert"
)
func Test_Bind_001(t *testing.T) {
assert := assert.New(t)
t.Run("1", func(t *testing.T) {
bind := pg.NewBind("a", "b")
assert.NotNil(bind)
assert.True(bind.Has("a"))
assert.Equal("b", bind.Get("a"))
})
t.Run("2", func(t *testing.T) {
bind := pg.NewBind("a", "b", "c")
assert.Nil(bind)
})
t.Run("3", func(t *testing.T) {
bind := pg.NewBind("a", 100)
assert.NotNil(bind)
assert.True(bind.Has("a"))
assert.Equal(100, bind.Get("a"))
})
t.Run("4", func(t *testing.T) {
bind := pg.NewBind()
assert.NotNil(bind)
assert.Equal("@a", bind.Set("a", "b"))
assert.True(bind.Has("a"))
assert.Equal("b", bind.Get("a"))
})
t.Run("5", func(t *testing.T) {
bind := pg.NewBind("", "b")
assert.Nil(bind)
})
t.Run("6", func(t *testing.T) {
bind := pg.NewBind()
assert.NotNil(bind)
assert.Equal("", bind.Set("", "b"))
})
}
func Test_Bind_002(t *testing.T) {
assert := assert.New(t)
tests := []struct {
In string
Out string
}{
{In: `$schema`, Out: "schema"},
{In: `${'schema'}`, Out: "'schema'"},
{In: `${"schema"}`, Out: `"schema"`},
{In: `$1`, Out: `$1`},
{In: `${1}`, Out: `$1`},
{In: `$$`, Out: `$$`},
{In: `${'single'}`, Out: `'''single'''`},
{In: `${"single"}`, Out: `"'single'"`},
{In: `${'double'}`, Out: `'"double"'`},
{In: `${"double"}`, Out: `"""double"""`},
}
bind := pg.NewBind(
"schema", "schema",
"single", "'single'",
"double", "\"double\"",
)
for _, test := range tests {
t.Run(test.In, func(t *testing.T) {
assert.Equal(test.Out, bind.Replace(test.In))
})
}
}
func Test_Bind_003(t *testing.T) {
assert := assert.New(t)
bind := pg.NewBind(
"list", []string{"a", "b", "c"},
)
assert.Equal("IN ('a','b','c')", bind.Replace("IN (${'list'})"))
}
func Test_Query_001(t *testing.T) {
assert := assert.New(t)
// Test Bind.Query method - sets span name and returns resolved SQL
bind := pg.NewBind("user.select", "SELECT * FROM users WHERE id = @id")
sql := bind.Query("user.select")
assert.Equal("SELECT * FROM users WHERE id = @id", sql)
}
func Test_Bind_Copy_001(t *testing.T) {
assert := assert.New(t)
bind := pg.NewBind("a", "b")
copy := bind.Copy()
if assert.NotNil(copy) {
assert.Equal("b", copy.Get("a"))
}
copy.Set("a", "c")
assert.Equal("b", bind.Get("a"))
assert.Equal("c", copy.Get("a"))
}
func Test_Bind_Copy_002(t *testing.T) {
assert := assert.New(t)
bind := pg.NewBind("a", "b")
copy := bind.Copy("c", "d")
if assert.NotNil(copy) {
assert.Equal("b", copy.Get("a"))
assert.Equal("d", copy.Get("c"))
}
assert.False(bind.Has("c"))
}