Stage 3.2 + tests
This commit is contained in:
108
test/logic_event_tests.erl
Normal file
108
test/logic_event_tests.erl
Normal file
@@ -0,0 +1,108 @@
|
||||
-module(logic_event_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
setup() ->
|
||||
mnesia:start(),
|
||||
mnesia:create_table(user, [
|
||||
{attributes, record_info(fields, user)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
mnesia:create_table(calendar, [
|
||||
{attributes, record_info(fields, calendar)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
mnesia:create_table(event, [
|
||||
{attributes, record_info(fields, event)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(event),
|
||||
mnesia:delete_table(calendar),
|
||||
mnesia:delete_table(user),
|
||||
mnesia:stop(),
|
||||
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">>, <<"">>),
|
||||
{UserId, Calendar#calendar.id}.
|
||||
|
||||
test_create_event() ->
|
||||
{UserId, CalendarId} = create_test_user_and_calendar(),
|
||||
Title = <<"Test Event">>,
|
||||
StartTime = {{2026, 5, 1}, {10, 0, 0}},
|
||||
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(),
|
||||
{ok, Event} = logic_event:create_event(UserId, CalendarId, <<"Test">>, {{2026, 5, 1}, {10, 0, 0}}, 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(),
|
||||
{ok, _} = logic_event:create_event(UserId, CalendarId, <<"Event 1">>, {{2026, 5, 1}, {10, 0, 0}}, 60),
|
||||
{ok, _} = logic_event:create_event(UserId, CalendarId, <<"Event 2">>, {{2026, 5, 2}, {11, 0, 0}}, 90),
|
||||
|
||||
{ok, Events} = logic_event:list_events(UserId, CalendarId),
|
||||
?assertEqual(2, length(Events)).
|
||||
|
||||
test_update_event() ->
|
||||
{UserId, CalendarId} = create_test_user_and_calendar(),
|
||||
{ok, Event} = logic_event:create_event(UserId, CalendarId, <<"Original">>, {{2026, 5, 1}, {10, 0, 0}}, 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(),
|
||||
{ok, Event} = logic_event:create_event(UserId, CalendarId, <<"Test">>, {{2026, 5, 1}, {10, 0, 0}}, 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 = {{2030, 1, 1}, {10, 0, 0}},
|
||||
?assertEqual(ok, logic_event:validate_event_time(FutureTime)).
|
||||
Reference in New Issue
Block a user