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:
2026-07-17 15:16:46 +03:00
parent 9d2780fef4
commit 4fa802702f
54 changed files with 4240 additions and 2281 deletions
@@ -0,0 +1,76 @@
-module(admin_handler_reviews_stats_tests).
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
-define(TABLES, [admin, admin_audit, review, ticket]).
-define(ADMIN_ID, <<"adm_test_1">>).
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_reviews_stats_test_() ->
{foreach, fun setup/0, fun cleanup/1, [
{"GET stats empty", {timeout, 60, fun test_stats_empty/0}},
{"GET stats with reviews", {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_reviews_stats, #{
method => <<"GET">>,
path => <<"/v1/admin/reviews/stats">>,
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(0, maps:get(<<"total_reviews">>, Result)),
?assertEqual(#{}, maps:get(<<"reviews_by_target_type">>, Result)),
?assertEqual(#{}, maps:get(<<"reviews_by_status">>, Result)),
?assertEqual([], maps:get(<<"top_targets_by_reviews">>, Result)).
test_stats_with_data() ->
{ok, _} = core_review:create(<<"u1">>, calendar, <<"cal1">>, 5, <<"great">>),
{ok, _} = core_review:create(<<"u2">>, event, <<"ev1">>, 1, <<"bad">>),
{Status, _, Body} = eh_test_support:call(admin_handler_reviews_stats, #{
method => <<"GET">>,
path => <<"/v1/admin/reviews/stats">>,
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(2, maps:get(<<"total_reviews">>, Result)),
ByType = maps:get(<<"reviews_by_target_type">>, Result),
?assertEqual(1, maps:get(<<"calendar">>, ByType)),
?assertEqual(1, maps:get(<<"event">>, ByType)),
ByStatus = maps:get(<<"reviews_by_status">>, Result),
?assertEqual(2, maps:get(<<"visible">>, ByStatus)),
?assert(length(maps:get(<<"top_targets_by_reviews">>, Result)) >= 1),
?assert(length(maps:get(<<"top_targets_by_positive_reviews">>, Result)) >= 1),
?assert(length(maps:get(<<"top_targets_by_negative_reviews">>, Result)) >= 1).
test_unauthorized() ->
{Status, _, _} = eh_test_support:call(admin_handler_reviews_stats, #{
method => <<"GET">>,
path => <<"/v1/admin/reviews/stats">>,
auth => none
}),
?assertEqual(401, Status).
test_wrong_method() ->
{Status, _, _} = eh_test_support:call(admin_handler_reviews_stats, #{
method => <<"POST">>,
path => <<"/v1/admin/reviews/stats">>,
auth => ?ADMIN_ID
}),
?assertEqual(405, Status).