Stage 10 final
This commit is contained in:
90
test/unit/core_calendar_tests.erl
Normal file
90
test/unit/core_calendar_tests.erl
Normal file
@@ -0,0 +1,90 @@
|
||||
-module(core_calendar_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
setup() ->
|
||||
mnesia:start(),
|
||||
mnesia:create_table(calendar, [
|
||||
{attributes, record_info(fields, calendar)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(calendar),
|
||||
mnesia:stop(),
|
||||
ok.
|
||||
|
||||
core_calendar_test_() ->
|
||||
{foreach,
|
||||
fun setup/0,
|
||||
fun cleanup/1,
|
||||
[
|
||||
{"Create calendar test", fun test_create_calendar/0},
|
||||
{"Get calendar by id test", fun test_get_by_id/0},
|
||||
{"List calendars by owner test", fun test_list_by_owner/0},
|
||||
{"Update calendar test", fun test_update_calendar/0},
|
||||
{"Delete calendar test", fun test_delete_calendar/0}
|
||||
]}.
|
||||
|
||||
test_create_calendar() ->
|
||||
OwnerId = <<"owner123">>,
|
||||
Title = <<"Test Calendar">>,
|
||||
Description = <<"Test Description">>,
|
||||
Confirmation = auto,
|
||||
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, Title, Description, Confirmation),
|
||||
|
||||
?assertEqual(OwnerId, Calendar#calendar.owner_id),
|
||||
?assertEqual(Title, Calendar#calendar.title),
|
||||
?assertEqual(Description, Calendar#calendar.description),
|
||||
?assertEqual(personal, Calendar#calendar.type),
|
||||
?assertEqual(Confirmation, Calendar#calendar.confirmation),
|
||||
?assertEqual(active, Calendar#calendar.status),
|
||||
?assert(is_binary(Calendar#calendar.id)),
|
||||
?assert(Calendar#calendar.created_at =/= undefined),
|
||||
?assert(Calendar#calendar.updated_at =/= undefined).
|
||||
|
||||
test_get_by_id() ->
|
||||
OwnerId = <<"owner123">>,
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, <<"Test">>, <<"Desc">>, manual),
|
||||
|
||||
{ok, Found} = core_calendar:get_by_id(Calendar#calendar.id),
|
||||
?assertEqual(Calendar#calendar.id, Found#calendar.id),
|
||||
|
||||
{error, not_found} = core_calendar:get_by_id(<<"nonexistent">>).
|
||||
|
||||
test_list_by_owner() ->
|
||||
OwnerId = <<"owner123">>,
|
||||
OtherOwner = <<"other456">>,
|
||||
|
||||
{ok, _} = core_calendar:create(OwnerId, <<"Calendar 1">>, <<"">>, manual),
|
||||
{ok, _} = core_calendar:create(OwnerId, <<"Calendar 2">>, <<"">>, auto),
|
||||
{ok, _} = core_calendar:create(OtherOwner, <<"Other Calendar">>, <<"">>, manual),
|
||||
|
||||
{ok, Calendars} = core_calendar:list_by_owner(OwnerId),
|
||||
?assertEqual(2, length(Calendars)).
|
||||
|
||||
test_update_calendar() ->
|
||||
OwnerId = <<"owner123">>,
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, <<"Original">>, <<"">>, manual),
|
||||
timer:sleep(2000),
|
||||
Updates = [{title, <<"Updated">>}, {description, <<"New Desc">>}, {confirmation, auto}],
|
||||
{ok, Updated} = core_calendar:update(Calendar#calendar.id, Updates),
|
||||
|
||||
?assertEqual(<<"Updated">>, Updated#calendar.title),
|
||||
?assertEqual(<<"New Desc">>, Updated#calendar.description),
|
||||
?assertEqual(auto, Updated#calendar.confirmation),
|
||||
?assert(Updated#calendar.updated_at > Calendar#calendar.updated_at),
|
||||
|
||||
{error, not_found} = core_calendar:update(<<"nonexistent">>, Updates).
|
||||
|
||||
test_delete_calendar() ->
|
||||
OwnerId = <<"owner123">>,
|
||||
{ok, Calendar} = core_calendar:create(OwnerId, <<"Test">>, <<"">>, manual),
|
||||
|
||||
{ok, Deleted} = core_calendar:delete(Calendar#calendar.id),
|
||||
?assertEqual(deleted, Deleted#calendar.status),
|
||||
|
||||
{ok, ActiveCalendars} = core_calendar:list_by_owner(OwnerId),
|
||||
?assertEqual(0, length(ActiveCalendars)).
|
||||
Reference in New Issue
Block a user