script_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build script
  15. package operator
  16. import (
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/internal/topo/context"
  22. "github.com/lf-edge/ekuiper/internal/xsql"
  23. )
  24. func TestScriptOp(t *testing.T) {
  25. tests := []struct {
  26. script string
  27. isAgg bool
  28. data interface{}
  29. result interface{}
  30. }{
  31. {
  32. script: `function exec(msg, meta) {msg.value = msg.value + 1; return msg}`,
  33. data: &xsql.Tuple{
  34. Emitter: "tbl",
  35. Message: xsql.Message{
  36. "value": int64(6),
  37. },
  38. },
  39. result: &xsql.Tuple{
  40. Emitter: "tbl",
  41. Message: xsql.Message{
  42. "value": int64(7),
  43. },
  44. },
  45. },
  46. {
  47. script: `function exec(msgs) {
  48. for (let i = 0; i < msgs.length; i++) {
  49. msgs[i].value = msgs[i].value + 1;
  50. }
  51. return msgs
  52. }`,
  53. data: &xsql.WindowTuples{
  54. Content: []xsql.TupleRow{
  55. &xsql.Tuple{
  56. Emitter: "tbl",
  57. Message: xsql.Message{
  58. "value": int64(6),
  59. },
  60. },
  61. &xsql.Tuple{
  62. Emitter: "tbl",
  63. Message: xsql.Message{
  64. "value": 8.5,
  65. },
  66. },
  67. &xsql.Tuple{
  68. Emitter: "tbl",
  69. Message: xsql.Message{
  70. "value": 10.2,
  71. },
  72. },
  73. },
  74. },
  75. result: &xsql.WindowTuples{
  76. Content: []xsql.TupleRow{
  77. &xsql.Tuple{
  78. Emitter: "",
  79. Message: xsql.Message{
  80. "value": int64(7),
  81. },
  82. },
  83. &xsql.Tuple{
  84. Emitter: "",
  85. Message: xsql.Message{
  86. "value": 9.5,
  87. },
  88. },
  89. &xsql.Tuple{
  90. Emitter: "",
  91. Message: xsql.Message{
  92. "value": 11.2,
  93. },
  94. },
  95. },
  96. },
  97. },
  98. {
  99. script: `function exec(msgs) {
  100. agg = {value:0}
  101. for (let i = 0; i < msgs.length; i++) {
  102. agg.value = agg.value + msgs[i].value;
  103. }
  104. return agg
  105. }`,
  106. isAgg: true,
  107. data: &xsql.WindowTuples{
  108. Content: []xsql.TupleRow{
  109. &xsql.Tuple{
  110. Emitter: "tbl",
  111. Message: xsql.Message{
  112. "value": int64(6),
  113. },
  114. },
  115. &xsql.Tuple{
  116. Emitter: "tbl",
  117. Message: xsql.Message{
  118. "value": 8.5,
  119. },
  120. },
  121. &xsql.Tuple{
  122. Emitter: "tbl",
  123. Message: xsql.Message{
  124. "value": 10.2,
  125. },
  126. },
  127. },
  128. },
  129. result: &xsql.Tuple{
  130. Message: xsql.Message{
  131. "value": 24.7,
  132. },
  133. },
  134. },
  135. }
  136. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  137. contextLogger := conf.Log.WithField("rule", "TestScriptOp_Apply")
  138. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  139. for i, tt := range tests {
  140. pp, err := NewScriptOp(tt.script, tt.isAgg)
  141. if err != nil {
  142. t.Errorf("NewScriptOp error: %v", err)
  143. continue
  144. }
  145. result := pp.Apply(ctx, tt.data, nil, nil)
  146. if !reflect.DeepEqual(tt.result, result) {
  147. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.script, tt.result, result)
  148. }
  149. }
  150. }