-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
visit_test.go
87 lines (64 loc) · 2.44 KB
/
visit_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
package clover
import (
"testing"
"github.com/ostafen/clover/v2/index"
q "github.com/ostafen/clover/v2/query"
"github.com/stretchr/testify/require"
)
func TestFlattenNot1(t *testing.T) {
c := q.Field("myField").Gt(1).And(q.Field("myField").Lt(2)).Not()
c = c.Accept(&CriteriaNormalizeVisitor{}).(q.Criteria)
c = c.Accept(&NotFlattenVisitor{}).(q.Criteria)
binNode := c.(*q.BinaryCriteria)
require.Equal(t, binNode.OpType, q.LogicalOr)
require.IsType(t, binNode.C1, &q.UnaryCriteria{})
require.IsType(t, binNode.C2, &q.UnaryCriteria{})
require.Equal(t, binNode.C1.(*q.UnaryCriteria).OpType, q.LtEqOp)
require.Equal(t, binNode.C2.(*q.UnaryCriteria).OpType, q.GtEqOp)
}
func TestFlattenNot2(t *testing.T) {
c := q.Field("myField").Eq(10).Not()
c = c.Accept(&CriteriaNormalizeVisitor{}).(q.Criteria)
c = c.Accept(&NotFlattenVisitor{}).(q.Criteria)
binNode := c.(*q.BinaryCriteria)
require.Equal(t, binNode.OpType, q.LogicalOr)
require.IsType(t, binNode.C1, &q.UnaryCriteria{})
require.IsType(t, binNode.C2, &q.UnaryCriteria{})
c1 := binNode.C1.(*q.UnaryCriteria)
c2 := binNode.C2.(*q.UnaryCriteria)
require.Equal(t, c1.OpType, q.LtOp)
require.Equal(t, c2.OpType, q.GtOp)
}
func TestFlattenNot3(t *testing.T) {
c := q.Field("myField").GtEq(10).Or(q.Field("myField").LtEq(100)).Not()
c = c.Accept(&CriteriaNormalizeVisitor{}).(q.Criteria)
c = c.Accept(&NotFlattenVisitor{}).(q.Criteria)
binNode := c.(*q.BinaryCriteria)
require.Equal(t, binNode.OpType, q.LogicalAnd)
require.IsType(t, binNode.C1, &q.UnaryCriteria{})
require.IsType(t, binNode.C2, &q.UnaryCriteria{})
c1 := binNode.C1.(*q.UnaryCriteria)
c2 := binNode.C2.(*q.UnaryCriteria)
require.Equal(t, c1.OpType, q.LtOp)
require.Equal(t, c2.OpType, q.GtOp)
}
func TestSelectIndexes(t *testing.T) {
c := q.Field("a").Gt(1).And(q.Field("a").Lt(2)).Or(q.Field("b").Eq(100))
c = c.Accept(&CriteriaNormalizeVisitor{}).(q.Criteria)
c = c.Accept(&NotFlattenVisitor{}).(q.Criteria)
s := c.Accept(&IndexSelectVisitor{Fields: map[string]*index.Info{
"a": {Field: "a"},
}}).([]*index.Info)
require.Len(t, s, 0)
s = c.Accept(&IndexSelectVisitor{Fields: map[string]*index.Info{
"b": {Field: "b"},
}}).([]*index.Info)
require.Len(t, s, 0)
s = c.Accept(&IndexSelectVisitor{Fields: map[string]*index.Info{
"a": {Field: "a"},
"b": {Field: "b"},
}}).([]*index.Info)
require.Len(t, s, 2)
require.Equal(t, s[0], &index.Info{Field: "a"})
require.Equal(t, s[1], &index.Info{Field: "b"})
}