-
Notifications
You must be signed in to change notification settings - Fork 4
/
query_test.go
112 lines (91 loc) · 2.52 KB
/
query_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
package charlatan
import (
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type dummyPerson struct {
name string
age int
}
func (d *dummyPerson) Find(f *Field) (*Const, error) {
switch f.Name() {
case "name":
return StringConst(d.name), nil
case "age":
return IntConst(int64(d.age)), nil
default:
return nil, errors.New("wrong field")
}
}
var _ Record = &dummyPerson{}
func TestQueryInRange(t *testing.T) {
q := &Query{
fields: []*Field{
&Field{"name"},
},
expression: &rangeTestOperation{
test: Field{"age"},
min: IntConst(10),
max: IntConst(20),
},
}
m, err := q.Evaluate(&dummyPerson{name: "A", age: 15})
assert.Nil(t, err)
assert.True(t, m)
m, err = q.Evaluate(&dummyPerson{name: "A", age: 25})
assert.Nil(t, err)
assert.False(t, m)
vals, err := q.FieldsValues(&dummyPerson{name: "A", age: 12})
assert.Nil(t, err)
assert.Equal(t, 1, len(vals))
assert.Equal(t, "A", vals[0].AsString())
}
func TestQueryFromStringInRange(t *testing.T) {
q, err := QueryFromString("SELECT a FROM b WHERE a BETWEEN 1 AND 2")
require.Nil(t, err)
assert.NotNil(t, q)
assert.NotNil(t, q.fields)
assert.NotNil(t, q.expression)
assert.Equal(t, "b", q.from)
assert.Equal(t, int64(0), q.startingAt)
assert.Equal(t, 1, len(q.fields))
assert.NotNil(t, q.fields[0])
assert.Equal(t, "a", q.fields[0].name)
expr, ok := q.expression.(*rangeTestOperation)
require.True(t, ok, "%v should be a range test", q.expression)
assert.NotNil(t, expr.test)
assert.NotNil(t, expr.min)
assert.NotNil(t, expr.max)
f, ok := expr.test.(*Field)
require.True(t, ok)
assert.Equal(t, "a", f.name)
min, ok := expr.min.(*Const)
require.True(t, ok, "%v should be a const (not %s)",
expr.min, reflect.TypeOf(expr.min))
assert.True(t, min.IsNumeric())
assert.Equal(t, int64(1), min.AsInt())
max, ok := expr.max.(*Const)
require.True(t, ok, "%v should be a const (not %s)",
expr.min, reflect.TypeOf(expr.max))
assert.True(t, max.IsNumeric())
assert.Equal(t, int64(2), max.AsInt())
}
func TestQueryHasLimit(t *testing.T) {
q := NewQuery("foo")
assert.False(t, q.HasLimit())
assert.Equal(t, int64(0), q.Limit())
q.setLimit(42)
assert.True(t, q.HasLimit())
assert.Equal(t, int64(42), q.Limit())
}
func TestQueryFromStringLimitOffset(t *testing.T) {
q, err := QueryFromString("SELECT a FROM b WHERE a LIMIT 40, 100")
require.Nil(t, err)
assert.NotNil(t, q)
assert.Equal(t, int64(40), q.startingAt)
assert.True(t, q.HasLimit())
assert.Equal(t, int64(100), q.Limit())
}