-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathparser_test.go
168 lines (164 loc) · 4.37 KB
/
parser_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
package predicate
import (
"fmt"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/influxdb"
influxtesting "github.com/influxdata/influxdb/testing"
"github.com/influxdata/influxql"
)
func TestParseNode(t *testing.T) {
cases := []struct {
str string
node Node
err error
}{
{
str: `abc=opq`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "opq"}},
},
{
str: `abc=opq and gender="male"`,
node: LogicalNode{Operator: LogicalAnd, Children: [2]Node{
TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "opq"}},
TagRuleNode{Tag: influxdb.Tag{Key: "gender", Value: "male"}},
}},
},
{
str: ` abc="opq" AND gender="male" AND temp=1123`,
node: LogicalNode{Operator: LogicalAnd, Children: [2]Node{
LogicalNode{Operator: LogicalAnd, Children: [2]Node{
TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "opq"}},
TagRuleNode{Tag: influxdb.Tag{Key: "gender", Value: "male"}},
}},
TagRuleNode{Tag: influxdb.Tag{Key: "temp", Value: "1123"}},
}},
},
{
str: ` abc="opq" Or gender="male" OR temp=1123`,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "the logical operator OR is not supported yet at position 11",
},
},
{
str: ` (t1="v1" and t2="v2") and (t3=v3 and (t4=v4 and t5=v5 and t6=v6))`,
node: LogicalNode{Operator: LogicalAnd, Children: [2]Node{
LogicalNode{Operator: LogicalAnd, Children: [2]Node{
TagRuleNode{Tag: influxdb.Tag{Key: "t1", Value: "v1"}},
TagRuleNode{Tag: influxdb.Tag{Key: "t2", Value: "v2"}},
}},
LogicalNode{Operator: LogicalAnd, Children: [2]Node{
TagRuleNode{Tag: influxdb.Tag{Key: "t3", Value: "v3"}},
LogicalNode{Operator: LogicalAnd, Children: [2]Node{
LogicalNode{Operator: LogicalAnd, Children: [2]Node{
TagRuleNode{Tag: influxdb.Tag{Key: "t4", Value: "v4"}},
TagRuleNode{Tag: influxdb.Tag{Key: "t5", Value: "v5"}},
}},
TagRuleNode{Tag: influxdb.Tag{Key: "t6", Value: "v6"}},
}},
}},
}},
},
{
str: ` (t1="v1" and t2="v2") and (`,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("extra ( seen"),
},
},
{
str: ` (t1="v1" and t2="v2"))`,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("extra ) seen"),
},
},
}
for _, c := range cases {
node, err := Parse(c.str)
influxtesting.ErrorsEqual(t, err, c.err)
if c.err == nil {
if diff := cmp.Diff(node, c.node); diff != "" {
t.Errorf("tag rule mismatch:\n %s", diff)
}
}
}
}
func TestParseTagRule(t *testing.T) {
cases := []struct {
str string
node TagRuleNode
err error
}{
{
str: ` abc = "opq"`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "opq"}},
},
{
str: `abc=0x1231`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "0x1231"}},
},
{
str: `abc=2d`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "2d"}},
},
{
str: `abc=-5i`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "-5i"}},
},
{
str: `abc= -1221`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "-1221"}},
},
{
str: ` abc != "opq"`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "opq"}, Operator: influxdb.NotEqual},
},
{
str: `abc=123`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "123"}, Operator: influxdb.Equal},
},
{
str: `abc=true`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "true"}, Operator: influxdb.Equal},
},
{
str: `abc=false`,
node: TagRuleNode{Tag: influxdb.Tag{Key: "abc", Value: "false"}, Operator: influxdb.Equal},
},
{
str: `abc!~/^payments\./`,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: `operator: "!~" at position: 3 is not supported yet`,
},
},
{
str: `abc=~/^payments\./`,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: `operator: "=~" at position: 3 is not supported yet`,
},
},
{
str: `abc>1000`,
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: `invalid operator ">" at position: 3`,
},
},
}
for _, c := range cases {
p := new(parser)
p.sc = influxql.NewScanner(strings.NewReader(c.str))
tr, err := p.parseTagRuleNode()
influxtesting.ErrorsEqual(t, err, c.err)
if c.err == nil {
if diff := cmp.Diff(tr, c.node); diff != "" {
t.Errorf("tag rule mismatch:\n %s", diff)
}
}
}
}