Rewrite and expand EUnit suite; run unit tests in CI from shared test image. Refs EventHub/EventHubBack#36 [skip ci]
This commit is contained in:
@@ -2,27 +2,16 @@
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(TABLES, [user, calendar, event]).
|
||||
|
||||
setup() ->
|
||||
mnesia:start(),
|
||||
mnesia:create_table(user, [
|
||||
{attributes, record_info(fields, user)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
mnesia:create_table(calendar, [
|
||||
{attributes, record_info(fields, calendar)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
mnesia:create_table(event, [
|
||||
{attributes, record_info(fields, event)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
eh_test_support:start_mnesia(),
|
||||
eh_test_support:ensure_tables(?TABLES),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(event),
|
||||
mnesia:delete_table(calendar),
|
||||
mnesia:delete_table(user),
|
||||
mnesia:stop(),
|
||||
eh_test_support:delete_tables(?TABLES),
|
||||
eh_test_support:stop_mnesia(),
|
||||
ok.
|
||||
|
||||
logic_search_test_() ->
|
||||
@@ -75,89 +64,96 @@ create_test_event(CalendarId, Title, Description, StartTime, Tags, Location) ->
|
||||
{ok, Updated} = core_event:get_by_id(Event#event.id),
|
||||
Updated#event.id.
|
||||
|
||||
add_days(DateTime, Days) ->
|
||||
Sec = calendar:datetime_to_gregorian_seconds(DateTime) + Days * 86400,
|
||||
calendar:gregorian_seconds_to_datetime(Sec).
|
||||
|
||||
events_from({ok, Total, #{<<"events">> := Events}}) ->
|
||||
{Total, Events}.
|
||||
|
||||
calendars_from({ok, Total, #{<<"calendars">> := Calendars}}) ->
|
||||
{Total, Calendars}.
|
||||
|
||||
%% Тесты
|
||||
test_search_events_by_text() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
create_test_event(CalendarId, <<"Python Workshop">>, <<"Learn Python">>, StartTime, [], undefined),
|
||||
create_test_event(CalendarId, <<"JavaScript Conference">>, <<"JS for everyone">>, StartTime, [], undefined),
|
||||
create_test_event(CalendarId, <<"Yoga Class">>, <<"Morning yoga">>, StartTime, [], undefined),
|
||||
|
||||
Params = #{},
|
||||
{ok, Total, Results} = logic_search:search(<<"event">>, <<"Python">>, OwnerId, Params),
|
||||
{Total, Results} = events_from(logic_search:search(<<"event">>, <<"Python">>, OwnerId, Params)),
|
||||
?assertEqual(1, Total),
|
||||
?assertEqual(1, length(Results)),
|
||||
|
||||
{ok, Total2, _Results2} = logic_search:search(<<"event">>, <<"conference">>, OwnerId, Params),
|
||||
{Total2, _} = events_from(logic_search:search(<<"event">>, <<"conference">>, OwnerId, Params)),
|
||||
?assertEqual(1, Total2).
|
||||
|
||||
test_search_events_by_tags() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
create_test_event(CalendarId, <<"Event 1">>, <<"">>, StartTime, [<<"python">>, <<"workshop">>], undefined),
|
||||
create_test_event(CalendarId, <<"Event 2">>, <<"">>, StartTime, [<<"javascript">>, <<"conference">>], undefined),
|
||||
create_test_event(CalendarId, <<"Event 3">>, <<"">>, StartTime, [<<"python">>, <<"advanced">>], undefined),
|
||||
|
||||
% Поиск по одному тегу
|
||||
Params = #{tags => <<"python">>},
|
||||
{ok, Total, _} = logic_search:search(<<"event">>, undefined, OwnerId, Params),
|
||||
?assertEqual(2, Total), % Event 1 и Event 3
|
||||
{Total, _} = events_from(logic_search:search(<<"event">>, undefined, OwnerId, #{tags => <<"python">>})),
|
||||
?assertEqual(2, Total),
|
||||
|
||||
% Поиск по тегу, который есть только у одного события
|
||||
Params2 = #{tags => <<"workshop">>},
|
||||
{ok, Total2, _} = logic_search:search(<<"event">>, undefined, OwnerId, Params2),
|
||||
?assertEqual(1, Total2), % Только Event 1
|
||||
{Total2, _} = events_from(logic_search:search(<<"event">>, undefined, OwnerId, #{tags => <<"workshop">>})),
|
||||
?assertEqual(1, Total2),
|
||||
|
||||
% Поиск по тегу, которого нет
|
||||
Params3 = #{tags => <<"nonexistent">>},
|
||||
{ok, Total3, _} = logic_search:search(<<"event">>, undefined, OwnerId, Params3),
|
||||
{Total3, _} = events_from(logic_search:search(<<"event">>, undefined, OwnerId, #{tags => <<"nonexistent">>})),
|
||||
?assertEqual(0, Total3).
|
||||
|
||||
test_search_events_by_date() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
|
||||
create_test_event(CalendarId, <<"June Event">>, <<"">>, {{2026, 6, 15}, {10, 0, 0}}, [], undefined),
|
||||
create_test_event(CalendarId, <<"July Event">>, <<"">>, {{2026, 7, 15}, {10, 0, 0}}, [], undefined),
|
||||
create_test_event(CalendarId, <<"August Event">>, <<"">>, {{2026, 8, 15}, {10, 0, 0}}, [], undefined),
|
||||
Base = eh_test_support:future_start(),
|
||||
create_test_event(CalendarId, <<"Near Event">>, <<"">>, Base, [], undefined),
|
||||
create_test_event(CalendarId, <<"Mid Event">>, <<"">>, add_days(Base, 30), [], undefined),
|
||||
create_test_event(CalendarId, <<"Far Event">>, <<"">>, add_days(Base, 60), [], undefined),
|
||||
|
||||
Params = #{from => {{2026, 6, 1}, {0, 0, 0}}, to => {{2026, 6, 30}, {23, 59, 59}}},
|
||||
{ok, Total, _} = logic_search:search(<<"event">>, undefined, OwnerId, Params),
|
||||
Params = #{from => add_days(Base, -1), to => add_days(Base, 7)},
|
||||
{Total, _} = events_from(logic_search:search(<<"event">>, undefined, OwnerId, Params)),
|
||||
?assertEqual(1, Total).
|
||||
|
||||
test_search_events_by_location() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
MoscowLoc = #location{address = <<"Moscow">>, lat = 55.7558, lon = 37.6173},
|
||||
SpbLoc = #location{address = <<"SPb">>, lat = 59.9343, lon = 30.3351},
|
||||
|
||||
create_test_event(CalendarId, <<"Moscow Event">>, <<"">>, StartTime, [], MoscowLoc),
|
||||
create_test_event(CalendarId, <<"SPb Event">>, <<"">>, StartTime, [], SpbLoc),
|
||||
|
||||
Params = #{lat => 55.7558, lon => 37.6173, radius => 10},
|
||||
{ok, Total, _Results} = logic_search:search(<<"event">>, undefined, OwnerId, Params),
|
||||
{Total, _} = events_from(logic_search:search(<<"event">>, undefined, OwnerId,
|
||||
#{lat => 55.7558, lon => 37.6173, radius => 10})),
|
||||
?assertEqual(1, Total),
|
||||
|
||||
Params2 = #{lat => 59.9343, lon => 30.3351, radius => 10},
|
||||
{ok, Total2, _} = logic_search:search(<<"event">>, undefined, OwnerId, Params2),
|
||||
{Total2, _} = events_from(logic_search:search(<<"event">>, undefined, OwnerId,
|
||||
#{lat => 59.9343, lon => 30.3351, radius => 10})),
|
||||
?assertEqual(1, Total2).
|
||||
|
||||
test_combined_search() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
|
||||
StartTime = {{2026, 6, 15}, {10, 0, 0}},
|
||||
create_test_event(CalendarId, <<"Python Workshop">>, <<"Learn Python">>, StartTime, [<<"python">>, <<"workshop">>], undefined),
|
||||
create_test_event(CalendarId, <<"Python Advanced">>, <<"Advanced Python">>, {{2026, 7, 15}, {10, 0, 0}}, [<<"python">>, <<"advanced">>], undefined),
|
||||
Base = eh_test_support:future_start(),
|
||||
create_test_event(CalendarId, <<"Python Workshop">>, <<"Learn Python">>, Base,
|
||||
[<<"python">>, <<"workshop">>], undefined),
|
||||
create_test_event(CalendarId, <<"Python Advanced">>, <<"Advanced Python">>, add_days(Base, 30),
|
||||
[<<"python">>, <<"advanced">>], undefined),
|
||||
|
||||
Params = #{from => {{2026, 6, 1}, {0, 0, 0}}, to => {{2026, 6, 30}, {23, 59, 59}}, tags => <<"python">>},
|
||||
{ok, Total, _} = logic_search:search(<<"event">>, <<"Workshop">>, OwnerId, Params),
|
||||
Params = #{from => add_days(Base, -1), to => add_days(Base, 7), tags => <<"python">>},
|
||||
{Total, _} = events_from(logic_search:search(<<"event">>, <<"Workshop">>, OwnerId, Params)),
|
||||
?assertEqual(1, Total).
|
||||
|
||||
test_search_calendars() ->
|
||||
@@ -165,60 +161,57 @@ test_search_calendars() ->
|
||||
create_test_calendar(OwnerId, personal, [<<"tech">>, <<"programming">>]),
|
||||
create_test_calendar(OwnerId, commercial, [<<"yoga">>, <<"wellness">>]),
|
||||
|
||||
Params = #{tags => <<"tech">>},
|
||||
{ok, Total, _} = logic_search:search(<<"calendar">>, undefined, OwnerId, Params),
|
||||
{Total, _} = calendars_from(logic_search:search(<<"calendar">>, undefined, OwnerId, #{tags => <<"tech">>})),
|
||||
?assertEqual(1, Total),
|
||||
|
||||
{ok, Total2, _} = logic_search:search(<<"calendar">>, <<"Calendar">>, OwnerId, #{}),
|
||||
{Total2, _} = calendars_from(logic_search:search(<<"calendar">>, <<"Calendar">>, OwnerId, #{})),
|
||||
?assertEqual(2, Total2).
|
||||
|
||||
test_search_all() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
create_test_event(CalendarId, <<"Test Event">>, <<"">>, StartTime, [], undefined),
|
||||
|
||||
{ok, Total, Results} = logic_search:search(undefined, <<"Test">>, OwnerId, #{}),
|
||||
?assert(Total >= 2),
|
||||
?assert(maps:is_key(events, Results)),
|
||||
?assert(maps:is_key(calendars, Results)).
|
||||
?assert(maps:is_key(<<"events">>, Results)),
|
||||
?assert(maps:is_key(<<"calendars">>, Results)).
|
||||
|
||||
test_pagination() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
|
||||
lists:foreach(fun(I) ->
|
||||
Title = iolist_to_binary(["Event ", integer_to_binary(I)]),
|
||||
create_test_event(CalendarId, Title, <<"">>, StartTime, [], undefined)
|
||||
end, [1, 2, 3, 4, 5]),
|
||||
|
||||
Params = #{limit => 2, offset => 0},
|
||||
{ok, Total, Results} = logic_search:search(<<"event">>, undefined, OwnerId, Params),
|
||||
{Total, Results} = events_from(logic_search:search(<<"event">>, undefined, OwnerId, #{limit => 2, offset => 0})),
|
||||
?assertEqual(5, Total),
|
||||
?assertEqual(2, length(Results)),
|
||||
|
||||
Params2 = #{limit => 2, offset => 2},
|
||||
{ok, _, Results2} = logic_search:search(<<"event">>, undefined, OwnerId, Params2),
|
||||
{_, Results2} = events_from(logic_search:search(<<"event">>, undefined, OwnerId, #{limit => 2, offset => 2})),
|
||||
?assertEqual(2, length(Results2)),
|
||||
|
||||
Params3 = #{limit => 2, offset => 4},
|
||||
{ok, _, Results3} = logic_search:search(<<"event">>, undefined, OwnerId, Params3),
|
||||
{_, Results3} = events_from(logic_search:search(<<"event">>, undefined, OwnerId, #{limit => 2, offset => 4})),
|
||||
?assertEqual(1, length(Results3)).
|
||||
|
||||
test_sorting() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
|
||||
create_test_event(CalendarId, <<"Early">>, <<"">>, {{2026, 6, 1}, {10, 0, 0}}, [], undefined),
|
||||
create_test_event(CalendarId, <<"Late">>, <<"">>, {{2026, 6, 30}, {10, 0, 0}}, [], undefined),
|
||||
Base = eh_test_support:future_start(),
|
||||
create_test_event(CalendarId, <<"Early">>, <<"">>, Base, [], undefined),
|
||||
create_test_event(CalendarId, <<"Late">>, <<"">>, add_days(Base, 29), [], undefined),
|
||||
|
||||
Params = #{sort => <<"start_time">>, order => <<"asc">>},
|
||||
{ok, _, [First | _]} = logic_search:search(<<"event">>, undefined, OwnerId, Params),
|
||||
{_, [First | _]} = events_from(logic_search:search(<<"event">>, undefined, OwnerId,
|
||||
#{sort => <<"start_time">>, order => <<"asc">>})),
|
||||
?assertMatch(#{title := <<"Early">>}, First),
|
||||
|
||||
Params2 = #{sort => <<"start_time">>, order => <<"desc">>},
|
||||
{ok, _, [First2 | _]} = logic_search:search(<<"event">>, undefined, OwnerId, Params2),
|
||||
{_, [First2 | _]} = events_from(logic_search:search(<<"event">>, undefined, OwnerId,
|
||||
#{sort => <<"start_time">>, order => <<"desc">>})),
|
||||
?assertMatch(#{title := <<"Late">>}, First2).
|
||||
|
||||
test_access_control() ->
|
||||
@@ -228,12 +221,11 @@ test_access_control() ->
|
||||
PersonalId = create_test_calendar(OwnerId, personal, []),
|
||||
CommercialId = create_test_calendar(OwnerId, commercial, []),
|
||||
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
create_test_event(PersonalId, <<"Personal Event">>, <<"">>, StartTime, [], undefined),
|
||||
create_test_event(CommercialId, <<"Public Event">>, <<"">>, StartTime, [], undefined),
|
||||
|
||||
% Другой пользователь видит только коммерческие события
|
||||
{ok, Total, Results} = logic_search:search(<<"event">>, undefined, OtherId, #{}),
|
||||
{Total, Results} = events_from(logic_search:search(<<"event">>, undefined, OtherId, #{})),
|
||||
?assertEqual(1, Total),
|
||||
[Event] = Results,
|
||||
?assertMatch(#{title := <<"Public Event">>}, Event).
|
||||
@@ -241,9 +233,9 @@ test_access_control() ->
|
||||
test_empty_search() ->
|
||||
OwnerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, personal, []),
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
create_test_event(CalendarId, <<"Test">>, <<"">>, StartTime, [], undefined),
|
||||
|
||||
{ok, Total, Results} = logic_search:search(<<"event">>, <<"nonexistent">>, OwnerId, #{}),
|
||||
{Total, Results} = events_from(logic_search:search(<<"event">>, <<"nonexistent">>, OwnerId, #{})),
|
||||
?assertEqual(0, Total),
|
||||
?assertEqual([], Results).
|
||||
?assertEqual([], Results).
|
||||
|
||||
Reference in New Issue
Block a user