141 lines
5.5 KiB
Erlang
141 lines
5.5 KiB
Erlang
-module(logic_event_tests).
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
-include("records.hrl").
|
|
|
|
-define(TABLES, [user, calendar, event, calendar_specialist, subscription, calendar_share]).
|
|
|
|
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},
|
|
{"Persist specialist_id on update", fun test_specialist_id_persist/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}.
|
|
|
|
create_commercial_with_specialist() ->
|
|
OwnerId = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}),
|
|
SpecId = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}),
|
|
Owner = #user{
|
|
id = OwnerId, email = <<"owner@ex.com">>, password_hash = <<"hash">>,
|
|
role = user, status = active,
|
|
created_at = calendar:universal_time(), updated_at = calendar:universal_time()
|
|
},
|
|
Spec = #user{
|
|
id = SpecId, email = <<"spec@ex.com">>, password_hash = <<"hash">>,
|
|
role = user, status = active,
|
|
created_at = calendar:universal_time(), updated_at = calendar:universal_time()
|
|
},
|
|
mnesia:dirty_write(Owner),
|
|
mnesia:dirty_write(Spec),
|
|
{ok, _} = logic_subscription:start_trial(OwnerId),
|
|
{ok, Cal} = logic_calendar:create_calendar(OwnerId, <<"Studio">>, <<>>, manual, commercial),
|
|
{ok, _} = core_calendar_specialist:create(Cal#calendar.id, SpecId, <<"Spec">>, []),
|
|
{OwnerId, SpecId, Cal#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(),
|
|
|
|
ClosedMonth = {{2020, 1, 1}, {10, 0, 0}},
|
|
{error, archived} = logic_event:create_event(UserId, CalendarId, <<"Closed">>, ClosedMonth, 60),
|
|
|
|
NowSec = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
|
|
PastInHot = calendar:gregorian_seconds_to_datetime(NowSec - 3600),
|
|
{error, event_in_past} = logic_event:create_event(UserId, CalendarId, <<"Past">>, PastInHot, 60),
|
|
|
|
FutureTime = eh_test_support:future_start(),
|
|
?assertEqual(ok, logic_event:validate_event_time(FutureTime)).
|
|
|
|
test_specialist_id_persist() ->
|
|
{OwnerId, SpecId, CalendarId} = create_commercial_with_specialist(),
|
|
StartTime = eh_test_support:future_start(),
|
|
{ok, Event} = logic_event:create_event(OwnerId, CalendarId, <<"Slot">>, StartTime, 60),
|
|
?assertEqual(<<>>, Event#event.specialist_id),
|
|
{ok, Updated} = logic_event:update_event(OwnerId, Event#event.id, [{specialist_id, SpecId}]),
|
|
?assertEqual(SpecId, Updated#event.specialist_id),
|
|
BadSpec = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}),
|
|
?assertMatch({error, invalid_specialist},
|
|
logic_event:update_event(OwnerId, Event#event.id, [{specialist_id, BadSpec}])).
|