Stage 3.3
This commit is contained in:
104
test/core_event_recurring_tests.erl
Normal file
104
test/core_event_recurring_tests.erl
Normal file
@@ -0,0 +1,104 @@
|
||||
-module(core_event_recurring_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
setup() ->
|
||||
mnesia:start(),
|
||||
mnesia:create_table(event, [
|
||||
{attributes, record_info(fields, event)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
mnesia:create_table(recurrence_exception, [
|
||||
{attributes, record_info(fields, recurrence_exception)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(recurrence_exception),
|
||||
mnesia:delete_table(event),
|
||||
mnesia:stop(),
|
||||
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}
|
||||
]}.
|
||||
|
||||
test_create_recurring() ->
|
||||
CalendarId = <<"calendar123">>,
|
||||
Title = <<"Weekly Meeting">>,
|
||||
StartTime = {{2026, 4, 20}, {10, 0, 0}},
|
||||
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 = create_recurring_event(CalendarId),
|
||||
OccurrenceStart = {{2026, 4, 27}, {10, 0, 0}},
|
||||
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 = create_recurring_event(CalendarId),
|
||||
OccurrenceStart = {{2026, 4, 27}, {10, 0, 0}},
|
||||
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">>,
|
||||
{ok, SingleEvent} = core_event:create(CalendarId, <<"Single">>, {{2026, 4, 20}, {10, 0, 0}}, 60),
|
||||
|
||||
{error, not_recurring} = core_event:materialize_occurrence(
|
||||
SingleEvent#event.id, {{2026, 4, 27}, {10, 0, 0}}, <<"spec">>
|
||||
).
|
||||
|
||||
create_recurring_event(CalendarId) ->
|
||||
{ok, Event} = core_event:create_recurring(
|
||||
CalendarId,
|
||||
<<"Weekly Meeting">>,
|
||||
{{2026, 4, 20}, {10, 0, 0}},
|
||||
60,
|
||||
#{<<"freq">> => <<"WEEKLY">>, <<"interval">> => 1}
|
||||
),
|
||||
Event#event.id.
|
||||
155
test/logic_event_recurring_tests.erl
Normal file
155
test/logic_event_recurring_tests.erl
Normal file
@@ -0,0 +1,155 @@
|
||||
-module(logic_event_recurring_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
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()]}
|
||||
]),
|
||||
mnesia:create_table(recurrence_exception, [
|
||||
{attributes, record_info(fields, recurrence_exception)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(recurrence_exception),
|
||||
mnesia:delete_table(event),
|
||||
mnesia:delete_table(calendar),
|
||||
mnesia:delete_table(user),
|
||||
mnesia:stop(),
|
||||
ok.
|
||||
|
||||
logic_event_recurring_test_() ->
|
||||
{foreach,
|
||||
fun setup/0,
|
||||
fun cleanup/1,
|
||||
[
|
||||
{"Create recurring event test", fun test_create_recurring_event/0},
|
||||
{"Create recurring event invalid RRULE test", fun test_create_recurring_invalid/0},
|
||||
{"Get occurrences test", fun test_get_occurrences/0},
|
||||
{"Cancel occurrence test", fun test_cancel_occurrence/0},
|
||||
{"Get occurrences with cancelled test", fun test_occurrences_with_cancelled/0},
|
||||
{"Materialize for booking test", fun test_materialize_for_booking/0}
|
||||
]}.
|
||||
|
||||
create_test_user_and_calendar() ->
|
||||
UserId = base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}),
|
||||
User = #user{
|
||||
id = UserId,
|
||||
email = <<"test@example.com">>,
|
||||
password_hash = <<"hash">>,
|
||||
role = user,
|
||||
status = active,
|
||||
created_at = calendar:universal_time(),
|
||||
updated_at = calendar:universal_time()
|
||||
},
|
||||
mnesia:dirty_write(User),
|
||||
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Test Calendar">>, <<"">>),
|
||||
{UserId, Calendar#calendar.id}.
|
||||
|
||||
test_create_recurring_event() ->
|
||||
{UserId, CalendarId} = create_test_user_and_calendar(),
|
||||
Title = <<"Weekly Meeting">>,
|
||||
StartTime = {{2026, 5, 1}, {10, 0, 0}},
|
||||
Duration = 60,
|
||||
RRule = #{<<"freq">> => <<"WEEKLY">>, <<"interval">> => 1},
|
||||
|
||||
{ok, Event} = logic_event:create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule),
|
||||
|
||||
?assertEqual(CalendarId, Event#event.calendar_id),
|
||||
?assertEqual(Title, Event#event.title),
|
||||
?assertEqual(recurring, Event#event.event_type).
|
||||
|
||||
test_create_recurring_invalid() ->
|
||||
{UserId, CalendarId} = create_test_user_and_calendar(),
|
||||
Title = <<"Invalid">>,
|
||||
StartTime = {{2026, 5, 1}, {10, 0, 0}},
|
||||
Duration = 60,
|
||||
InvalidRRule = #{<<"freq">> => <<"YEARLY">>, <<"interval">> => 1},
|
||||
|
||||
{error, invalid_rrule} = logic_event:create_recurring_event(
|
||||
UserId, CalendarId, Title, StartTime, Duration, InvalidRRule
|
||||
).
|
||||
|
||||
test_get_occurrences() ->
|
||||
{UserId, CalendarId} = create_test_user_and_calendar(),
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}}, % Июнь 2026
|
||||
RRule = #{<<"freq">> => <<"WEEKLY">>, <<"interval">> => 1},
|
||||
|
||||
{ok, Event} = logic_event:create_recurring_event(
|
||||
UserId, CalendarId, <<"Weekly">>, StartTime, 60, RRule
|
||||
),
|
||||
|
||||
RangeEnd = {{2026, 6, 29}, {10, 0, 0}}, % Конец июня
|
||||
{ok, Occurrences} = logic_event:get_occurrences(UserId, Event#event.id, RangeEnd),
|
||||
|
||||
% 1, 8, 15, 22, 29 июня = 5 вхождений
|
||||
?assertEqual(5, length(Occurrences)).
|
||||
|
||||
test_cancel_occurrence() ->
|
||||
{UserId, CalendarId} = create_test_user_and_calendar(),
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
RRule = #{<<"freq">> => <<"WEEKLY">>, <<"interval">> => 1},
|
||||
|
||||
{ok, Event} = logic_event:create_recurring_event(
|
||||
UserId, CalendarId, <<"Weekly">>, StartTime, 60, RRule
|
||||
),
|
||||
|
||||
CancelTime = {{2026, 6, 8}, {10, 0, 0}}, % Второе вхождение
|
||||
{ok, cancelled} = logic_event:cancel_occurrence(UserId, Event#event.id, CancelTime).
|
||||
|
||||
test_occurrences_with_cancelled() ->
|
||||
{UserId, CalendarId} = create_test_user_and_calendar(),
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
RRule = #{<<"freq">> => <<"WEEKLY">>, <<"interval">> => 1},
|
||||
|
||||
{ok, Event} = logic_event:create_recurring_event(
|
||||
UserId, CalendarId, <<"Weekly">>, StartTime, 60, RRule
|
||||
),
|
||||
|
||||
% Отменяем второе вхождение
|
||||
CancelTime = {{2026, 6, 8}, {10, 0, 0}},
|
||||
{ok, cancelled} = logic_event:cancel_occurrence(UserId, Event#event.id, CancelTime),
|
||||
|
||||
RangeEnd = {{2026, 6, 29}, {10, 0, 0}},
|
||||
{ok, Occurrences} = logic_event:get_occurrences(UserId, Event#event.id, RangeEnd),
|
||||
|
||||
% 1, 15, 22, 29 июня = 4 вхождения (одно отменено)
|
||||
?assertEqual(4, length(Occurrences)),
|
||||
|
||||
% Проверяем, что отменённого вхождения нет
|
||||
Starts = [O || {virtual, O} <- Occurrences],
|
||||
?assertNot(lists:member(CancelTime, Starts)).
|
||||
|
||||
test_materialize_for_booking() ->
|
||||
{UserId, CalendarId} = create_test_user_and_calendar(),
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
RRule = #{<<"freq">> => <<"WEEKLY">>, <<"interval">> => 1},
|
||||
|
||||
{ok, Event} = logic_event:create_recurring_event(
|
||||
UserId, CalendarId, <<"Weekly">>, StartTime, 60, RRule
|
||||
),
|
||||
|
||||
OccurrenceStart = {{2026, 6, 8}, {10, 0, 0}},
|
||||
SpecialistId = <<"specialist123">>,
|
||||
|
||||
{ok, Instance} = logic_event:materialize_for_booking(
|
||||
Event#event.id, OccurrenceStart, SpecialistId
|
||||
),
|
||||
|
||||
?assertEqual(Event#event.id, Instance#event.master_id),
|
||||
?assertEqual(OccurrenceStart, Instance#event.start_time),
|
||||
?assertEqual(SpecialistId, Instance#event.specialist_id),
|
||||
?assertEqual(true, Instance#event.is_instance).
|
||||
110
test/logic_recurrence_tests.erl
Normal file
110
test/logic_recurrence_tests.erl
Normal file
@@ -0,0 +1,110 @@
|
||||
-module(logic_recurrence_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
logic_recurrence_test_() ->
|
||||
[
|
||||
{"Parse RRULE test", fun test_parse_rrule/0},
|
||||
{"Validate RRULE test", fun test_validate_rrule/0},
|
||||
{"Generate daily occurrences test", fun test_daily_occurrences/0},
|
||||
{"Generate weekly occurrences test", fun test_weekly_occurrences/0},
|
||||
{"Generate monthly occurrences test", fun test_monthly_occurrences/0},
|
||||
{"Generate with count limit test", fun test_count_limit/0},
|
||||
{"Generate with until limit test", fun test_until_limit/0}
|
||||
].
|
||||
|
||||
test_parse_rrule() ->
|
||||
RRule = #{
|
||||
<<"freq">> => <<"WEEKLY">>,
|
||||
<<"interval">> => 2,
|
||||
<<"until">> => <<"2026-12-31T00:00:00Z">>,
|
||||
<<"byday">> => [<<"MO">>, <<"WE">>, <<"FR">>]
|
||||
},
|
||||
|
||||
{ok, Parsed} = logic_recurrence:parse_rrule(RRule),
|
||||
?assertEqual(<<"WEEKLY">>, maps:get(freq, Parsed)),
|
||||
?assertEqual(2, maps:get(interval, Parsed)),
|
||||
?assertEqual(<<"2026-12-31T00:00:00Z">>, maps:get(until, Parsed)),
|
||||
?assertEqual([<<"MO">>, <<"WE">>, <<"FR">>], maps:get(byday, Parsed)).
|
||||
|
||||
test_validate_rrule() ->
|
||||
ValidRule = #{
|
||||
freq => <<"DAILY">>,
|
||||
interval => 1
|
||||
},
|
||||
?assert(logic_recurrence:validate_rrule(ValidRule)),
|
||||
|
||||
InvalidFreq = #{
|
||||
freq => <<"YEARLY">>,
|
||||
interval => 1
|
||||
},
|
||||
?assertNot(logic_recurrence:validate_rrule(InvalidFreq)),
|
||||
|
||||
InvalidInterval = #{
|
||||
freq => <<"DAILY">>,
|
||||
interval => 0
|
||||
},
|
||||
?assertNot(logic_recurrence:validate_rrule(InvalidInterval)).
|
||||
|
||||
test_daily_occurrences() ->
|
||||
StartTime = {{2026, 4, 20}, {10, 0, 0}},
|
||||
RRule = #{
|
||||
freq => <<"DAILY">>,
|
||||
interval => 1
|
||||
},
|
||||
RangeEnd = {{2026, 4, 25}, {10, 0, 0}},
|
||||
|
||||
Occurrences = logic_recurrence:generate_occurrences(StartTime, RRule, RangeEnd),
|
||||
?assertEqual(6, length(Occurrences)),
|
||||
?assertEqual({{2026, 4, 20}, {10, 0, 0}}, lists:nth(1, Occurrences)),
|
||||
?assertEqual({{2026, 4, 25}, {10, 0, 0}}, lists:nth(6, Occurrences)).
|
||||
|
||||
test_weekly_occurrences() ->
|
||||
StartTime = {{2026, 4, 20}, {10, 0, 0}}, % Monday
|
||||
RRule = #{
|
||||
freq => <<"WEEKLY">>,
|
||||
interval => 1
|
||||
},
|
||||
RangeEnd = {{2026, 5, 11}, {10, 0, 0}},
|
||||
|
||||
Occurrences = logic_recurrence:generate_occurrences(StartTime, RRule, RangeEnd),
|
||||
?assertEqual(4, length(Occurrences)),
|
||||
?assertEqual({{2026, 4, 20}, {10, 0, 0}}, lists:nth(1, Occurrences)),
|
||||
?assertEqual({{2026, 5, 11}, {10, 0, 0}}, lists:nth(4, Occurrences)).
|
||||
|
||||
test_monthly_occurrences() ->
|
||||
StartTime = {{2026, 4, 20}, {10, 0, 0}},
|
||||
RRule = #{
|
||||
freq => <<"MONTHLY">>,
|
||||
interval => 1
|
||||
},
|
||||
RangeEnd = {{2026, 7, 20}, {10, 0, 0}},
|
||||
|
||||
Occurrences = logic_recurrence:generate_occurrences(StartTime, RRule, RangeEnd),
|
||||
?assertEqual(4, length(Occurrences)),
|
||||
?assertEqual({{2026, 4, 20}, {10, 0, 0}}, lists:nth(1, Occurrences)),
|
||||
?assertEqual({{2026, 7, 20}, {10, 0, 0}}, lists:nth(4, Occurrences)).
|
||||
|
||||
test_count_limit() ->
|
||||
StartTime = {{2026, 4, 20}, {10, 0, 0}},
|
||||
RRule = #{
|
||||
freq => <<"DAILY">>,
|
||||
interval => 1,
|
||||
count => 3
|
||||
},
|
||||
RangeEnd = {{2026, 12, 31}, {10, 0, 0}},
|
||||
|
||||
Occurrences = logic_recurrence:generate_occurrences(StartTime, RRule, RangeEnd),
|
||||
?assertEqual(3, length(Occurrences)).
|
||||
|
||||
test_until_limit() ->
|
||||
StartTime = {{2026, 4, 20}, {10, 0, 0}},
|
||||
RRule = #{
|
||||
freq => <<"DAILY">>,
|
||||
interval => 1,
|
||||
until => <<"2026-04-22T10:00:00Z">>
|
||||
},
|
||||
RangeEnd = {{2026, 12, 31}, {10, 0, 0}},
|
||||
|
||||
Occurrences = logic_recurrence:generate_occurrences(StartTime, RRule, RangeEnd),
|
||||
% 20, 21, 22 апреля = 3 дня
|
||||
?assertEqual(3, length(Occurrences)).
|
||||
Reference in New Issue
Block a user