Skip to content

Commit

Permalink
Merge pull request #147 from inaka/elbrujohalcon.147.error_using_sumo…
Browse files Browse the repository at this point in the history
…_find_to_retri

Error using sumo:find to retrieve an inexistent element
  • Loading branch information
Brujo Benavides committed May 9, 2015
2 parents 5b9606b + d7b6f0c commit 55334a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/sumo_store_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,20 @@ find_by(DocName, Conditions, Limit, Offset, State) ->
sumo_store:result([sumo_internal:doc()], state()).
find_by(DocName, Conditions, [], Limit, Offset, State) ->
MatchSpec = build_match_spec(DocName, Conditions),
Transaction0 = fun() -> mnesia:select(DocName, MatchSpec) end,
TransactionL =
fun() ->
case mnesia:select(DocName, MatchSpec, Offset + Limit, read) of
{ManyItems, _Cont} ->
lists:sublist(ManyItems, Offset + 1, Limit);
'$end_of_table' ->
[]
end
end,
Transaction =
case Limit of
0 ->
fun() -> mnesia:select(DocName, MatchSpec) end;
Limit ->
fun() ->
{ManyItems, _Cont} =
mnesia:select(DocName, MatchSpec, Offset + Limit, read),
lists:sublist(ManyItems, Offset + 1, Limit)
end
0 -> Transaction0;
Limit -> TransactionL
end,
case mnesia:transaction(Transaction) of
{aborted, Reason} ->
Expand Down
10 changes: 10 additions & 0 deletions test/sumo_basic_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
]).

-export([
find/1,
find_all/1,
find_by/1,
delete_all/1,
Expand Down Expand Up @@ -54,6 +55,9 @@ end_per_suite(Config) ->
%%% Exported Tests Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

find(_Config) ->
run_all_stores(fun find_module/1).

find_all(_Config) ->
run_all_stores(fun find_all_module/1).

Expand Down Expand Up @@ -84,6 +88,12 @@ init_store(Module) ->
%% Sync Timeout.
sync_timeout(Module).

find_module(Module) ->
[First, Second | _] = sumo:find_all(Module),
First = sumo:find(Module, Module:id(First)),
Second = sumo:find(Module, Module:id(Second)),
notfound = sumo:find(Module, 0).

find_all_module(Module) ->
6 = length(sumo:find_all(Module)).

Expand Down

0 comments on commit 55334a8

Please sign in to comment.