102 lines
3.4 KiB
Erlang
102 lines
3.4 KiB
Erlang
-module(core_event_tests).
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
-include("records.hrl").
|
|
|
|
-define(TABLES, [event]).
|
|
|
|
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_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}
|
|
]}.
|
|
|
|
add_days(DateTime, Days) ->
|
|
Sec = calendar:datetime_to_gregorian_seconds(DateTime) + Days * 86400,
|
|
calendar:gregorian_seconds_to_datetime(Sec).
|
|
|
|
test_create_event() ->
|
|
CalendarId = <<"calendar123">>,
|
|
Title = <<"Test Event">>,
|
|
StartTime = eh_test_support:future_start(),
|
|
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">>, eh_test_support:future_start(), 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">>,
|
|
Base = eh_test_support:future_start(),
|
|
|
|
{ok, _} = core_event:create(CalendarId1, <<"Event 1">>, Base, 60),
|
|
{ok, _} = core_event:create(CalendarId1, <<"Event 2">>, add_days(Base, 1), 90),
|
|
{ok, _} = core_event:create(CalendarId2, <<"Event 3">>, add_days(Base, 2), 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">>, eh_test_support:future_start(), 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">>, eh_test_support:future_start(), 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)).
|