forked from chuckpreslar/codex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollector_test.go
More file actions
102 lines (92 loc) · 2.88 KB
/
collector_test.go
File metadata and controls
102 lines (92 loc) · 2.88 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
package codex
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCollectorEmpty(t *testing.T) {
c := &Collector{}
assert.Equal(t, 0, cap(c.sqlBuf.Bytes()))
assert.Equal(t, "", c.String())
assert.Empty(t, c.Args())
}
func TestCollector(t *testing.T) {
c := NewCollector()
assert.Equal(t, EXPECTED_SQL_QUERY_LEN, cap(c.sqlBuf.Bytes()))
c.AppendSqlStr("WHERE id")
c.AppendSqlByte(EQUAL)
c.AppendSqlByte(QUESTION)
c.AppendArg(77)
assert.Equal(t, "WHERE id=?", c.String())
assert.Equal(t, []interface{}{77}, c.Args())
}
func TestPostgresCollectorEmpty(t *testing.T) {
c := &PostgresCollector{}
assert.Equal(t, 0, cap(c.sqlBuf.Bytes()))
assert.Equal(t, "", c.String())
assert.Empty(t, c.Args())
}
func TestPostgresCollectorAppendByte(t *testing.T) {
c := NewPostgresCollector()
assert.Equal(t, EXPECTED_SQL_QUERY_LEN, cap(c.sqlBuf.Bytes()))
c.AppendSqlByte(LT)
assert.Equal(t, "<", c.String())
c.AppendSqlByte(EQUAL)
assert.Equal(t, "<=", c.String())
c.AppendSqlByte(SPACE)
assert.Equal(t, "<= ", c.String())
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3,$4", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3,$4,$5", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3,$4,$5,$6", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3,$4,$5,$6,$7", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3,$4,$5,$6,$7,$8", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3,$4,$5,$6,$7,$8,$9", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3,$4,$5,$6,$7,$8,$9,$10", c.String())
c.AppendSqlByte(COMMA)
c.AppendSqlByte(QUESTION)
assert.Equal(t, "<= $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11", c.String())
}
func TestPostgresCollector(t *testing.T) {
c := NewPostgresCollector()
c.AppendSqlStr("WHERE id")
c.AppendSqlByte(EQUAL)
c.AppendSqlByte(QUESTION)
c.AppendArg(77)
assert.Equal(t, "WHERE id=$1", c.String())
assert.Equal(t, []interface{}{77}, c.Args())
}
func TestPostgresCollectorAppendSqlStr(t *testing.T) {
c := NewPostgresCollector()
c.AppendSqlStr("WHERE id=? AND name=?")
c.AppendArg(77)
c.AppendArg("Hans")
assert.Equal(t, "WHERE id=$1 AND name=$2", c.String())
assert.Equal(t, []interface{}{77, "Hans"}, c.Args())
}
func TestPostgresCollectorAppendSqlStrWithoutPlaceholder(t *testing.T) {
c := NewPostgresCollector()
c.AppendSqlStr("WHERE id=foo_id AND bar=bla")
assert.Equal(t, "WHERE id=foo_id AND bar=bla", c.String())
assert.Empty(t, c.Args())
}