Stage 10 final
This commit is contained in:
90
test/unit/core_subscription_tests.erl
Normal file
90
test/unit/core_subscription_tests.erl
Normal file
@@ -0,0 +1,90 @@
|
||||
-module(core_subscription_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
setup() ->
|
||||
mnesia:start(),
|
||||
mnesia:create_table(subscription, [
|
||||
{attributes, record_info(fields, subscription)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
mnesia:create_table(calendar, [
|
||||
{attributes, record_info(fields, calendar)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(calendar),
|
||||
mnesia:delete_table(subscription),
|
||||
mnesia:stop(),
|
||||
ok.
|
||||
|
||||
core_subscription_test_() ->
|
||||
{foreach,
|
||||
fun setup/0,
|
||||
fun cleanup/1,
|
||||
[
|
||||
{"Create trial subscription", fun test_create_trial/0},
|
||||
{"Create paid subscription", fun test_create_paid/0},
|
||||
{"Get active by user", fun test_get_active_by_user/0},
|
||||
{"List by user", fun test_list_by_user/0},
|
||||
{"Update status", fun test_update_status/0},
|
||||
{"Check expired", fun test_check_expired/0}
|
||||
]}.
|
||||
|
||||
test_create_trial() ->
|
||||
UserId = <<"user123">>,
|
||||
{ok, Sub} = core_subscription:create(UserId, trial, false),
|
||||
|
||||
?assertEqual(UserId, Sub#subscription.user_id),
|
||||
?assertEqual(trial, Sub#subscription.plan),
|
||||
?assertEqual(false, Sub#subscription.trial_used),
|
||||
?assertEqual(active, Sub#subscription.status).
|
||||
|
||||
test_create_paid() ->
|
||||
UserId = <<"user123">>,
|
||||
{ok, Sub} = core_subscription:create(UserId, monthly, true),
|
||||
|
||||
?assertEqual(UserId, Sub#subscription.user_id),
|
||||
?assertEqual(monthly, Sub#subscription.plan),
|
||||
?assertEqual(true, Sub#subscription.trial_used),
|
||||
?assertEqual(active, Sub#subscription.status).
|
||||
|
||||
test_get_active_by_user() ->
|
||||
UserId = <<"user123">>,
|
||||
{ok, Sub1} = core_subscription:create(UserId, trial, false),
|
||||
{ok, Sub2} = core_subscription:create(UserId, monthly, true),
|
||||
|
||||
core_subscription:update_status(Sub1#subscription.id, expired),
|
||||
|
||||
{ok, Active} = core_subscription:get_active_by_user(UserId),
|
||||
?assertEqual(Sub2#subscription.id, Active#subscription.id).
|
||||
|
||||
test_list_by_user() ->
|
||||
UserId = <<"user123">>,
|
||||
{ok, _} = core_subscription:create(UserId, trial, false),
|
||||
{ok, _} = core_subscription:create(UserId, monthly, true),
|
||||
|
||||
{ok, Subs} = core_subscription:list_by_user(UserId),
|
||||
?assertEqual(2, length(Subs)).
|
||||
|
||||
test_update_status() ->
|
||||
UserId = <<"user123">>,
|
||||
{ok, Sub} = core_subscription:create(UserId, trial, false),
|
||||
|
||||
{ok, Cancelled} = core_subscription:update_status(Sub#subscription.id, cancelled),
|
||||
?assertEqual(cancelled, Cancelled#subscription.status).
|
||||
|
||||
test_check_expired() ->
|
||||
UserId = <<"user123">>,
|
||||
% Создаём подписку с истёкшим сроком
|
||||
{ok, Sub} = core_subscription:create(UserId, trial, false),
|
||||
% Принудительно устанавливаем expires_at в прошлое
|
||||
Past = {{2020, 1, 1}, {0, 0, 0}},
|
||||
mnesia:dirty_write(Sub#subscription{expires_at = Past}),
|
||||
|
||||
core_subscription:check_expired(),
|
||||
|
||||
{ok, Updated} = core_subscription:get_by_id(Sub#subscription.id),
|
||||
?assertEqual(expired, Updated#subscription.status).
|
||||
Reference in New Issue
Block a user