-module(core_booking_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). -define(TABLES, [booking]). setup() -> eh_test_support:start_mnesia(), eh_test_support:ensure_tables(?TABLES), ok. cleanup(_) -> 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} ]}. test_create_booking() -> EventId = <<"event123">>, UserId = <<"user123">>, {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), ?assert(is_binary(Booking#booking.id)), ?assert(Booking#booking.created_at =/= undefined), ?assert(Booking#booking.updated_at =/= undefined). test_get_by_id() -> {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">>, {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() -> {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(<<"event2">>), ?assertEqual(1, length(Bookings2)). test_list_by_user() -> {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(<<"user2">>), ?assertEqual(1, length(Bookings2)). test_update_status() -> {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), ?assertEqual(Now, Confirmed#booking.confirmed_at), {ok, Cancelled} = core_booking:update(Booking#booking.id, [{status, cancelled}]), ?assertEqual(cancelled, Cancelled#booking.status), {error, not_found} = core_booking:update(<<"nonexistent">>, [{status, confirmed}]). test_delete_booking() -> {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).