Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage fixes #9

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
with:
otp-version: ${{matrix.otp}}
rebar3-version: ${{matrix.rebar3}}
- run: rebar3 do xref,eunit,coveralls send
- run: rebar3 do xref,eunit,dialyzer,coveralls send
3 changes: 2 additions & 1 deletion src/equery.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
{registered, []},
{applications, [
kernel,
stdlib
stdlib,
syntax_tools
]},
{env, []},
{maintainers, ["Yakov Kozlov"]},
Expand Down
4 changes: 1 addition & 3 deletions src/equery_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ wrap(F) ->

-spec field_name(atom()) -> iolist().
field_name(Atom) when is_atom(Atom) ->
wrap(atom_to_list(Atom));
field_name(Bin) when is_binary(Bin) ->
wrap(Bin).
wrap(atom_to_list(Atom)).

to_binary(Atom) when is_atom(Atom) ->
atom_to_binary(Atom, latin1);
Expand Down
50 changes: 32 additions & 18 deletions test/q_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,18 @@ q_from_query_test() ->
])}, Feilds).

q_compile_test() ->
WhereFun = q:compile(fun() ->
fun([#{name := Name, filter := F}]) ->
Name =:= <<"test2">> orelse F
end
end),
{Sql, Args, Feilds} = to_sql(
qsql:select(q:pipe(q:from(?MODULE), [
q:data(
fun([#{name := Name}=TD]) ->
[TD#{filter => Name =:= <<"test1">>}]
end),
q:where(
fun([#{name := Name, filter := F}]) ->
Name =:= <<"test2">> orelse F
end),
q:where(WhereFun),
q:join(?COMMENT_SCHEMA,
fun([#{id := UserId}, #{author := AuthorId}]) ->
UserId =:= AuthorId
Expand Down Expand Up @@ -523,6 +525,7 @@ operators_test() ->
Id =/= 7 andalso
not Id * 1 + 2 - 3 / 4 orelse
pg_sql:in(Id, [8,9,10]) orelse
pg_sql:in(Id, [11]) orelse
pg_sql:like(Id, <<"11%">>) orelse
pg_sql:ilike(Id, <<"11%">>) orelse
pg_sql:'~'(Id, <<"a">>) orelse
Expand All @@ -531,20 +534,21 @@ operators_test() ->
q:select(fun([T]) -> maps:with([name], T) end)
]))),
?assertEqual(
<<"select \"__alias-0\".\"name\" as \"name\" from \"users\" as \"__alias-0\" where "
"(((\"__alias-0\".\"id\" < $1) and "
"(\"__alias-0\".\"id\" <= $2)) or "
"(((\"__alias-0\".\"id\" > $3) and "
"(\"__alias-0\".\"id\" >= $4)) or "
"((not (\"__alias-0\".\"id\" = $5) and "
"(((not \"__alias-0\".\"id\" * $6) + $7) - ($8 / $9))) or "
"(\"__alias-0\".\"id\" = ANY($10) or "
"(\"__alias-0\".\"id\" like $11 or "
"(\"__alias-0\".\"id\" ilike $12 or "
"((\"__alias-0\".\"id\" ~ $13) or "
"(\"__alias-0\".\"id\" ~* $14))))))))">>,
<<"select \"__alias-0\".\"name\" as \"name\" from \"users\" as \"__alias-0\" where "
"(((\"__alias-0\".\"id\" < $1) and "
"(\"__alias-0\".\"id\" <= $2)) or "
"(((\"__alias-0\".\"id\" > $3) and "
"(\"__alias-0\".\"id\" >= $4)) or "
"((not (\"__alias-0\".\"id\" = $5) and "
"(((not \"__alias-0\".\"id\" * $6) + $7) - ($8 / $9))) or "
"(\"__alias-0\".\"id\" = ANY($10) or "
"((\"__alias-0\".\"id\" = $11) or "
"(\"__alias-0\".\"id\" like $12 or "
"(\"__alias-0\".\"id\" ilike $13 or "
"((\"__alias-0\".\"id\" ~ $14) or "
"(\"__alias-0\".\"id\" ~* $15)))))))))">>,
Sql),
?assertEqual([3,4,5,6,7,1,2,3,4,[8,9,10],<<"11%">>,<<"11%">>,<<"a">>,<<"A">>], Args),
?assertEqual([3,4,5,6,7,1,2,3,4,[8,9,10],11,<<"11%">>,<<"11%">>,<<"a">>,<<"A">>], Args),
?assertEqual({model, ?MODULE, ?USER_FIELDS_LIST([name])}, Feilds).

distinct_operation_test() ->
Expand Down Expand Up @@ -789,7 +793,14 @@ transform_fun_test() ->
TFun = equery_pt:transform_fun(Fun),
{Sql, Args} = qast:to_sql(TFun(3)),
?assertEqual(<<"not ($1 = $2)">>, Sql),
?assertEqual([3,2], Args).
?assertEqual([3,2], Args),

%% check some erl_eval functions
EvalStr = equery_pt:transform_fun(fun erl_eval:eval_str/1),
{ok, TFunR} = EvalStr(FunS ++ "\r\n"),
{SqlR, ArgsR} = qast:to_sql((equery_pt:transform_fun(TFunR))(3)),
?assertEqual(<<"not ($1 = $2)">>, SqlR),
?assertEqual([3,2], ArgsR).

join_type_test_() ->
Q = q:from(?USER_SCHEMA),
Expand Down Expand Up @@ -925,6 +936,9 @@ drop_distinct_on_test() ->
?assertEqual([], Args),
?assertEqual({model, ?MODULE, ?USER_FIELDS_LIST}, Feilds).

bad_set_test() ->
?assertException(error, bad_set, q:set(fun(_) -> bad end, q:from(?MODULE))).

%% =============================================================================
%% Internal functions
%% =============================================================================
Expand Down