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

Relates to https://emqx.atlassian.net/browse/EMQX-11174
This commit is contained in:
Thales Macedo Garitezi 2023-10-19 18:19:42 -03:00
parent eda82caf65
commit 8c4beec6f0
1 changed files with 48 additions and 0 deletions

View File

@ -92,6 +92,7 @@ groups() ->
t_sqlparse_foreach_6, t_sqlparse_foreach_6,
t_sqlparse_foreach_7, t_sqlparse_foreach_7,
t_sqlparse_foreach_8, t_sqlparse_foreach_8,
t_sqlparse_foreach_9,
t_sqlparse_case_when_1, t_sqlparse_case_when_1,
t_sqlparse_case_when_2, t_sqlparse_case_when_2,
t_sqlparse_case_when_3, t_sqlparse_case_when_3,
@ -2451,6 +2452,53 @@ t_sqlparse_foreach_8(_Config) ->
|| SqlN <- [Sql3] || 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) -> t_sqlparse_case_when_1(_Config) ->
%% case-when-else clause %% case-when-else clause
Sql = Sql =