-module(core_event_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()]} ]), ok. cleanup(_) -> mnesia:delete_table(event), mnesia:stop(), ok. core_event_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"Create event test", fun test_create_event/0}, {"Get event by id test", fun test_get_by_id/0}, {"List events by calendar test", fun test_list_by_calendar/0}, {"Update event test", fun test_update_event/0}, {"Delete event test", fun test_delete_event/0} ]}. test_create_event() -> CalendarId = <<"calendar123">>, Title = <<"Test Event">>, StartTime = {{2026, 4, 25}, {10, 0, 0}}, Duration = 60, {ok, Event} = core_event:create(CalendarId, Title, StartTime, Duration), ?assertEqual(CalendarId, Event#event.calendar_id), ?assertEqual(Title, Event#event.title), ?assertEqual(StartTime, Event#event.start_time), ?assertEqual(Duration, Event#event.duration), ?assertEqual(single, Event#event.event_type), ?assertEqual(active, Event#event.status), ?assertEqual(false, Event#event.is_instance), ?assert(is_binary(Event#event.id)), ?assert(Event#event.created_at =/= undefined), ?assert(Event#event.updated_at =/= undefined). test_get_by_id() -> CalendarId = <<"calendar123">>, {ok, Event} = core_event:create(CalendarId, <<"Test">>, {{2026, 4, 25}, {10, 0, 0}}, 60), {ok, Found} = core_event:get_by_id(Event#event.id), ?assertEqual(Event#event.id, Found#event.id), {error, not_found} = core_event:get_by_id(<<"nonexistent">>). test_list_by_calendar() -> CalendarId1 = <<"calendar1">>, CalendarId2 = <<"calendar2">>, {ok, _} = core_event:create(CalendarId1, <<"Event 1">>, {{2026, 4, 25}, {10, 0, 0}}, 60), {ok, _} = core_event:create(CalendarId1, <<"Event 2">>, {{2026, 4, 26}, {11, 0, 0}}, 90), {ok, _} = core_event:create(CalendarId2, <<"Event 3">>, {{2026, 4, 27}, {12, 0, 0}}, 30), {ok, Events1} = core_event:list_by_calendar(CalendarId1), ?assertEqual(2, length(Events1)), {ok, Events2} = core_event:list_by_calendar(CalendarId2), ?assertEqual(1, length(Events2)), {ok, Events3} = core_event:list_by_calendar(<<"empty">>), ?assertEqual(0, length(Events3)). test_update_event() -> CalendarId = <<"calendar123">>, {ok, Event} = core_event:create(CalendarId, <<"Original">>, {{2026, 4, 25}, {10, 0, 0}}, 60), timer:sleep(2000), Updates = [{title, <<"Updated">>}, {capacity, 100}, {description, <<"New desc">>}], {ok, Updated} = core_event:update(Event#event.id, Updates), ?assertEqual(<<"Updated">>, Updated#event.title), ?assertEqual(100, Updated#event.capacity), ?assertEqual(<<"New desc">>, Updated#event.description), ?assert(Updated#event.updated_at > Event#event.updated_at), {error, not_found} = core_event:update(<<"nonexistent">>, Updates). test_delete_event() -> CalendarId = <<"calendar123">>, {ok, Event} = core_event:create(CalendarId, <<"Test">>, {{2026, 4, 25}, {10, 0, 0}}, 60), {ok, Deleted} = core_event:delete(Event#event.id), ?assertEqual(deleted, Deleted#event.status), % Удалённое событие не возвращается в списке активных {ok, ActiveEvents} = core_event:list_by_calendar(CalendarId), ?assertEqual(0, length(ActiveEvents)).