funcs_misc_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2022-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. 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 TestToMap(t *testing.T) {
  26. f, ok := builtins["object_construct"]
  27. if !ok {
  28. t.Fatal("builtin not found")
  29. }
  30. contextLogger := conf.Log.WithField("rule", "testExec")
  31. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  32. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  33. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  34. var tests = []struct {
  35. args []interface{}
  36. result interface{}
  37. }{
  38. { // 0
  39. args: []interface{}{
  40. "foo",
  41. "bar",
  42. },
  43. result: map[string]interface{}{
  44. "foo": "bar",
  45. },
  46. }, { // 1
  47. args: []interface{}{
  48. true,
  49. "bar",
  50. },
  51. result: fmt.Errorf("key true is not a string"),
  52. }, { // 2
  53. args: []interface{}{
  54. "key1",
  55. "bar",
  56. "key2",
  57. "foo",
  58. },
  59. result: map[string]interface{}{
  60. "key1": "bar",
  61. "key2": "foo",
  62. },
  63. },
  64. }
  65. for i, tt := range tests {
  66. result, _ := f.exec(fctx, tt.args)
  67. if !reflect.DeepEqual(result, tt.result) {
  68. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  69. }
  70. }
  71. }
  72. func TestCoalesceExec(t *testing.T) {
  73. f, ok := builtins["coalesce"]
  74. if !ok {
  75. t.Fatal("builtin not found")
  76. }
  77. contextLogger := conf.Log.WithField("rule", "testExec")
  78. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  79. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  80. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  81. var tests = []struct {
  82. args []interface{}
  83. result interface{}
  84. }{
  85. { // 1
  86. args: []interface{}{
  87. "foo",
  88. "bar",
  89. "2",
  90. },
  91. result: "foo",
  92. },
  93. { // 2
  94. args: []interface{}{
  95. nil,
  96. "dd",
  97. "1",
  98. },
  99. result: "dd",
  100. },
  101. { // 3
  102. args: []interface{}{
  103. "bar",
  104. nil,
  105. "1",
  106. },
  107. result: "bar",
  108. },
  109. { // 4
  110. args: []interface{}{
  111. nil,
  112. nil,
  113. "2",
  114. },
  115. result: "2",
  116. },
  117. { // 4
  118. args: []interface{}{
  119. nil,
  120. nil,
  121. nil,
  122. },
  123. result: nil,
  124. },
  125. }
  126. for i, tt := range tests {
  127. result, _ := f.exec(fctx, tt.args)
  128. if !reflect.DeepEqual(result, tt.result) {
  129. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  130. }
  131. }
  132. }
  133. func TestToJson(t *testing.T) {
  134. f, ok := builtins["to_json"]
  135. if !ok {
  136. t.Fatal("builtin not found")
  137. }
  138. contextLogger := conf.Log.WithField("rule", "testExec")
  139. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  140. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  141. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  142. var tests = []struct {
  143. args []interface{}
  144. result interface{}
  145. }{
  146. { // 0
  147. args: []interface{}{
  148. "foo",
  149. },
  150. result: `"foo"`,
  151. }, { // 1
  152. args: []interface{}{
  153. nil,
  154. },
  155. result: "null",
  156. }, { // 2
  157. args: []interface{}{
  158. map[string]interface{}{
  159. "key1": "bar",
  160. "key2": "foo",
  161. },
  162. },
  163. result: `{"key1":"bar","key2":"foo"}`,
  164. },
  165. }
  166. for i, tt := range tests {
  167. result, _ := f.exec(fctx, tt.args)
  168. if !reflect.DeepEqual(result, tt.result) {
  169. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  170. }
  171. }
  172. }
  173. func TestFromJson(t *testing.T) {
  174. f, ok := builtins["parse_json"]
  175. if !ok {
  176. t.Fatal("builtin not found")
  177. }
  178. contextLogger := conf.Log.WithField("rule", "testExec")
  179. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  180. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  181. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  182. var tests = []struct {
  183. args []interface{}
  184. result interface{}
  185. }{
  186. { // 0
  187. args: []interface{}{
  188. `"foo"`,
  189. },
  190. result: "foo",
  191. }, { // 1
  192. args: []interface{}{
  193. "null",
  194. },
  195. result: nil,
  196. }, { // 2
  197. args: []interface{}{
  198. `{"key1":"bar","key2":"foo"}`,
  199. },
  200. result: map[string]interface{}{
  201. "key1": "bar",
  202. "key2": "foo",
  203. },
  204. }, { // 3
  205. args: []interface{}{
  206. "key1",
  207. },
  208. result: fmt.Errorf("fail to parse json: invalid character 'k' looking for beginning of value"),
  209. }, { // 4
  210. args: []interface{}{
  211. `[{"key1":"bar","key2":"foo"}]`,
  212. },
  213. result: []interface{}{
  214. map[string]interface{}{
  215. "key1": "bar",
  216. "key2": "foo",
  217. },
  218. },
  219. },
  220. }
  221. for i, tt := range tests {
  222. result, _ := f.exec(fctx, tt.args)
  223. if !reflect.DeepEqual(result, tt.result) {
  224. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  225. }
  226. }
  227. }
  228. func TestDelay(t *testing.T) {
  229. f, ok := builtins["delay"]
  230. if !ok {
  231. t.Fatal("builtin not found")
  232. }
  233. contextLogger := conf.Log.WithField("rule", "testExec")
  234. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  235. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  236. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  237. err := f.val(fctx, []ast.Expr{&ast.StringLiteral{Val: "abc"}})
  238. if err == nil {
  239. t.Fatal("expect error")
  240. }
  241. err = f.val(fctx, []ast.Expr{&ast.StringLiteral{Val: "1s"}, &ast.StringLiteral{Val: "1s"}})
  242. if err == nil {
  243. t.Fatal("expect error")
  244. }
  245. err = f.val(fctx, []ast.Expr{&ast.IntegerLiteral{Val: 1000}, &ast.StringLiteral{Val: "1s"}})
  246. if err != nil {
  247. t.Fatal("expect no error")
  248. }
  249. var tests = []struct {
  250. args []interface{}
  251. result interface{}
  252. }{
  253. { // 0
  254. args: []interface{}{
  255. 10,
  256. "bar",
  257. },
  258. result: "bar",
  259. }, { // 1
  260. args: []interface{}{
  261. "bar",
  262. "bar",
  263. },
  264. result: fmt.Errorf("cannot convert string(bar) to int"),
  265. },
  266. }
  267. for i, tt := range tests {
  268. result, _ := f.exec(fctx, tt.args)
  269. if !reflect.DeepEqual(result, tt.result) {
  270. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  271. }
  272. }
  273. }