Merge pull request #11793 from thalesmg/test-rule-engine-foreach-m-20231019
test(rule_engine): add test to exemplify `foreach` with json array payload
This commit is contained in:
commit
d392c9989f
|
@ -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 =
|
||||
|
|
Loading…
Reference in New Issue