Rewrite and expand EUnit suite; run unit tests in CI from shared test image. Refs EventHub/EventHubBack#36 [skip ci]
This commit is contained in:
@@ -2,34 +2,16 @@
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(TEST_EMAIL, <<"test@example.com">>).
|
||||
-define(TABLES, [user, calendar, event, booking]).
|
||||
|
||||
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()]}
|
||||
]),
|
||||
mnesia:create_table(booking, [
|
||||
{attributes, record_info(fields, booking)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
eh_test_support:start_mnesia(),
|
||||
eh_test_support:ensure_tables(?TABLES),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(booking),
|
||||
mnesia:delete_table(event),
|
||||
mnesia:delete_table(calendar),
|
||||
mnesia:delete_table(user),
|
||||
mnesia:stop(),
|
||||
eh_test_support:delete_tables(?TABLES),
|
||||
eh_test_support:stop_mnesia(),
|
||||
ok.
|
||||
|
||||
logic_booking_test_() ->
|
||||
@@ -37,16 +19,15 @@ logic_booking_test_() ->
|
||||
fun setup/0,
|
||||
fun cleanup/1,
|
||||
[
|
||||
{"Create booking with auto confirmation", fun test_create_booking_auto/0},
|
||||
{"Create booking with manual confirmation", fun test_create_booking_manual/0},
|
||||
{"Create booking with timeout confirmation", fun test_create_booking_timeout/0},
|
||||
{"Create booking returns pending", fun test_create_booking_pending/0},
|
||||
{"Create duplicate booking", fun test_create_duplicate_booking/0},
|
||||
{"Create booking for inactive event", fun test_booking_inactive_event/0},
|
||||
{"Create booking for missing event", fun test_booking_missing_event/0},
|
||||
{"Create booking when event is full", fun test_booking_event_full/0},
|
||||
{"Confirm booking by owner", fun test_confirm_booking/0},
|
||||
{"Decline booking by owner", fun test_decline_booking/0},
|
||||
{"Pending bookings do not fill capacity", fun test_pending_does_not_fill/0},
|
||||
{"Confirm booking", fun test_confirm_booking/0},
|
||||
{"Confirm non-pending booking denied", fun test_confirm_non_pending/0},
|
||||
{"Cancel booking by participant", fun test_cancel_booking/0},
|
||||
{"Unauthorized confirm attempt", fun test_unauthorized_confirm/0},
|
||||
{"Cancel booking access denied", fun test_cancel_access_denied/0},
|
||||
{"List event bookings", fun test_list_event_bookings/0},
|
||||
{"List user bookings", fun test_list_user_bookings/0}
|
||||
]}.
|
||||
@@ -71,50 +52,28 @@ create_test_calendar(OwnerId, Confirmation) ->
|
||||
Calendar#calendar.id.
|
||||
|
||||
create_test_event(CalendarId) ->
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
{ok, Event} = core_event:create(CalendarId, <<"Test Event">>, StartTime, 60),
|
||||
Event#event.id.
|
||||
|
||||
create_test_event_with_capacity(CalendarId, Capacity) ->
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
{ok, Event} = core_event:create(CalendarId, <<"Test Event">>, StartTime, 60),
|
||||
{ok, Updated} = core_event:update(Event#event.id, [{capacity, Capacity}]),
|
||||
Updated#event.id.
|
||||
|
||||
%% Тесты
|
||||
test_create_booking_auto() ->
|
||||
test_create_booking_pending() ->
|
||||
OwnerId = create_test_user(user),
|
||||
ParticipantId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, auto),
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, Booking} = logic_booking:create_booking(ParticipantId, EventId),
|
||||
|
||||
timer:sleep(100),
|
||||
{ok, Updated} = core_booking:get_by_id(Booking#booking.id),
|
||||
?assertEqual(confirmed, Updated#booking.status).
|
||||
|
||||
test_create_booking_manual() ->
|
||||
OwnerId = create_test_user(user),
|
||||
ParticipantId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, manual),
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, Booking} = logic_booking:create_booking(ParticipantId, EventId),
|
||||
?assertEqual(pending, Booking#booking.status).
|
||||
|
||||
test_create_booking_timeout() ->
|
||||
OwnerId = create_test_user(user),
|
||||
ParticipantId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, {timeout, 1}),
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, Booking} = logic_booking:create_booking(ParticipantId, EventId),
|
||||
?assertEqual(pending, Booking#booking.status),
|
||||
|
||||
timer:sleep(1500),
|
||||
{ok, Updated} = core_booking:get_by_id(Booking#booking.id),
|
||||
?assertEqual(confirmed, Updated#booking.status).
|
||||
{ok, Stored} = core_booking:get_by_id(Booking#booking.id),
|
||||
?assertEqual(pending, Stored#booking.status).
|
||||
|
||||
test_create_duplicate_booking() ->
|
||||
OwnerId = create_test_user(user),
|
||||
@@ -125,25 +84,31 @@ test_create_duplicate_booking() ->
|
||||
{ok, _} = logic_booking:create_booking(ParticipantId, EventId),
|
||||
{error, already_booked} = logic_booking:create_booking(ParticipantId, EventId).
|
||||
|
||||
test_booking_inactive_event() ->
|
||||
OwnerId = create_test_user(user),
|
||||
test_booking_missing_event() ->
|
||||
ParticipantId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, manual),
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, _} = core_event:update(EventId, [{status, cancelled}]),
|
||||
|
||||
{error, event_not_active} = logic_booking:create_booking(ParticipantId, EventId).
|
||||
{error, not_found} = logic_booking:create_booking(ParticipantId, <<"nonexistent">>).
|
||||
|
||||
test_booking_event_full() ->
|
||||
OwnerId = create_test_user(user),
|
||||
Participant1Id = create_test_user(user),
|
||||
Participant2Id = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, auto),
|
||||
CalendarId = create_test_calendar(OwnerId, manual),
|
||||
EventId = create_test_event_with_capacity(CalendarId, 1),
|
||||
|
||||
{ok, B1} = logic_booking:create_booking(Participant1Id, EventId),
|
||||
{ok, _} = logic_booking:confirm_booking(Participant1Id, B1#booking.id, confirm),
|
||||
{error, full} = logic_booking:create_booking(Participant2Id, EventId).
|
||||
|
||||
test_pending_does_not_fill() ->
|
||||
OwnerId = create_test_user(user),
|
||||
Participant1Id = create_test_user(user),
|
||||
Participant2Id = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, manual),
|
||||
EventId = create_test_event_with_capacity(CalendarId, 1),
|
||||
|
||||
{ok, _} = logic_booking:create_booking(Participant1Id, EventId),
|
||||
{error, event_full} = logic_booking:create_booking(Participant2Id, EventId).
|
||||
{ok, B2} = logic_booking:create_booking(Participant2Id, EventId),
|
||||
?assertEqual(pending, B2#booking.status).
|
||||
|
||||
test_confirm_booking() ->
|
||||
OwnerId = create_test_user(user),
|
||||
@@ -155,15 +120,15 @@ test_confirm_booking() ->
|
||||
{ok, Confirmed} = logic_booking:confirm_booking(OwnerId, Booking#booking.id, confirm),
|
||||
?assertEqual(confirmed, Confirmed#booking.status).
|
||||
|
||||
test_decline_booking() ->
|
||||
test_confirm_non_pending() ->
|
||||
OwnerId = create_test_user(user),
|
||||
ParticipantId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, manual),
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, Booking} = logic_booking:create_booking(ParticipantId, EventId),
|
||||
{ok, Declined} = logic_booking:confirm_booking(OwnerId, Booking#booking.id, decline),
|
||||
?assertEqual(cancelled, Declined#booking.status).
|
||||
{ok, _} = logic_booking:confirm_booking(OwnerId, Booking#booking.id, confirm),
|
||||
{error, access_denied} = logic_booking:confirm_booking(OwnerId, Booking#booking.id, confirm).
|
||||
|
||||
test_cancel_booking() ->
|
||||
OwnerId = create_test_user(user),
|
||||
@@ -172,10 +137,10 @@ test_cancel_booking() ->
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, Booking} = logic_booking:create_booking(ParticipantId, EventId),
|
||||
{ok, Cancelled} = logic_booking:cancel_booking(ParticipantId, Booking#booking.id),
|
||||
{ok, Cancelled} = logic_booking:cancel_booking(ParticipantId, Booking#booking.id, cancel),
|
||||
?assertEqual(cancelled, Cancelled#booking.status).
|
||||
|
||||
test_unauthorized_confirm() ->
|
||||
test_cancel_access_denied() ->
|
||||
OwnerId = create_test_user(user),
|
||||
ParticipantId = create_test_user(user),
|
||||
OtherId = create_test_user(user),
|
||||
@@ -183,7 +148,7 @@ test_unauthorized_confirm() ->
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, Booking} = logic_booking:create_booking(ParticipantId, EventId),
|
||||
{error, access_denied} = logic_booking:confirm_booking(OtherId, Booking#booking.id, confirm).
|
||||
{error, access_denied} = logic_booking:cancel_booking(OtherId, Booking#booking.id, cancel).
|
||||
|
||||
test_list_event_bookings() ->
|
||||
OwnerId = create_test_user(user),
|
||||
@@ -195,7 +160,7 @@ test_list_event_bookings() ->
|
||||
{ok, _} = logic_booking:create_booking(Participant1Id, EventId),
|
||||
{ok, _} = logic_booking:create_booking(Participant2Id, EventId),
|
||||
|
||||
{ok, Bookings} = logic_booking:list_event_bookings(OwnerId, EventId),
|
||||
{ok, Bookings} = logic_booking:list_event_bookings(EventId),
|
||||
?assertEqual(2, length(Bookings)).
|
||||
|
||||
test_list_user_bookings() ->
|
||||
@@ -209,4 +174,4 @@ test_list_user_bookings() ->
|
||||
{ok, _} = logic_booking:create_booking(ParticipantId, EventId2),
|
||||
|
||||
{ok, Bookings} = logic_booking:list_user_bookings(ParticipantId),
|
||||
?assertEqual(2, length(Bookings)).
|
||||
?assertEqual(2, length(Bookings)).
|
||||
|
||||
Reference in New Issue
Block a user