Add record to track overridden limits

Before, we were relying on just changing the cards_count in account, but this
could create problems where the system to calculate the next card number fails due
to the unique constraint.
This commit is contained in:
Jorge Manrubia
2025-12-15 14:49:47 +01:00
parent 725e850802
commit 3ef5e4eeef
19 changed files with 196 additions and 80 deletions
-31
View File
@@ -15,35 +15,4 @@ class Account::BillingTest < ActiveSupport::TestCase
account.subscription.update!(status: "active")
assert_equal account.subscription, account.active_subscription
end
test "detect nearing card limit" do
# Paid plans are never limited
accounts(:"37s").update_column(:cards_count, 1_000_000)
assert_not accounts(:"37s").nearing_plan_cards_limit?
# Free plan not near limit
accounts(:initech).update_column(:cards_count, 899)
assert_not accounts(:initech).nearing_plan_cards_limit?
# Free plan near limit
accounts(:initech).update_column(:cards_count, 900)
assert_not accounts(:initech).nearing_plan_cards_limit?
accounts(:initech).update_column(:cards_count, 901)
assert accounts(:initech).nearing_plan_cards_limit?
end
test "detect exceeding card limit" do
# Paid plans are never limited
accounts(:"37s").update_column(:cards_count, 1_000_000)
assert_not accounts(:"37s").exceeding_card_limit?
# Free plan under limit
accounts(:initech).update_column(:cards_count, 999)
assert_not accounts(:initech).exceeding_card_limit?
# Free plan over limit
accounts(:initech).update_column(:cards_count, 1001)
assert accounts(:initech).exceeding_card_limit?
end
end
+51
View File
@@ -0,0 +1,51 @@
require "test_helper"
class Account::LimitedTest < ActiveSupport::TestCase
test "detect nearing card limit" do
# Paid plans are never limited
accounts(:"37s").update_column(:cards_count, 1_000_000)
assert_not accounts(:"37s").nearing_plan_cards_limit?
# Free plan not near limit
accounts(:initech).update_column(:cards_count, 899)
assert_not accounts(:initech).nearing_plan_cards_limit?
# Free plan near limit
accounts(:initech).update_column(:cards_count, 900)
assert_not accounts(:initech).nearing_plan_cards_limit?
accounts(:initech).update_column(:cards_count, 901)
assert accounts(:initech).nearing_plan_cards_limit?
end
test "detect exceeding card limit" do
# Paid plans are never limited
accounts(:"37s").update_column(:cards_count, 1_000_000)
assert_not accounts(:"37s").exceeding_card_limit?
# Free plan under limit
accounts(:initech).update_column(:cards_count, 999)
assert_not accounts(:initech).exceeding_card_limit?
# Free plan over limit
accounts(:initech).update_column(:cards_count, 1001)
assert accounts(:initech).exceeding_card_limit?
end
test "override limits" do
account = accounts(:initech)
account.update_column(:cards_count, 1001)
assert account.exceeding_card_limit?
assert_equal 1001, account.billed_cards_count
account.override_limits card_count: 500
assert_not account.exceeding_card_limit?
assert_equal 500, account.billed_cards_count
assert_equal 1001, account.cards_count # original unchanged
account.reset_overridden_limits
assert account.exceeding_card_limit?
assert_equal 1001, account.billed_cards_count
end
end