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
+23 -35
View File
@@ -1,49 +1,37 @@
-module(admin_handler_health_tests).
-include_lib("eunit/include/eunit.hrl").
%% ------------------------------------------------------------------
%% Фикстуры
%% ------------------------------------------------------------------
setup() ->
ok = meck:new(cowboy_req, [non_strict]),
ok.
cleanup(_) ->
meck:unload(cowboy_req).
eh_test_support:unload_cowboy(),
ok.
%% ------------------------------------------------------------------
%% Тесты
%% ------------------------------------------------------------------
admin_handler_health_test_() ->
{setup, fun setup/0, fun cleanup/1, [
{"GET /admin/health returns 200 with status ok",
fun test_health_get/0},
{"POST /admin/health returns 405 Method not allowed",
fun test_health_post/0}
admin_health_test_() ->
{foreach, fun setup/0, fun cleanup/1, [
{"GET health success", fun test_health_get/0},
{"POST health method not allowed", fun test_health_wrong_method/0}
]}.
%% ── Успешный GET ─────────────────────────────────────────────
test_health_get() ->
% Мокаем method → <<"GET">>
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
% reply/4 будем перехватывать
ok = meck:expect(cowboy_req, reply,
fun(Code, Headers, Body, Req) ->
put(test_reply, {Code, Headers, Body, Req})
end),
{ok, _, _} = admin_handler_health:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
{Status, _, Body} = eh_test_support:call(admin_handler_health, #{
method => <<"GET">>,
path => <<"/admin/health">>,
auth => none
}),
?assertEqual(200, Status),
?assertEqual(#{<<"status">> => <<"ok">>}, jsx:decode(RespBody, [return_maps])).
Result = jsx:decode(Body, [return_maps]),
?assertEqual(<<"ok">>, maps:get(<<"status">>, Result)),
?assertEqual(<<"eventhub">>, maps:get(<<"service">>, Result)),
?assert(is_map_key(<<"version">>, Result)),
?assert(is_map_key(<<"git_sha">>, Result)).
%% ── Метод не разрешён ───────────────────────────────────────
test_health_post() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"POST">> end),
ok = meck:expect(cowboy_req, reply,
fun(Code, Headers, Body, Req) ->
put(test_reply, {Code, Headers, Body, Req})
end),
{ok, _, _} = admin_handler_health:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
test_health_wrong_method() ->
{Status, _, Body} = eh_test_support:call(admin_handler_health, #{
method => <<"POST">>,
path => <<"/admin/health">>,
auth => none
}),
?assertEqual(405, Status),
?assertEqual(#{<<"error">> => <<"Method not allowed">>}, jsx:decode(RespBody, [return_maps])).
#{<<"error">> := <<"Method not allowed">>} = jsx:decode(Body, [return_maps]).