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:
Thales Macedo Garitezi 2023-10-23 13:43:33 -03:00 committed by GitHub
commit d392c9989f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 0 deletions

View File

@ -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 =