-
Notifications
You must be signed in to change notification settings - Fork 27
/
sqlbuffer_test.go
124 lines (109 loc) · 3.23 KB
/
sqlbuffer_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
package godb
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestNewSQLBuffer(t *testing.T) {
Convey("Calling NewSQLBuffer creates the buffer using the given lengths", t, func() {
b := NewSQLBuffer(10, 20)
So(b.sql.Cap(), ShouldEqual, 10)
So(cap(b.arguments), ShouldEqual, 20)
})
}
func TestSQLBufferSQL(t *testing.T) {
Convey("SQL returns the builded query string", t, func() {
b := NewSQLBuffer(16, 4)
sql := "I'M SQL"
b.Write(sql)
So(b.SQL(), ShouldEqual, sql)
})
}
func TestSQLBufferArguments(t *testing.T) {
Convey("Arguments returns the arguments from the builded query", t, func() {
b := NewSQLBuffer(16, 4)
b.Write("", 1, 2, 3)
arguments := b.Arguments()
So(len(arguments), ShouldEqual, 3)
So(arguments[0], ShouldEqual, 1)
So(arguments[1], ShouldEqual, 2)
So(arguments[2], ShouldEqual, 3)
})
}
func TestSQLBufferErr(t *testing.T) {
Convey("Err returns the error", t, func() {
b := NewSQLBuffer(16, 4)
b.WriteCondition(Q("Bad Condition", 123))
So(b.Err(), ShouldNotBeNil)
})
}
func TestSQLBufferSQLLen(t *testing.T) {
Convey("SQLLen returns the len of the sql buffer", t, func() {
b := NewSQLBuffer(16, 4)
b.Write("I'M SQL")
So(b.SQLLen(), ShouldEqual, 7)
})
}
func TestSQLBufferWrite(t *testing.T) {
Convey("Write append string and arguments to the buffer", t, func() {
b := NewSQLBuffer(16, 4)
sql := "WHERE id = ?"
b.Write(sql, 123)
So(b.SQL(), ShouldEqual, sql)
So(len(b.Arguments()), ShouldEqual, 1)
So(b.Arguments()[0], ShouldEqual, 123)
})
}
func TestSQLBufferWriteIfNotEmpty(t *testing.T) {
Convey("WriteIfNotEmpty append string and arguments to the buffer only if the buffer isn't empty", t, func() {
b := NewSQLBuffer(16, 4)
b.WriteIfNotEmpty("something", 123)
So(b.SQLLen(), ShouldEqual, 0)
So(len(b.Arguments()), ShouldEqual, 0)
b.Write("will not be empty")
sql := "WHERE id = ?"
b.WriteIfNotEmpty(sql, 123)
So(b.SQL(), ShouldEndWith, sql)
So(len(b.Arguments()), ShouldEqual, 1)
So(b.Arguments()[0], ShouldEqual, 123)
})
}
func TestSQLBufferWriteBytes(t *testing.T) {
Convey("WriteBytes append bytes and arguments to the buffer", t, func() {
b := NewSQLBuffer(16, 4)
sql := "WHERE id = ?"
sqlBytes := []byte(sql)
b.WriteBytes(sqlBytes, 123)
So(b.SQL(), ShouldEqual, sql)
So(len(b.Arguments()), ShouldEqual, 1)
So(b.Arguments()[0], ShouldEqual, 123)
})
}
func TestSQLBufferWriteStrings(t *testing.T) {
Convey("WriteStrings writes the givens strings, using a separator", t, func() {
b := NewSQLBuffer(16, 4)
b.WriteStrings(",", "A", "B", "C")
So(b.SQL(), ShouldEqual, "A,B,C")
})
}
func TestSQLBufferAppend(t *testing.T) {
Convey("Append appends the given buffer to the current", t, func() {
b := NewSQLBuffer(16, 4)
b.Write("FIRST", 123)
other := NewSQLBuffer(0, 0)
other.Write("SECOND", 456)
b.Append(other)
So(b.SQL(), ShouldEqual, "FIRSTSECOND")
So(len(b.Arguments()), ShouldEqual, 2)
})
}
func TestSQLBufferWriteCondition(t *testing.T) {
Convey("WriteCondition write the condition to the buffer", t, func() {
b := NewSQLBuffer(16, 4)
sql := "WHERE id = ?"
q := Q(sql, 123)
b.WriteCondition(q)
So(b.SQL(), ShouldEqual, sql)
So(len(b.Arguments()), ShouldEqual, 1)
So(b.Arguments()[0], ShouldEqual, 123)
})
}