Skip to content

Commit 4f0ddf6

Browse files
committed
move eunit tests to test dir
1 parent b14c84c commit 4f0ddf6

File tree

2 files changed

+78
-84
lines changed

2 files changed

+78
-84
lines changed

src/mod_invites.erl

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@
5454
-export([process/2]).
5555

5656
-ifdef(TEST).
57-
-export([create_roster_invite/2, create_account_invite/4, gen_invite/1, gen_invite/2, get_invites/2,
58-
is_token_valid/3]).
59-
-include_lib("eunit/include/eunit.hrl").
57+
-export([create_roster_invite/2, create_account_invite/4, find_invites_tree_root_t/4, gen_invite/1,
58+
gen_invite/2, get_invites/2, get_invites_tree_as_root_t/2, is_token_valid/3]).
6059
-endif.
6160

6261
-include("logger.hrl").
@@ -955,48 +954,6 @@ maybe_block_speedy_goat(Now, CreatedAt, Lvl) when Lvl == ?SPEEDY_GOAT_LEVELS ->
955954
maybe_block_speedy_goat(_, _, _) ->
956955
ok.
957956

958-
-ifdef(TEST).
959-
960-
find_invites_tree_root_t_test_() ->
961-
{setup,
962-
fun() ->
963-
meck:new(db, [non_strict]),
964-
meck:expect(db,
965-
get_invite_by_invitee_t,
966-
fun (_, <<"4@host">>) ->
967-
#invite_token{inviter = {<<"3">>, <<"host">>}};
968-
(_, <<"3@host">>) ->
969-
#invite_token{inviter = {<<"2">>, <<"host">>}};
970-
(_, <<"2@host">>) ->
971-
#invite_token{inviter = {<<"1">>, <<"host">>}};
972-
(_, _) ->
973-
{error, not_found}
974-
end),
975-
meck:new(gen_mod, [passthrough]),
976-
meck:expect(gen_mod, db_mod, 2, db),
977-
meck:new(calendar, [unstick, passthrough]),
978-
meck:expect(calendar, now_to_datetime, 1, then),
979-
meck:expect(calendar, datetime_to_gregorian_seconds, fun(then) -> 1 end),
980-
[db, gen_mod, calendar]
981-
end,
982-
fun meck:unload/1,
983-
fun(_) ->
984-
[%% lvl not reached
985-
?_assertMatch({<<"1">>, <<"host">>},
986-
find_invites_tree_root_t(2, host, {<<"3">>, <<"host">>}, 0)),
987-
%% lvl reached
988-
?_assertThrow(speedy_goat, find_invites_tree_root_t(2, host, {<<"4">>, <<"host">>}, 0)),
989-
%% lvl reached but later
990-
?_assertMatch({<<"1">>, <<"host">>},
991-
find_invites_tree_root_t(?SPEEDY_GOAT_SECONDS + 1,
992-
host,
993-
{<<"4">>, <<"host">>},
994-
0)),
995-
?_assert(meck:validate(db))]
996-
end}.
997-
998-
-endif.
999-
1000957
-spec get_invites_tree_as_root_t(binary(), {binary(), binary()}) -> [invite_token()].
1001958
get_invites_tree_as_root_t(Host, Inviter) ->
1002959
Invites = get_invites_t(Host, Inviter),
@@ -1029,45 +986,6 @@ get_invites_tree_as_root_t(Host,
1029986
get_invites_tree_as_root_t(Host, Inviter, Invites, [Invite | Acc])
1030987
end.
1031988

1032-
-ifdef(TEST).
1033-
1034-
get_invites_tree_as_root_t_test_() ->
1035-
{setup,
1036-
fun() ->
1037-
meck:new(db, [non_strict]),
1038-
meck:expect(db,
1039-
get_invites_t,
1040-
fun (_, {<<"1">>, _}) ->
1041-
[#invite_token{invitee = <<"2@host">>, type = account_only},
1042-
#invite_token{invitee = <<"rosterinvite@forcecrash">>}];
1043-
(_, {<<"2">>, _}) ->
1044-
[#invite_token{invitee = <<"3@host">>, type = account_only},
1045-
#invite_token{invitee = <<"4@host">>, type = account_only}];
1046-
(_, {<<"3">>, _}) ->
1047-
[#invite_token{invitee = <<"5@host">>, type = account_subscription},
1048-
#invite_token{invitee = <<"6@host">>, account_name = <<"6">>},
1049-
#invite_token{type = account_only}];
1050-
(_, {_, <<"host">>}) ->
1051-
[]
1052-
end),
1053-
meck:new(gen_mod, [passthrough]),
1054-
meck:expect(gen_mod, db_mod, 2, db),
1055-
meck:expect(jid,
1056-
decode,
1057-
fun(Str) ->
1058-
[LUser, LServer] =
1059-
[list_to_binary(T) || T <- string:tokens(binary_to_list(Str), "@")],
1060-
#jid{luser = LUser, lserver = LServer}
1061-
end),
1062-
[db, gen_mod, jid]
1063-
end,
1064-
fun meck:unload/1,
1065-
fun(_) ->
1066-
[?_assertMatch(6, length(get_invites_tree_as_root_t(<<"host">>, {<<"1">>, <<"host">>})))]
1067-
end}.
1068-
1069-
-endif.
1070-
1071989
maybe_throw({error, _} = Error) ->
1072990
throw(Error);
1073991
maybe_throw(Good) ->

test/invites_tests.erl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
-include("mod_invites.hrl").
3333
-include("mod_roster.hrl").
3434

35+
-include_lib("eunit/include/eunit.hrl").
36+
3537
%% killme
3638
-record(ejabberd_module,
3739
{module_host = {undefined, <<"">>} :: {atom(), binary()},
@@ -41,6 +43,80 @@
4143

4244
%% @format-begin
4345

46+
find_invites_tree_root_t_test_() ->
47+
{setup,
48+
fun() ->
49+
meck:new(db, [non_strict]),
50+
meck:expect(db,
51+
get_invite_by_invitee_t,
52+
fun (_, <<"4@host">>) ->
53+
#invite_token{inviter = {<<"3">>, <<"host">>}};
54+
(_, <<"3@host">>) ->
55+
#invite_token{inviter = {<<"2">>, <<"host">>}};
56+
(_, <<"2@host">>) ->
57+
#invite_token{inviter = {<<"1">>, <<"host">>}};
58+
(_, _) ->
59+
{error, not_found}
60+
end),
61+
meck:new(gen_mod, [passthrough]),
62+
meck:expect(gen_mod, db_mod, 2, db),
63+
meck:new(calendar, [unstick, passthrough]),
64+
meck:expect(calendar, now_to_datetime, 1, then),
65+
meck:expect(calendar, datetime_to_gregorian_seconds, fun(then) -> 1 end),
66+
[db, gen_mod, calendar]
67+
end,
68+
fun meck:unload/1,
69+
fun(_) ->
70+
[%% lvl not reached
71+
?_assertMatch({<<"1">>, <<"host">>},
72+
mod_invites:find_invites_tree_root_t(2, host, {<<"3">>, <<"host">>}, 0)),
73+
%% lvl reached
74+
?_assertThrow(speedy_goat, mod_invites:find_invites_tree_root_t(2, host, {<<"4">>, <<"host">>}, 0)),
75+
%% lvl reached but later
76+
?_assertMatch({<<"1">>, <<"host">>},
77+
mod_invites:find_invites_tree_root_t(?SPEEDY_GOAT_SECONDS + 1,
78+
host,
79+
{<<"4">>, <<"host">>},
80+
0)),
81+
?_assert(meck:validate(db))]
82+
end}.
83+
84+
get_invites_tree_as_root_t_test_() ->
85+
{setup,
86+
fun() ->
87+
meck:new(db, [non_strict]),
88+
meck:expect(db,
89+
get_invites_t,
90+
fun (_, {<<"1">>, _}) ->
91+
[#invite_token{invitee = <<"2@host">>, type = account_only},
92+
#invite_token{invitee = <<"rosterinvite@forcecrash">>}];
93+
(_, {<<"2">>, _}) ->
94+
[#invite_token{invitee = <<"3@host">>, type = account_only},
95+
#invite_token{invitee = <<"4@host">>, type = account_only}];
96+
(_, {<<"3">>, _}) ->
97+
[#invite_token{invitee = <<"5@host">>, type = account_subscription},
98+
#invite_token{invitee = <<"6@host">>, account_name = <<"6">>},
99+
#invite_token{type = account_only}];
100+
(_, {_, <<"host">>}) ->
101+
[]
102+
end),
103+
meck:new(gen_mod, [passthrough]),
104+
meck:expect(gen_mod, db_mod, 2, db),
105+
meck:expect(jid,
106+
decode,
107+
fun(Str) ->
108+
[LUser, LServer] =
109+
[list_to_binary(T) || T <- string:tokens(binary_to_list(Str), "@")],
110+
#jid{luser = LUser, lserver = LServer}
111+
end),
112+
[db, gen_mod, jid]
113+
end,
114+
fun meck:unload/1,
115+
fun(_) ->
116+
[?_assertMatch(6, length(mod_invites:get_invites_tree_as_root_t(<<"host">>, {<<"1">>, <<"host">>})))]
117+
end}.
118+
119+
44120
%%%===================================================================
45121
%%% API
46122
%%%===================================================================

0 commit comments

Comments
 (0)