-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstreamsql_nested_field_test.go
More file actions
486 lines (408 loc) · 12.9 KB
/
streamsql_nested_field_test.go
File metadata and controls
486 lines (408 loc) · 12.9 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package streamsql
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestComprehensiveNestedFieldAccess 全面测试嵌套字段访问功能
func TestComprehensiveNestedFieldAccess(t *testing.T) {
t.Run("多层嵌套字段访问", func(t *testing.T) {
streamsql := New()
defer func() {
if streamsql != nil {
streamsql.Stop()
}
}()
// 测试多层嵌套字段访问
var rsql = "SELECT device.info.name as device_name, device.location.building as building, sensor.data.temperature as temp FROM stream"
err := streamsql.Execute(rsql)
assert.Nil(t, err, "多层嵌套字段SQL应该能够执行")
require.NoError(t, err, "多层嵌套字段访问不应该出错")
strm := streamsql.stream
// 创建结果接收通道
resultChan := make(chan interface{}, 10)
// 添加结果接收器
strm.AddSink(func(result []map[string]interface{}) {
resultChan <- result
})
// 添加带多层嵌套字段的测试数据
testData := map[string]interface{}{
"device": map[string]interface{}{
"info": map[string]interface{}{
"name": "温度传感器001",
"type": "temperature",
"status": "active",
},
"location": map[string]interface{}{
"building": "A栋",
"floor": "3F",
"room": "301",
},
},
"sensor": map[string]interface{}{
"data": map[string]interface{}{
"temperature": 28.5,
"humidity": 65.0,
"pressure": 1013.25,
},
"status": "online",
},
}
// 发送数据
strm.Emit(testData)
// 等待结果
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
select {
case result := <-resultChan:
// 验证结果
resultSlice, ok := result.([]map[string]interface{})
require.True(t, ok, "结果应该是[]map[string]interface{}类型")
require.Len(t, resultSlice, 1, "应该只有一条结果")
item := resultSlice[0]
// 检查各个嵌套字段的提取情况
deviceName, deviceNameExists := item["device_name"]
assert.True(t, deviceNameExists, "device.info.name字段应该存在")
assert.Equal(t, "温度传感器001", deviceName, "device.info.name应该被正确提取")
building, buildingExists := item["building"]
assert.True(t, buildingExists, "device.location.building字段应该存在")
assert.Equal(t, "A栋", building, "device.location.building应该被正确提取")
temp, tempExists := item["temp"]
assert.True(t, tempExists, "sensor.data.temperature字段应该存在")
assert.Equal(t, 28.5, temp, "sensor.data.temperature应该被正确提取")
case <-ctx.Done():
t.Fatal("多层嵌套测试超时")
}
})
t.Run("嵌套字段聚合查询", func(t *testing.T) {
streamsql := New()
defer func() {
if streamsql != nil {
streamsql.Stop()
}
}()
// 测试聚合查询中的嵌套字段
var rsql = "SELECT device.type, AVG(sensor.temperature) as avg_temp, COUNT(*) as cnt FROM stream GROUP BY device.type, TumblingWindow('1s')"
err := streamsql.Execute(rsql)
assert.Nil(t, err, "嵌套字段聚合SQL应该能够执行")
require.NoError(t, err, "嵌套字段聚合查询不应该出错")
strm := streamsql.stream
// 创建结果接收通道
resultChan := make(chan interface{}, 10)
// 添加结果回调
strm.AddSink(func(result []map[string]interface{}) {
resultChan <- result
})
// 添加测试数据
testData := []map[string]interface{}{
{
"device": map[string]interface{}{
"type": "temperature",
"id": "sensor001",
},
"sensor": map[string]interface{}{
"temperature": 25.0,
},
},
{
"device": map[string]interface{}{
"type": "temperature",
"id": "sensor002",
},
"sensor": map[string]interface{}{
"temperature": 35.0,
},
},
{
"device": map[string]interface{}{
"type": "humidity",
"id": "sensor003",
},
"sensor": map[string]interface{}{
"temperature": 20.0,
},
},
}
// 添加数据
for _, data := range testData {
strm.Emit(data)
}
// 等待窗口触发
time.Sleep(1200 * time.Millisecond)
// 等待结果
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
select {
case result := <-resultChan:
// 验证聚合结果
resultSlice, ok := result.([]map[string]interface{})
require.True(t, ok, "结果应该是[]map[string]interface{}类型")
// 聚合查询可能返回空结果,这是正常的
if len(resultSlice) > 0 {
// 如果有结果,验证结果结构
for _, item := range resultSlice {
// 检查是否包含任何聚合字段
hasAnyField := false
if _, exists := item["type"]; exists {
hasAnyField = true
}
if _, exists := item["avg_temp"]; exists {
hasAnyField = true
}
if _, exists := item["cnt"]; exists {
hasAnyField = true
}
assert.True(t, hasAnyField, "聚合结果应该包含至少一个预期字段")
}
}
case <-ctx.Done():
t.Fatal("嵌套字段聚合查询测试超时")
}
})
t.Run("复杂嵌套字段WHERE条件", func(t *testing.T) {
streamsql := New()
defer func() {
if streamsql != nil {
streamsql.Stop()
}
}()
// 测试复杂的WHERE条件
var rsql = "SELECT * FROM stream WHERE device.info.status = 'active' AND sensor.data.temperature > 25 AND device.location.building = 'A栋'"
err := streamsql.Execute(rsql)
assert.Nil(t, err, "复杂嵌套字段WHERE SQL应该能够执行")
require.NoError(t, err, "复杂嵌套字段WHERE条件不应该出错")
strm := streamsql.stream
// 创建结果接收通道
resultChan := make(chan interface{}, 10)
// 添加结果接收器
strm.AddSink(func(result []map[string]interface{}) {
resultChan <- result
})
// 添加测试数据:一条满足所有条件,一条不满足
testData1 := map[string]interface{}{
"device": map[string]interface{}{
"info": map[string]interface{}{
"name": "传感器A",
"status": "active", // 满足条件
},
"location": map[string]interface{}{
"building": "A栋", // 满足条件
},
},
"sensor": map[string]interface{}{
"data": map[string]interface{}{
"temperature": 30.0, // 满足条件 > 25
},
},
}
testData2 := map[string]interface{}{
"device": map[string]interface{}{
"info": map[string]interface{}{
"name": "传感器B",
"status": "inactive", // 不满足条件
},
"location": map[string]interface{}{
"building": "B栋", // 不满足条件
},
},
"sensor": map[string]interface{}{
"data": map[string]interface{}{
"temperature": 20.0, // 不满足条件 <= 25
},
},
}
// 发送数据
strm.Emit(testData1)
strm.Emit(testData2)
// 等待结果
var results []interface{}
timeout := time.After(3 * time.Second)
done := false
for !done {
select {
case result := <-resultChan:
results = append(results, result)
if len(results) >= 1 {
done = true
}
case <-timeout:
done = true
}
}
// 验证结果
assert.Greater(t, len(results), 0, "复杂WHERE条件应该返回结果")
for _, result := range results {
resultSlice, ok := result.([]map[string]interface{})
require.True(t, ok, "结果应该是[]map[string]interface{}类型")
for _, item := range resultSlice {
// 验证通过过滤的数据确实满足所有条件
device, deviceOk := item["device"].(map[string]interface{})
assert.True(t, deviceOk, "device字段应该存在且为map类型")
info, infoOk := device["info"].(map[string]interface{})
assert.True(t, infoOk, "device.info字段应该存在且为map类型")
status, statusOk := info["status"].(string)
assert.True(t, statusOk, "device.info.status字段应该存在且为string类型")
assert.Equal(t, "active", status, "status应该是active")
location, locationOk := device["location"].(map[string]interface{})
assert.True(t, locationOk, "device.location字段应该存在且为map类型")
building, buildingOk := location["building"].(string)
assert.True(t, buildingOk, "device.location.building字段应该存在且为string类型")
assert.Equal(t, "A栋", building, "building应该是A栋")
sensor, sensorOk := item["sensor"].(map[string]interface{})
assert.True(t, sensorOk, "sensor字段应该存在且为map类型")
data, dataOk := sensor["data"].(map[string]interface{})
assert.True(t, dataOk, "sensor.data字段应该存在且为map类型")
temp, tempOk := data["temperature"].(float64)
assert.True(t, tempOk, "sensor.data.temperature字段应该存在且为float64类型")
assert.Greater(t, temp, 25.0, "temperature应该大于25")
}
}
})
}
// TestArrayFieldAccess 测试数组字段访问功能
func TestArrayFieldAccess(t *testing.T) {
t.Run("数组索引访问", func(t *testing.T) {
streamsql := New()
defer func() {
if streamsql != nil {
streamsql.Stop()
}
}()
// 测试数组索引访问
var rsql = "SELECT items[0].name as first_item_name, items[1].id as second_item_id, values[2] as third_value FROM stream"
err := streamsql.Execute(rsql)
assert.Nil(t, err, "数组索引访问SQL应该能够执行")
require.NoError(t, err, "数组索引访问不应该出错")
strm := streamsql.stream
resultChan := make(chan interface{}, 10)
strm.AddSink(func(result []map[string]interface{}) {
resultChan <- result
})
// 测试数据
testData := map[string]interface{}{
"items": []interface{}{
map[string]interface{}{"name": "item1", "id": 101},
map[string]interface{}{"name": "item2", "id": 102},
},
"values": []interface{}{10, 20, 30, 40},
}
strm.Emit(testData)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
select {
case result := <-resultChan:
resultSlice, ok := result.([]map[string]interface{})
require.True(t, ok)
require.Len(t, resultSlice, 1)
item := resultSlice[0]
// 验证 items[0].name
name, ok := item["first_item_name"]
assert.True(t, ok)
assert.Equal(t, "item1", name)
// 验证 items[1].id
id, ok := item["second_item_id"]
assert.True(t, ok)
assert.Equal(t, 102, id)
// 验证 values[2]
val, ok := item["third_value"]
assert.True(t, ok)
assert.Equal(t, 30, val)
case <-ctx.Done():
t.Fatal("测试超时")
}
})
t.Run("数组索引在WHERE条件中", func(t *testing.T) {
streamsql := New()
defer func() {
if streamsql != nil {
streamsql.Stop()
}
}()
var rsql = "SELECT * FROM stream WHERE tags[0] = 'urgent' AND scores[1] > 90"
err := streamsql.Execute(rsql)
require.NoError(t, err)
strm := streamsql.stream
resultChan := make(chan interface{}, 10)
strm.AddSink(func(result []map[string]interface{}) {
resultChan <- result
})
// 匹配的数据
matchData := map[string]interface{}{
"id": "match",
"tags": []interface{}{"urgent", "work"},
"scores": []interface{}{80, 95},
}
// 不匹配的数据
mismatchData := map[string]interface{}{
"id": "mismatch",
"tags": []interface{}{"normal", "home"},
"scores": []interface{}{80, 85},
}
strm.Emit(matchData)
strm.Emit(mismatchData)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
select {
case result := <-resultChan:
resultSlice, ok := result.([]map[string]interface{})
require.True(t, ok)
require.Len(t, resultSlice, 1)
assert.Equal(t, "match", resultSlice[0]["id"])
case <-ctx.Done():
t.Fatal("测试超时")
}
})
t.Run("嵌套数组聚合查询", func(t *testing.T) {
streamsql := New()
defer func() {
if streamsql != nil {
streamsql.Stop()
}
}()
// 测试聚合查询中的嵌套数组字段
var rsql = "SELECT device.type, AVG(sensors[0].temperature) as avg_temp FROM stream GROUP BY device.type, TumblingWindow('1s')"
err := streamsql.Execute(rsql)
assert.Nil(t, err, "嵌套数组聚合SQL应该能够执行")
strm := streamsql.stream
resultChan := make(chan interface{}, 10)
strm.AddSink(func(result []map[string]interface{}) {
resultChan <- result
})
testData := []map[string]interface{}{
{
"device": map[string]interface{}{"type": "temp_sensor"},
"sensors": []interface{}{
map[string]interface{}{"temperature": 20.0},
},
},
{
"device": map[string]interface{}{"type": "temp_sensor"},
"sensors": []interface{}{
map[string]interface{}{"temperature": 30.0},
},
},
}
for _, data := range testData {
strm.Emit(data)
}
time.Sleep(1200 * time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
select {
case result := <-resultChan:
resultSlice, ok := result.([]map[string]interface{})
require.True(t, ok)
// 聚合结果可能为空,取决于窗口触发时机
if len(resultSlice) > 0 {
item := resultSlice[0]
avgTemp, ok := item["avg_temp"].(float64)
assert.True(t, ok)
assert.Equal(t, 25.0, avgTemp)
}
case <-ctx.Done():
t.Fatal("测试超时")
}
})
}