84 lines
3.0 KiB
Erlang
84 lines
3.0 KiB
Erlang
-module(admin_handler_calendar_stats_tests).
|
||
-include_lib("eunit/include/eunit.hrl").
|
||
-include("records.hrl").
|
||
|
||
-define(TABLES, [admin, admin_audit, calendar, review, ticket]).
|
||
-define(ADMIN_ID, <<"adm_test_1">>).
|
||
-define(OWNER_ID, <<"owner_cal_stats">>).
|
||
|
||
setup() ->
|
||
eh_test_support:start_mnesia(),
|
||
eh_test_support:ensure_tables(?TABLES),
|
||
eh_test_support:ensure_jwt(),
|
||
eh_test_support:seed_admin(#{id => ?ADMIN_ID}),
|
||
ok.
|
||
|
||
cleanup(_) ->
|
||
eh_test_support:unload_cowboy(),
|
||
eh_test_support:delete_tables(?TABLES),
|
||
eh_test_support:stop_mnesia(),
|
||
ok.
|
||
|
||
admin_calendar_stats_test_() ->
|
||
{foreach, fun setup/0, fun cleanup/1, [
|
||
{"GET stats – empty", {timeout, 60, fun test_stats_empty/0}},
|
||
{"GET stats – with calendars", {timeout, 60, fun test_stats_with_data/0}},
|
||
{"GET – unauthorized", {timeout, 60, fun test_unauthorized/0}},
|
||
{"POST – method not allowed", {timeout, 60, fun test_wrong_method/0}}
|
||
]}.
|
||
|
||
test_stats_empty() ->
|
||
{Status, _, Body} = eh_test_support:call(admin_handler_calendar_stats, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/calendars/stats">>,
|
||
auth => ?ADMIN_ID
|
||
}),
|
||
?assertEqual(200, Status),
|
||
Result = jsx:decode(Body, [return_maps]),
|
||
?assertEqual(0, maps:get(<<"total_calendars">>, Result)),
|
||
?assertEqual(#{}, maps:get(<<"calendars_by_type">>, Result)),
|
||
?assertEqual(#{}, maps:get(<<"calendars_by_status">>, Result)),
|
||
?assertEqual([], maps:get(<<"top_calendars_by_reviews">>, Result)),
|
||
?assertEqual([], maps:get(<<"top_calendars_by_rating">>, Result)).
|
||
|
||
test_stats_with_data() ->
|
||
{ok, C1} = core_calendar:create(?OWNER_ID, <<"Personal">>, <<"d">>, auto, personal),
|
||
{ok, _} = core_calendar:create(?OWNER_ID, <<"Shop">>, <<"d">>, manual, commercial),
|
||
{ok, _} = core_calendar:update(C1#calendar.id, [
|
||
{rating_avg, 4.5},
|
||
{rating_count, 3}
|
||
]),
|
||
{Status, _, Body} = eh_test_support:call(admin_handler_calendar_stats, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/calendars/stats">>,
|
||
auth => ?ADMIN_ID
|
||
}),
|
||
?assertEqual(200, Status),
|
||
Result = jsx:decode(Body, [return_maps]),
|
||
?assertEqual(2, maps:get(<<"total_calendars">>, Result)),
|
||
ByType = maps:get(<<"calendars_by_type">>, Result),
|
||
?assertEqual(1, maps:get(<<"personal">>, ByType)),
|
||
?assertEqual(1, maps:get(<<"commercial">>, ByType)),
|
||
ByStatus = maps:get(<<"calendars_by_status">>, Result),
|
||
?assertEqual(2, maps:get(<<"active">>, ByStatus)),
|
||
TopRating = maps:get(<<"top_calendars_by_rating">>, Result),
|
||
?assert(length(TopRating) >= 1),
|
||
[First | _] = TopRating,
|
||
?assertEqual(C1#calendar.id, maps:get(<<"id">>, First)).
|
||
|
||
test_unauthorized() ->
|
||
{Status, _, _} = eh_test_support:call(admin_handler_calendar_stats, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/calendars/stats">>,
|
||
auth => none
|
||
}),
|
||
?assertEqual(401, Status).
|
||
|
||
test_wrong_method() ->
|
||
{Status, _, _} = eh_test_support:call(admin_handler_calendar_stats, #{
|
||
method => <<"POST">>,
|
||
path => <<"/v1/admin/calendars/stats">>,
|
||
auth => ?ADMIN_ID
|
||
}),
|
||
?assertEqual(405, Status).
|