-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement_test.go
More file actions
117 lines (98 loc) · 2.82 KB
/
statement_test.go
File metadata and controls
117 lines (98 loc) · 2.82 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
package sqlb
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRawStatement(t *testing.T) {
statement := RawStatement{
Raw: "EXPLAIN",
}
sql, args, err := statement.ToExpr()
assert.Equal(t, "EXPLAIN", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestRawStatementWithArgs(t *testing.T) {
statement := RawStatement{
Raw: "EXPLAIN",
Args: []interface{}{"1", 1},
}
sql, args, err := statement.ToExpr()
assert.Equal(t, "EXPLAIN", sql, "they should be equal")
assert.Equal(t, []interface{}{"1", 1}, args, "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestSelectStatement(t *testing.T) {
statement := SelectStatement{
Columns: []string{"a", "b"},
}
sql, args, err := statement.ToExpr()
assert.Equal(t, "SELECT a, b", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestFromStatement(t *testing.T) {
statement := FromStatement{
Tables: []string{"a", "b"},
}
sql, args, err := statement.ToExpr()
assert.Equal(t, "FROM a, b", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestWhereStatement(t *testing.T) {
statement := WhereStatement{
Exprs: []Expr{
Eq{
Column: "foo",
Value: "bar",
},
},
}
sql, args, err := statement.ToExpr()
assert.Equal(t, "WHERE foo = ?", sql, "they should be equal")
assert.Equal(t, []interface{}{"bar"}, args, "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestLimitStatement(t *testing.T) {
statement := LimitStatement{
Limit: 10,
}
sql, args, err := statement.ToExpr()
assert.Equal(t, "LIMIT 10", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestOffsetStatement(t *testing.T) {
statement := OffsetStatement{
Offset: 0,
}
sql, args, err := statement.ToExpr()
assert.Equal(t, "OFFSET 0", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestOrderByStatement(t *testing.T) {
tests := []struct {
column string
descending bool
wantedSQL string
}{
{"foo", false, "ORDER BY foo ASC"},
{"foo", true, "ORDER BY foo DESC"},
}
for _, test := range tests {
statement := OrderByStatement{
Orders: []Order{
Order{
Column: test.column,
Descending: test.descending,
},
},
}
sql, args, err := statement.ToExpr()
assert.Equal(t, test.wantedSQL, sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
}