funcs_misc_test.go 5.2 KB

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