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,125 +2,84 @@
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(TABLES, [booking]).
|
||||
|
||||
setup() ->
|
||||
mnesia:start(),
|
||||
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:stop(),
|
||||
eh_test_support:delete_tables(?TABLES),
|
||||
eh_test_support:stop_mnesia(),
|
||||
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}
|
||||
]}.
|
||||
{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),
|
||||
|
||||
{ok, Booking} = core_booking:create(EventId, UserId, pending),
|
||||
?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, Booking} = core_booking:create(<<"event123">>, <<"user123">>, pending),
|
||||
{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),
|
||||
{ok, Booking1} = core_booking:create(EventId, <<"user1">>, pending),
|
||||
{ok, _} = core_booking:create(EventId, <<"user2">>, pending),
|
||||
{ok, Found} = core_booking:get_by_event_and_user(EventId, <<"user1">>),
|
||||
?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),
|
||||
{ok, _} = core_booking:create(<<"event1">>, <<"user123">>, pending),
|
||||
{ok, _} = core_booking:create(<<"event1">>, <<"user2">>, pending),
|
||||
{ok, _} = core_booking:create(<<"event2">>, <<"user123">>, pending),
|
||||
{ok, Bookings1} = core_booking:list_by_event(<<"event1">>),
|
||||
?assertEqual(2, length(Bookings1)),
|
||||
|
||||
{ok, Bookings2} = core_booking:list_by_event(EventId2),
|
||||
{ok, Bookings2} = core_booking:list_by_event(<<"event2">>),
|
||||
?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),
|
||||
{ok, _} = core_booking:create(<<"event123">>, <<"user1">>, pending),
|
||||
{ok, _} = core_booking:create(<<"event456">>, <<"user1">>, pending),
|
||||
{ok, _} = core_booking:create(<<"event123">>, <<"user2">>, pending),
|
||||
{ok, Bookings1} = core_booking:list_by_user(<<"user1">>),
|
||||
?assertEqual(2, length(Bookings1)),
|
||||
|
||||
{ok, Bookings2} = core_booking:list_by_user(UserId2),
|
||||
{ok, Bookings2} = core_booking:list_by_user(<<"user2">>),
|
||||
?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),
|
||||
{ok, Booking} = core_booking:create(<<"event123">>, <<"user123">>, pending),
|
||||
Now = calendar:universal_time(),
|
||||
{ok, Confirmed} = core_booking:update(Booking#booking.id,
|
||||
[{status, confirmed}, {confirmed_at, Now}]),
|
||||
?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(Now, Confirmed#booking.confirmed_at),
|
||||
{ok, Cancelled} = core_booking:update(Booking#booking.id, [{status, cancelled}]),
|
||||
?assertEqual(cancelled, Cancelled#booking.status),
|
||||
?assert(Cancelled#booking.updated_at > Confirmed#booking.updated_at),
|
||||
|
||||
{error, not_found} = core_booking:update_status(<<"nonexistent">>, confirmed).
|
||||
{error, not_found} = core_booking:update(<<"nonexistent">>, [{status, 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).
|
||||
{ok, Booking} = core_booking:create(<<"event123">>, <<"user123">>, pending),
|
||||
ok = core_booking:delete(Booking#booking.id),
|
||||
{error, not_found} = core_booking:get_by_id(Booking#booking.id).
|
||||
|
||||
Reference in New Issue
Block a user