-module(core_calendar_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). %% Setup и cleanup setup() -> mnesia:start(), mnesia:create_table(calendar, [ {attributes, record_info(fields, calendar)}, {ram_copies, [node()]} ]), ok. cleanup(_) -> mnesia:delete_table(calendar), mnesia:stop(), ok. %% Группа тестов core_calendar_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"Create calendar test", fun test_create_calendar/0}, {"Get calendar by id test", fun test_get_by_id/0}, {"List calendars by owner test", fun test_list_by_owner/0}, {"Update calendar test", fun test_update_calendar/0}, {"Delete calendar test", fun test_delete_calendar/0} ]}. %% Тесты test_create_calendar() -> OwnerId = <<"owner123">>, Title = <<"Test Calendar">>, Description = <<"Test Description">>, {ok, Calendar} = core_calendar:create(OwnerId, Title, Description), ?assertEqual(OwnerId, Calendar#calendar.owner_id), ?assertEqual(Title, Calendar#calendar.title), ?assertEqual(Description, Calendar#calendar.description), ?assertEqual(personal, Calendar#calendar.type), ?assertEqual(active, Calendar#calendar.status), ?assert(is_binary(Calendar#calendar.id)), ?assert(Calendar#calendar.created_at =/= undefined), ?assert(Calendar#calendar.updated_at =/= undefined). test_get_by_id() -> OwnerId = <<"owner123">>, {ok, Calendar} = core_calendar:create(OwnerId, <<"Test">>, <<"Desc">>), {ok, Found} = core_calendar:get_by_id(Calendar#calendar.id), ?assertEqual(Calendar#calendar.id, Found#calendar.id), {error, not_found} = core_calendar:get_by_id(<<"nonexistent">>). test_list_by_owner() -> OwnerId = <<"owner123">>, OtherOwner = <<"other456">>, {ok, _} = core_calendar:create(OwnerId, <<"Calendar 1">>, <<"">>), {ok, _} = core_calendar:create(OwnerId, <<"Calendar 2">>, <<"">>), {ok, _} = core_calendar:create(OtherOwner, <<"Other Calendar">>, <<"">>), {ok, Calendars} = core_calendar:list_by_owner(OwnerId), ?assertEqual(2, length(Calendars)). test_update_calendar() -> OwnerId = <<"owner123">>, {ok, Calendar} = core_calendar:create(OwnerId, <<"Original">>, <<"">>), timer:sleep(2000), Updates = [{title, <<"Updated">>}, {description, <<"New Desc">>}], {ok, Updated} = core_calendar:update(Calendar#calendar.id, Updates), ?assertEqual(<<"Updated">>, Updated#calendar.title), ?assertEqual(<<"New Desc">>, Updated#calendar.description), ?assert(Updated#calendar.updated_at > Calendar#calendar.updated_at), {error, not_found} = core_calendar:update(<<"nonexistent">>, Updates). test_delete_calendar() -> OwnerId = <<"owner123">>, {ok, Calendar} = core_calendar:create(OwnerId, <<"Test">>, <<"">>), {ok, Deleted} = core_calendar:delete(Calendar#calendar.id), ?assertEqual(deleted, Deleted#calendar.status), % Удалённый календарь не возвращается в списке активных {ok, ActiveCalendars} = core_calendar:list_by_owner(OwnerId), ?assertEqual(0, length(ActiveCalendars)).