funcs_misc_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2022 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. package function
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  19. "github.com/lf-edge/ekuiper/internal/topo/state"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. "reflect"
  23. "testing"
  24. )
  25. func TestChangedColValidation(t *testing.T) {
  26. f, ok := builtins["changed_col"]
  27. if !ok {
  28. t.Fatal("builtin not found")
  29. }
  30. var tests = []struct {
  31. args []ast.Expr
  32. err error
  33. }{
  34. {
  35. args: []ast.Expr{
  36. &ast.StringLiteral{Val: "foo"},
  37. },
  38. err: fmt.Errorf("Expect 2 arguments but found 1."),
  39. }, {
  40. args: []ast.Expr{
  41. &ast.StringLiteral{Val: "foo"},
  42. &ast.StringLiteral{Val: "bar"},
  43. },
  44. err: fmt.Errorf("Expect boolean type for parameter 1"),
  45. }, {
  46. args: []ast.Expr{
  47. &ast.StringLiteral{Val: "foo"},
  48. &ast.StringLiteral{Val: "bar"},
  49. &ast.StringLiteral{Val: "baz"},
  50. },
  51. err: fmt.Errorf("Expect 2 arguments but found 3."),
  52. }, {
  53. args: []ast.Expr{
  54. &ast.BooleanLiteral{Val: true},
  55. &ast.StringLiteral{Val: "baz"},
  56. },
  57. },
  58. }
  59. for i, tt := range tests {
  60. err := f.val(nil, tt.args)
  61. if !reflect.DeepEqual(err, tt.err) {
  62. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, err, tt.err)
  63. }
  64. }
  65. }
  66. func TestChangedColExec(t *testing.T) {
  67. f, ok := builtins["changed_col"]
  68. if !ok {
  69. t.Fatal("builtin not found")
  70. }
  71. contextLogger := conf.Log.WithField("rule", "testExec")
  72. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  73. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  74. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  75. var tests = []struct {
  76. args []interface{}
  77. result interface{}
  78. }{
  79. { // 0
  80. args: []interface{}{
  81. "foo",
  82. "bar",
  83. },
  84. result: fmt.Errorf("first arg is not a bool but got foo"),
  85. }, { // 1
  86. args: []interface{}{
  87. true,
  88. "bar",
  89. },
  90. result: "bar",
  91. }, { // 2
  92. args: []interface{}{
  93. true,
  94. "bar",
  95. },
  96. result: nil,
  97. }, { // 3
  98. args: []interface{}{
  99. true,
  100. "baz",
  101. },
  102. result: "baz",
  103. }, { // 4
  104. args: []interface{}{
  105. false,
  106. nil,
  107. },
  108. result: nil,
  109. }, { // 5
  110. args: []interface{}{
  111. false,
  112. "baz",
  113. },
  114. result: "baz",
  115. }, { // 6
  116. args: []interface{}{
  117. true,
  118. "foo",
  119. },
  120. result: "foo",
  121. },
  122. }
  123. for i, tt := range tests {
  124. result, _ := f.exec(fctx, tt.args)
  125. if !reflect.DeepEqual(result, tt.result) {
  126. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  127. }
  128. }
  129. }
  130. func TestToMap(t *testing.T) {
  131. f, ok := builtins["object_construct"]
  132. if !ok {
  133. t.Fatal("builtin not found")
  134. }
  135. contextLogger := conf.Log.WithField("rule", "testExec")
  136. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  137. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  138. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  139. var tests = []struct {
  140. args []interface{}
  141. result interface{}
  142. }{
  143. { // 0
  144. args: []interface{}{
  145. "foo",
  146. "bar",
  147. },
  148. result: map[string]interface{}{
  149. "foo": "bar",
  150. },
  151. }, { // 1
  152. args: []interface{}{
  153. true,
  154. "bar",
  155. },
  156. result: fmt.Errorf("key true is not a string"),
  157. }, { // 2
  158. args: []interface{}{
  159. "key1",
  160. "bar",
  161. "key2",
  162. "foo",
  163. },
  164. result: map[string]interface{}{
  165. "key1": "bar",
  166. "key2": "foo",
  167. },
  168. },
  169. }
  170. for i, tt := range tests {
  171. result, _ := f.exec(fctx, tt.args)
  172. if !reflect.DeepEqual(result, tt.result) {
  173. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  174. }
  175. }
  176. }