-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalphabet_test.go
202 lines (162 loc) · 4.38 KB
/
alphabet_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package vtab_test
import (
"fmt"
"io"
"testing"
"github.com/augmentable-dev/vtab"
_ "github.com/augmentable-dev/vtab/pkg/sqlite"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"go.riyazali.net/sqlite"
)
var alphabet = []rune("abcdefghijklmnopqrstuvwxyz")
type alphabetIter struct {
current int
desc bool
}
func (i *alphabetIter) Column(ctx vtab.Context, c int) error {
switch alphabetIterCols[c].Name {
case "letter":
ctx.ResultText(string(alphabet[i.current]))
default:
return fmt.Errorf("unknown column")
}
return nil
}
func (i *alphabetIter) Next() (vtab.Row, error) {
totalIterations++
if i.desc {
i.current--
if i.current <= -1 {
return nil, io.EOF
}
} else {
i.current++
if i.current >= len(alphabet) {
return nil, io.EOF
}
}
return i, nil
}
var alphabetIterCols = []vtab.Column{
{Name: "letter", Type: "TEXT", OrderBy: vtab.ASC | vtab.DESC, Filters: []*vtab.ColumnFilter{
{Op: sqlite.INDEX_CONSTRAINT_GT}, {Op: sqlite.INDEX_CONSTRAINT_GE},
{Op: sqlite.INDEX_CONSTRAINT_LT}, {Op: sqlite.INDEX_CONSTRAINT_LE},
}},
}
var alphabetModule = vtab.NewTableFunc("alphabet", alphabetIterCols, func(constraints []*vtab.Constraint, order []*sqlite.OrderBy) (vtab.Iterator, error) {
var desc bool
current := -1
if len(order) == 1 && order[0].ColumnIndex == 0 {
if order[0].Desc {
desc = true
current = len(alphabet)
}
}
return &alphabetIter{current, desc}, nil
}, vtab.EarlyOrderByConstraintExit(true))
func TestAlphabetSimple(t *testing.T) {
db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
totalBefore := totalIterations
var contents []string
err = db.Select(&contents, "select * from alphabet")
if err != nil {
t.Fatal(err)
}
iterations := totalIterations - totalBefore
assert.Equal(t, 26, len(contents))
assert.Equal(t, 27, iterations)
assert.Equal(t, "a", contents[0])
assert.Equal(t, "b", contents[1])
assert.Equal(t, "y", contents[24])
assert.Equal(t, "z", contents[25])
}
func TestAlphabetDescGT(t *testing.T) {
db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
totalBefore := totalIterations
var contents []string
err = db.Select(&contents, "select * from alphabet where letter > 'm' order by letter desc")
if err != nil {
t.Fatal(err)
}
iterations := totalIterations - totalBefore
t.Log(contents)
assert.Equal(t, 13, len(contents))
assert.Equal(t, 14, iterations)
assert.Equal(t, "z", contents[0])
assert.Equal(t, "y", contents[1])
assert.Equal(t, "o", contents[11])
assert.Equal(t, "n", contents[12])
}
func TestAlphabetDescGTE(t *testing.T) {
db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
totalBefore := totalIterations
var contents []string
err = db.Select(&contents, "select * from alphabet where letter >= 'm' order by letter desc")
if err != nil {
t.Fatal(err)
}
iterations := totalIterations - totalBefore
t.Log(contents)
assert.Equal(t, 14, len(contents))
assert.Equal(t, 15, iterations)
assert.Equal(t, "z", contents[0])
assert.Equal(t, "y", contents[1])
assert.Equal(t, "n", contents[12])
assert.Equal(t, "m", contents[13])
}
func TestAlphabetAscLT(t *testing.T) {
db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
totalBefore := totalIterations
var contents []string
err = db.Select(&contents, "select * from alphabet where letter < 'm' order by letter asc")
if err != nil {
t.Fatal(err)
}
iterations := totalIterations - totalBefore
t.Log(contents)
assert.Equal(t, 12, len(contents))
assert.Equal(t, 13, iterations)
assert.Equal(t, "a", contents[0])
assert.Equal(t, "b", contents[1])
assert.Equal(t, "k", contents[10])
assert.Equal(t, "l", contents[11])
}
func TestAlphabetAscLE(t *testing.T) {
db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
totalBefore := totalIterations
var contents []string
err = db.Select(&contents, "select * from alphabet where letter <= 'm' order by letter asc")
if err != nil {
t.Fatal(err)
}
iterations := totalIterations - totalBefore
t.Log((contents))
assert.Equal(t, 13, len(contents))
assert.Equal(t, 14, iterations)
assert.Equal(t, "a", contents[0])
assert.Equal(t, "b", contents[1])
assert.Equal(t, "l", contents[11])
assert.Equal(t, "m", contents[12])
}