105 lines
3.6 KiB
Erlang
105 lines
3.6 KiB
Erlang
-module(logic_event_tests).
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
-include("records.hrl").
|
|
|
|
-define(TABLES, [user, calendar, 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.
|
|
|
|
logic_event_test_() ->
|
|
{foreach,
|
|
fun setup/0,
|
|
fun cleanup/1,
|
|
[
|
|
{"Create event test", fun test_create_event/0},
|
|
{"Get event test", fun test_get_event/0},
|
|
{"List events test", fun test_list_events/0},
|
|
{"Update event test", fun test_update_event/0},
|
|
{"Delete event test", fun test_delete_event/0},
|
|
{"Event time validation test", fun test_time_validation/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">>, <<"">>, manual),
|
|
{UserId, Calendar#calendar.id}.
|
|
|
|
add_days(DateTime, Days) ->
|
|
Sec = calendar:datetime_to_gregorian_seconds(DateTime) + Days * 86400,
|
|
calendar:gregorian_seconds_to_datetime(Sec).
|
|
|
|
test_create_event() ->
|
|
{UserId, CalendarId} = create_test_user_and_calendar(),
|
|
Title = <<"Test Event">>,
|
|
StartTime = eh_test_support:future_start(),
|
|
Duration = 60,
|
|
|
|
{ok, Event} = logic_event:create_event(UserId, CalendarId, Title, StartTime, Duration),
|
|
?assertEqual(CalendarId, Event#event.calendar_id),
|
|
?assertEqual(Title, Event#event.title),
|
|
?assertEqual(single, Event#event.event_type).
|
|
|
|
test_get_event() ->
|
|
{UserId, CalendarId} = create_test_user_and_calendar(),
|
|
StartTime = eh_test_support:future_start(),
|
|
{ok, Event} = logic_event:create_event(UserId, CalendarId, <<"Test">>, StartTime, 60),
|
|
|
|
{ok, Found} = logic_event:get_event(UserId, Event#event.id),
|
|
?assertEqual(Event#event.id, Found#event.id).
|
|
|
|
test_list_events() ->
|
|
{UserId, CalendarId} = create_test_user_and_calendar(),
|
|
Start1 = eh_test_support:future_start(),
|
|
Start2 = add_days(Start1, 1),
|
|
{ok, _} = logic_event:create_event(UserId, CalendarId, <<"Event 1">>, Start1, 60),
|
|
{ok, _} = logic_event:create_event(UserId, CalendarId, <<"Event 2">>, Start2, 90),
|
|
|
|
{ok, Events} = logic_event:list_events(UserId, CalendarId),
|
|
?assertEqual(2, length(Events)).
|
|
|
|
test_update_event() ->
|
|
{UserId, CalendarId} = create_test_user_and_calendar(),
|
|
StartTime = eh_test_support:future_start(),
|
|
{ok, Event} = logic_event:create_event(UserId, CalendarId, <<"Original">>, StartTime, 60),
|
|
|
|
Updates = [{title, <<"Updated">>}, {capacity, 50}],
|
|
{ok, Updated} = logic_event:update_event(UserId, Event#event.id, Updates),
|
|
?assertEqual(<<"Updated">>, Updated#event.title),
|
|
?assertEqual(50, Updated#event.capacity).
|
|
|
|
test_delete_event() ->
|
|
{UserId, CalendarId} = create_test_user_and_calendar(),
|
|
StartTime = eh_test_support:future_start(),
|
|
{ok, Event} = logic_event:create_event(UserId, CalendarId, <<"Test">>, StartTime, 60),
|
|
|
|
{ok, Deleted} = logic_event:delete_event(UserId, Event#event.id),
|
|
?assertEqual(deleted, Deleted#event.status).
|
|
|
|
test_time_validation() ->
|
|
{UserId, CalendarId} = create_test_user_and_calendar(),
|
|
|
|
PastTime = {{2020, 1, 1}, {10, 0, 0}},
|
|
{error, event_in_past} = logic_event:create_event(UserId, CalendarId, <<"Past">>, PastTime, 60),
|
|
|
|
FutureTime = eh_test_support:future_start(),
|
|
?assertEqual(ok, logic_event:validate_event_time(FutureTime)).
|