funcs_misc_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. }
  177. func TestHadChangedValidation(t *testing.T) {
  178. f, ok := builtins["had_changed"]
  179. if !ok {
  180. t.Fatal("builtin not found")
  181. }
  182. var tests = []struct {
  183. args []ast.Expr
  184. err error
  185. }{
  186. {
  187. args: []ast.Expr{
  188. &ast.StringLiteral{Val: "foo"},
  189. },
  190. err: fmt.Errorf("expect more than one arg but got 1"),
  191. }, {
  192. args: []ast.Expr{
  193. &ast.StringLiteral{Val: "foo"},
  194. &ast.StringLiteral{Val: "bar"},
  195. &ast.StringLiteral{Val: "baz"},
  196. },
  197. err: fmt.Errorf("Expect bool type for parameter 1"),
  198. }, {
  199. args: []ast.Expr{
  200. &ast.IntegerLiteral{Val: 20},
  201. &ast.BooleanLiteral{Val: true},
  202. &ast.StringLiteral{Val: "baz"},
  203. },
  204. err: fmt.Errorf("Expect bool type for parameter 1"),
  205. }, {
  206. args: []ast.Expr{
  207. &ast.FieldRef{
  208. StreamName: "demo",
  209. Name: "a",
  210. AliasRef: nil,
  211. },
  212. &ast.BooleanLiteral{Val: true},
  213. &ast.StringLiteral{Val: "baz"},
  214. },
  215. err: nil,
  216. },
  217. }
  218. for i, tt := range tests {
  219. err := f.val(nil, tt.args)
  220. if !reflect.DeepEqual(err, tt.err) {
  221. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, err, tt.err)
  222. }
  223. }
  224. }
  225. func TestHadChangedExec(t *testing.T) {
  226. f, ok := builtins["had_changed"]
  227. if !ok {
  228. t.Fatal("builtin not found")
  229. }
  230. contextLogger := conf.Log.WithField("rule", "testExec")
  231. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  232. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  233. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 1)
  234. var tests = []struct {
  235. args []interface{}
  236. result interface{}
  237. }{
  238. { // 0
  239. args: []interface{}{
  240. "foo",
  241. "bar",
  242. "baz",
  243. },
  244. result: fmt.Errorf("first arg is not a bool but got foo"),
  245. }, { // 1
  246. args: []interface{}{
  247. "foo",
  248. "bar",
  249. },
  250. result: fmt.Errorf("first arg is not a bool but got foo"),
  251. }, { // 2
  252. args: []interface{}{
  253. true,
  254. "bar",
  255. 20,
  256. },
  257. result: true,
  258. }, { // 3
  259. args: []interface{}{
  260. true,
  261. "baz",
  262. 44,
  263. },
  264. result: true,
  265. }, { // 4
  266. args: []interface{}{
  267. true,
  268. "baz",
  269. 44,
  270. },
  271. result: false,
  272. }, { // 5
  273. args: []interface{}{
  274. true,
  275. "foo",
  276. 44,
  277. },
  278. result: true,
  279. }, { // 6
  280. args: []interface{}{
  281. true,
  282. "foo",
  283. nil,
  284. },
  285. result: false,
  286. }, { // 7
  287. args: []interface{}{
  288. true,
  289. "foo",
  290. 44,
  291. },
  292. result: false,
  293. }, { // 8
  294. args: []interface{}{
  295. true,
  296. "baz",
  297. 44,
  298. },
  299. result: true,
  300. },
  301. }
  302. for i, tt := range tests {
  303. result, _ := f.exec(fctx, tt.args)
  304. if !reflect.DeepEqual(result, tt.result) {
  305. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  306. }
  307. }
  308. }
  309. func TestHadChangedExecAllowNull(t *testing.T) {
  310. f, ok := builtins["had_changed"]
  311. if !ok {
  312. t.Fatal("builtin not found")
  313. }
  314. contextLogger := conf.Log.WithField("rule", "testExec")
  315. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  316. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  317. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 1)
  318. var tests = []struct {
  319. args []interface{}
  320. result interface{}
  321. }{
  322. { // 0
  323. args: []interface{}{
  324. "foo",
  325. "bar",
  326. "baz",
  327. },
  328. result: fmt.Errorf("first arg is not a bool but got foo"),
  329. }, { // 1
  330. args: []interface{}{
  331. "foo",
  332. "bar",
  333. },
  334. result: fmt.Errorf("first arg is not a bool but got foo"),
  335. }, { // 2
  336. args: []interface{}{
  337. false,
  338. "bar",
  339. 20,
  340. },
  341. result: true,
  342. }, { // 3
  343. args: []interface{}{
  344. false,
  345. "baz",
  346. nil,
  347. },
  348. result: true,
  349. }, { // 4
  350. args: []interface{}{
  351. false,
  352. "baz",
  353. 44,
  354. },
  355. result: true,
  356. }, { // 5
  357. args: []interface{}{
  358. false,
  359. nil,
  360. 44,
  361. },
  362. result: true,
  363. }, { // 6
  364. args: []interface{}{
  365. false,
  366. "baz",
  367. 44,
  368. },
  369. result: true,
  370. }, { // 7
  371. args: []interface{}{
  372. false,
  373. "baz",
  374. 44,
  375. },
  376. result: false,
  377. }, { // 8
  378. args: []interface{}{
  379. false,
  380. nil,
  381. nil,
  382. },
  383. result: true,
  384. }, { // 9
  385. args: []interface{}{
  386. false,
  387. "baz",
  388. 44,
  389. },
  390. result: true,
  391. },
  392. }
  393. for i, tt := range tests {
  394. result, _ := f.exec(fctx, tt.args)
  395. if !reflect.DeepEqual(result, tt.result) {
  396. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  397. }
  398. }
  399. }