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.
|
||||
Reference in New Issue
Block a user