forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_test.go
170 lines (151 loc) · 4.26 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
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
package kapacitor_test
import (
"fmt"
"testing"
"time"
"github.com/influxdata/kapacitor"
)
func TestQuery_Clone(t *testing.T) {
testCases := []string{
"SELECT usage FROM telegraf.autogen.cpu",
"SELECT mean(usage) FROM telegraf.autogen.cpu WHERE host = 'serverA'",
"SELECT mean(usage) FROM telegraf.autogen.cpu WHERE host = 'serverA' AND dc = 'slc'",
"SELECT mean(usage) FROM telegraf.autogen.cpu WHERE host = 'serverA' AND dc = 'slc' OR product = 'login'",
"SELECT mean(usage) FROM telegraf.autogen.cpu WHERE host = 'serverA' AND (dc = 'slc' OR product = 'login')",
"SELECT * from (SELECT usage FROM telegraf.autogen.cpu)",
"SELECT * INTO telegraf.autogen.cpu FROM (SELECT usage FROM telegraf.autogen.cpu)",
}
equal := func(q0, q1 *kapacitor.Query) error {
if got, exp := q0.String(), q1.String(); got != exp {
return fmt.Errorf("unequal query string: got %s exp %s", got, exp)
}
if got, exp := q0.StartTime(), q1.StartTime(); got != exp {
return fmt.Errorf("unequal query start time: got %v exp %v", got, exp)
}
if got, exp := q0.StopTime(), q1.StopTime(); got != exp {
return fmt.Errorf("unequal query stop time: got %v exp %v", got, exp)
}
if got, exp := q0.IsGroupedByTime(), q1.IsGroupedByTime(); got != exp {
return fmt.Errorf("unequal query IsGroupedByTime: got %v exp %v", got, exp)
}
return nil
}
for _, query := range testCases {
q, err := kapacitor.NewQuery(query)
if err != nil {
t.Fatal(err)
}
clone, err := q.Clone()
if err != nil {
t.Fatal(err)
}
if err := equal(clone, q); err != nil {
t.Error(err)
}
// Modify original start time
start := time.Date(1975, 1, 1, 0, 0, 0, 0, time.UTC)
q.SetStartTime(start)
if err := equal(clone, q); err == nil {
t.Errorf("equal after modification: got %v", clone)
}
// Modify clone in the same way
clone.SetStartTime(start)
if err := equal(clone, q); err != nil {
t.Error(err)
}
// Re-clone
clone, err = q.Clone()
if err != nil {
t.Fatal(err)
}
if err := equal(clone, q); err != nil {
t.Error(err)
}
// Modify original stop time
stop := time.Date(1975, 1, 2, 0, 0, 0, 0, time.UTC)
q.SetStopTime(stop)
if err := equal(clone, q); err == nil {
t.Errorf("equal after modification: got %v", clone)
}
// Modify clone in the same way
clone.SetStopTime(stop)
if err := equal(clone, q); err != nil {
t.Error(err)
}
// Re-clone
clone, err = q.Clone()
if err != nil {
t.Fatal(err)
}
if err := equal(clone, q); err != nil {
t.Error(err)
}
// Set dimensions
q.Dimensions([]interface{}{time.Hour})
if err := equal(clone, q); err == nil {
t.Errorf("equal after modification: got %v", clone)
}
// Set dimesions on the clone in the same way
clone.Dimensions([]interface{}{time.Hour})
if err := equal(clone, q); err != nil {
t.Error(err)
}
// Re-clone
clone, err = q.Clone()
if err != nil {
t.Fatal(err)
}
if err := equal(clone, q); err != nil {
t.Error(err)
}
// Set group align and dimensions
q.AlignGroup()
q.Dimensions([]interface{}{kapacitor.TimeDimension{
Length: time.Minute,
Offset: time.Second,
}})
if err := equal(clone, q); err == nil {
t.Errorf("equal after modification: got %v", clone)
return
}
// Set group align and dimesions on the clone in the same way
clone.AlignGroup()
clone.Dimensions([]interface{}{kapacitor.TimeDimension{
Length: time.Minute,
Offset: time.Second,
}})
if err := equal(clone, q); err != nil {
t.Error(err)
}
// Re-clone
clone, err = q.Clone()
if err != nil {
t.Fatal(err)
}
if err := equal(clone, q); err != nil {
t.Error(err)
}
}
}
func TestQuery_IsGroupedByTime(t *testing.T) {
q, err := kapacitor.NewQuery("SELECT usage FROM telegraf.autogen.cpu")
if err != nil {
t.Fatal(err)
}
q.Dimensions([]interface{}{time.Hour})
if !q.IsGroupedByTime() {
t.Error("expected query to be grouped by time")
}
q, err = kapacitor.NewQuery("SELECT usage FROM telegraf.autogen.cpu")
if err != nil {
t.Fatal(err)
}
q.Dimensions([]interface{}{kapacitor.TimeDimension{Length: time.Hour, Offset: time.Minute}})
if !q.IsGroupedByTime() {
t.Error("expected query to be grouped by time")
}
q.Dimensions([]interface{}{"host"})
if q.IsGroupedByTime() {
t.Error("expected query to not be grouped by time")
}
}