Просмотр исходного кода

test(rule_engine): add test to exemplify `foreach` with json array payload

Relates to https://emqx.atlassian.net/browse/EMQX-11174
Thales Macedo Garitezi 2 лет назад
Родитель
Сommit
8c4beec6f0
1 измененных файлов с 48 добавлено и 0 удалено
  1. 48 0
      apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl

+ 48 - 0
apps/emqx_rule_engine/test/emqx_rule_engine_SUITE.erl

@@ -92,6 +92,7 @@ groups() ->
             t_sqlparse_foreach_6,
             t_sqlparse_foreach_7,
             t_sqlparse_foreach_8,
+            t_sqlparse_foreach_9,
             t_sqlparse_case_when_1,
             t_sqlparse_case_when_2,
             t_sqlparse_case_when_3,
@@ -2451,6 +2452,53 @@ t_sqlparse_foreach_8(_Config) ->
      || SqlN <- [Sql3]
     ].
 
+t_sqlparse_foreach_9(_Config) ->
+    Sql1 =
+        "foreach json_decode(payload) as p "
+        "do p.ts as ts "
+        "from \"t/#\" ",
+    Context = #{
+        payload =>
+            emqx_utils_json:encode(
+                [
+                    #{
+                        <<"ts">> => 1451649600512,
+                        <<"values">> =>
+                            #{
+                                <<"respiratoryrate">> => 20,
+                                <<"heartrate">> => 130,
+                                <<"systolic">> => 50
+                            }
+                    }
+                ]
+            ),
+        topic => <<"t/a">>
+    },
+    ?assertMatch(
+        {ok, [#{<<"ts">> := 1451649600512}]},
+        emqx_rule_sqltester:test(
+            #{
+                sql => Sql1,
+                context => Context
+            }
+        )
+    ),
+    %% doesn't work if we don't decode it first
+    Sql2 =
+        "foreach payload as p "
+        "do p.ts as ts "
+        "from \"t/#\" ",
+    ?assertMatch(
+        {ok, []},
+        emqx_rule_sqltester:test(
+            #{
+                sql => Sql2,
+                context => Context
+            }
+        )
+    ),
+    ok.
+
 t_sqlparse_case_when_1(_Config) ->
     %% case-when-else clause
     Sql =