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,32 +2,16 @@
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-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.
|
||||
|
||||
booking_integration_test_() ->
|
||||
@@ -35,7 +19,7 @@ booking_integration_test_() ->
|
||||
fun setup/0,
|
||||
fun cleanup/1,
|
||||
[
|
||||
{"Full booking flow with auto confirmation", fun test_auto_booking_flow/0},
|
||||
{"Booking create stays pending", fun test_pending_booking_flow/0},
|
||||
{"Full booking flow with manual confirmation", fun test_manual_booking_flow/0},
|
||||
{"Capacity management test", fun test_capacity_management/0},
|
||||
{"Multiple bookings test", fun test_multiple_bookings/0}
|
||||
@@ -55,22 +39,26 @@ create_user() ->
|
||||
mnesia:dirty_write(User),
|
||||
UserId.
|
||||
|
||||
test_auto_booking_flow() ->
|
||||
add_days(DateTime, Days) ->
|
||||
Sec = calendar:datetime_to_gregorian_seconds(DateTime) + Days * 86400,
|
||||
calendar:gregorian_seconds_to_datetime(Sec).
|
||||
|
||||
test_pending_booking_flow() ->
|
||||
OwnerId = create_user(),
|
||||
ParticipantId = create_user(),
|
||||
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, <<"Auto">>, <<"">>, auto),
|
||||
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
{ok, Event} = core_event:create(Calendar#calendar.id, <<"Event">>, StartTime, 60),
|
||||
|
||||
{ok, Booking} = logic_booking:create_booking(ParticipantId, Event#event.id),
|
||||
timer:sleep(100),
|
||||
?assertEqual(pending, Booking#booking.status),
|
||||
|
||||
{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),
|
||||
|
||||
{ok, EventBookings} = logic_booking:list_event_bookings(OwnerId, Event#event.id),
|
||||
{ok, EventBookings} = logic_booking:list_event_bookings(Event#event.id),
|
||||
?assertEqual(1, length(EventBookings)),
|
||||
|
||||
{ok, UserBookings} = logic_booking:list_user_bookings(ParticipantId),
|
||||
@@ -82,7 +70,7 @@ test_manual_booking_flow() ->
|
||||
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, <<"Manual">>, <<"">>, manual),
|
||||
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
{ok, Event} = core_event:create(Calendar#calendar.id, <<"Event">>, StartTime, 60),
|
||||
|
||||
{ok, Booking} = logic_booking:create_booking(ParticipantId, Event#event.id),
|
||||
@@ -91,7 +79,7 @@ test_manual_booking_flow() ->
|
||||
{ok, Confirmed} = logic_booking:confirm_booking(OwnerId, Booking#booking.id, confirm),
|
||||
?assertEqual(confirmed, Confirmed#booking.status),
|
||||
|
||||
{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_capacity_management() ->
|
||||
@@ -100,21 +88,21 @@ test_capacity_management() ->
|
||||
Participant2Id = create_user(),
|
||||
Participant3Id = create_user(),
|
||||
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, <<"Test">>, <<"">>, auto),
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, <<"Test">>, <<"">>, manual),
|
||||
|
||||
StartTime = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime = eh_test_support:future_start(),
|
||||
{ok, Event} = core_event:create(Calendar#calendar.id, <<"Event">>, StartTime, 60),
|
||||
{ok, _} = core_event:update(Event#event.id, [{capacity, 2}]),
|
||||
|
||||
{ok, Booking1} = logic_booking:create_booking(Participant1Id, Event#event.id),
|
||||
{ok, _Booking2} = logic_booking:create_booking(Participant2Id, Event#event.id),
|
||||
{ok, Booking2} = logic_booking:create_booking(Participant2Id, Event#event.id),
|
||||
{ok, _} = logic_booking:confirm_booking(OwnerId, Booking1#booking.id, confirm),
|
||||
{ok, _} = logic_booking:confirm_booking(OwnerId, Booking2#booking.id, confirm),
|
||||
|
||||
{error, event_full} = logic_booking:create_booking(Participant3Id, Event#event.id),
|
||||
{error, full} = logic_booking:create_booking(Participant3Id, Event#event.id),
|
||||
|
||||
% Участник 1 отменяет своё бронирование
|
||||
{ok, _} = logic_booking:cancel_booking(Participant1Id, Booking1#booking.id),
|
||||
{ok, _} = logic_booking:cancel_booking(Participant1Id, Booking1#booking.id, cancel),
|
||||
|
||||
% Теперь третий может записаться
|
||||
{ok, _} = logic_booking:create_booking(Participant3Id, Event#event.id).
|
||||
|
||||
test_multiple_bookings() ->
|
||||
@@ -123,9 +111,10 @@ test_multiple_bookings() ->
|
||||
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, <<"Test">>, <<"">>, manual),
|
||||
|
||||
StartTime1 = {{2026, 6, 1}, {10, 0, 0}},
|
||||
StartTime2 = {{2026, 6, 2}, {10, 0, 0}},
|
||||
StartTime3 = {{2026, 6, 3}, {10, 0, 0}},
|
||||
Base = eh_test_support:future_start(),
|
||||
StartTime1 = Base,
|
||||
StartTime2 = add_days(Base, 1),
|
||||
StartTime3 = add_days(Base, 2),
|
||||
|
||||
{ok, Event1} = core_event:create(Calendar#calendar.id, <<"Event1">>, StartTime1, 60),
|
||||
{ok, Event2} = core_event:create(Calendar#calendar.id, <<"Event2">>, StartTime2, 60),
|
||||
@@ -145,4 +134,4 @@ test_multiple_bookings() ->
|
||||
?assertEqual(2, ConfirmedCount),
|
||||
|
||||
PendingCount = length([B || B <- UserBookings, B#booking.status =:= pending]),
|
||||
?assertEqual(1, PendingCount).
|
||||
?assertEqual(1, PendingCount).
|
||||
|
||||
Reference in New Issue
Block a user