Files
EventHubBack/test/unit/core_event_recurring_tests.erl
T

102 lines
3.4 KiB
Erlang

-module(core_event_recurring_tests).
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
-define(TABLES, [event, recurrence_exception]).
setup() ->
eh_test_support:start_mnesia(),
eh_test_support:ensure_tables(?TABLES),
ok.
cleanup(_) ->
eh_test_support:delete_tables(?TABLES),
eh_test_support:stop_mnesia(),
ok.
core_event_recurring_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
[
{"Create recurring event test", fun test_create_recurring/0},
{"Materialize occurrence test", fun test_materialize_occurrence/0},
{"Materialize existing occurrence test", fun test_materialize_existing/0},
{"Materialize non-recurring test", fun test_materialize_non_recurring/0}
]}.
add_days(DateTime, Days) ->
Sec = calendar:datetime_to_gregorian_seconds(DateTime) + Days * 86400,
calendar:gregorian_seconds_to_datetime(Sec).
test_create_recurring() ->
CalendarId = <<"calendar123">>,
Title = <<"Weekly Meeting">>,
StartTime = eh_test_support:future_start(),
Duration = 60,
RRule = #{<<"freq">> => <<"WEEKLY">>, <<"interval">> => 1},
{ok, Event} = core_event:create_recurring(CalendarId, Title, StartTime, Duration, RRule),
?assertEqual(CalendarId, Event#event.calendar_id),
?assertEqual(Title, Event#event.title),
?assertEqual(recurring, Event#event.event_type),
?assertEqual(false, Event#event.is_instance),
?assert(is_binary(Event#event.recurrence_rule)),
Decoded = jsx:decode(Event#event.recurrence_rule, [return_maps]),
RRuleMap = case Decoded of
Map when is_map(Map) -> Map;
{ok, Map} -> Map
end,
?assertEqual(<<"WEEKLY">>, maps:get(<<"freq">>, RRuleMap)).
test_materialize_occurrence() ->
CalendarId = <<"calendar123">>,
{MasterId, StartTime} = create_recurring_event(CalendarId),
OccurrenceStart = add_days(StartTime, 7),
SpecialistId = <<"specialist123">>,
{ok, Instance} = core_event:materialize_occurrence(MasterId, OccurrenceStart, SpecialistId),
?assertEqual(CalendarId, Instance#event.calendar_id),
?assertEqual(MasterId, Instance#event.master_id),
?assertEqual(OccurrenceStart, Instance#event.start_time),
?assertEqual(SpecialistId, Instance#event.specialist_id),
?assertEqual(single, Instance#event.event_type),
?assertEqual(true, Instance#event.is_instance).
test_materialize_existing() ->
CalendarId = <<"calendar123">>,
{MasterId, StartTime} = create_recurring_event(CalendarId),
OccurrenceStart = add_days(StartTime, 7),
SpecialistId1 = <<"specialist1">>,
SpecialistId2 = <<"specialist2">>,
{ok, Instance1} = core_event:materialize_occurrence(MasterId, OccurrenceStart, SpecialistId1),
{ok, Instance2} = core_event:materialize_occurrence(MasterId, OccurrenceStart, SpecialistId2),
?assertEqual(Instance1#event.id, Instance2#event.id),
?assertEqual(SpecialistId1, Instance2#event.specialist_id).
test_materialize_non_recurring() ->
CalendarId = <<"calendar123">>,
StartTime = eh_test_support:future_start(),
{ok, SingleEvent} = core_event:create(CalendarId, <<"Single">>, StartTime, 60),
{error, not_recurring} = core_event:materialize_occurrence(
SingleEvent#event.id, add_days(StartTime, 7), <<"spec">>
).
create_recurring_event(CalendarId) ->
StartTime = eh_test_support:future_start(),
{ok, Event} = core_event:create_recurring(
CalendarId,
<<"Weekly Meeting">>,
StartTime,
60,
#{<<"freq">> => <<"WEEKLY">>, <<"interval">> => 1}
),
{Event#event.id, StartTime}.