Files
EventHubBack/test/core_booking_tests.erl

126 lines
3.9 KiB
Erlang

-module(core_booking_tests).
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
setup() ->
mnesia:start(),
mnesia:create_table(booking, [
{attributes, record_info(fields, booking)},
{ram_copies, [node()]}
]),
ok.
cleanup(_) ->
mnesia:delete_table(booking),
mnesia:stop(),
ok.
core_booking_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
[
{"Create booking test", fun test_create_booking/0},
{"Get booking by id test", fun test_get_by_id/0},
{"Get booking by event and user test", fun test_get_by_event_and_user/0},
{"List bookings by event test", fun test_list_by_event/0},
{"List bookings by user test", fun test_list_by_user/0},
{"Update booking status test", fun test_update_status/0},
{"Delete booking test", fun test_delete_booking/0}
]}.
test_create_booking() ->
EventId = <<"event123">>,
UserId = <<"user123">>,
{ok, Booking} = core_booking:create(EventId, UserId),
?assertEqual(EventId, Booking#booking.event_id),
?assertEqual(UserId, Booking#booking.user_id),
?assertEqual(pending, Booking#booking.status),
?assertEqual(undefined, Booking#booking.confirmed_at),
?assert(is_binary(Booking#booking.id)),
?assert(Booking#booking.created_at =/= undefined),
?assert(Booking#booking.updated_at =/= undefined).
test_get_by_id() ->
EventId = <<"event123">>,
UserId = <<"user123">>,
{ok, Booking} = core_booking:create(EventId, UserId),
{ok, Found} = core_booking:get_by_id(Booking#booking.id),
?assertEqual(Booking#booking.id, Found#booking.id),
{error, not_found} = core_booking:get_by_id(<<"nonexistent">>).
test_get_by_event_and_user() ->
EventId = <<"event123">>,
UserId1 = <<"user1">>,
UserId2 = <<"user2">>,
{ok, Booking1} = core_booking:create(EventId, UserId1),
{ok, _Booking2} = core_booking:create(EventId, UserId2),
{ok, Found} = core_booking:get_by_event_and_user(EventId, UserId1),
?assertEqual(Booking1#booking.id, Found#booking.id),
{error, not_found} = core_booking:get_by_event_and_user(EventId, <<"user3">>).
test_list_by_event() ->
EventId1 = <<"event1">>,
EventId2 = <<"event2">>,
UserId = <<"user123">>,
{ok, _} = core_booking:create(EventId1, UserId),
{ok, _} = core_booking:create(EventId1, <<"user2">>),
{ok, _} = core_booking:create(EventId2, UserId),
{ok, Bookings1} = core_booking:list_by_event(EventId1),
?assertEqual(2, length(Bookings1)),
{ok, Bookings2} = core_booking:list_by_event(EventId2),
?assertEqual(1, length(Bookings2)).
test_list_by_user() ->
EventId = <<"event123">>,
UserId1 = <<"user1">>,
UserId2 = <<"user2">>,
{ok, _} = core_booking:create(EventId, UserId1),
{ok, _} = core_booking:create(EventId, UserId1),
{ok, _} = core_booking:create(EventId, UserId2),
{ok, Bookings1} = core_booking:list_by_user(UserId1),
?assertEqual(2, length(Bookings1)),
{ok, Bookings2} = core_booking:list_by_user(UserId2),
?assertEqual(1, length(Bookings2)).
test_update_status() ->
EventId = <<"event123">>,
UserId = <<"user123">>,
{ok, Booking} = core_booking:create(EventId, UserId),
timer:sleep(2000), % 2 секунды
{ok, Confirmed} = core_booking:update_status(Booking#booking.id, confirmed),
?assertEqual(confirmed, Confirmed#booking.status),
?assert(Confirmed#booking.confirmed_at =/= undefined),
?assert(Confirmed#booking.updated_at > Booking#booking.updated_at),
timer:sleep(2000), % 2 секунды
{ok, Cancelled} = core_booking:update_status(Booking#booking.id, cancelled),
?assertEqual(cancelled, Cancelled#booking.status),
?assert(Cancelled#booking.updated_at > Confirmed#booking.updated_at),
{error, not_found} = core_booking:update_status(<<"nonexistent">>, confirmed).
test_delete_booking() ->
EventId = <<"event123">>,
UserId = <<"user123">>,
{ok, Booking} = core_booking:create(EventId, UserId),
{ok, deleted} = core_booking:delete(Booking#booking.id),
{error, not_found} = core_booking:get_by_id(Booking#booking.id).