Files
sure/test/models/mercury_account_test.rb
Clayton 1191d9f7d8 feat: scope Mercury account uniqueness to mercury_item (#1032)
* feat: scope Mercury account uniqueness to mercury_item

* feat: extend to all other providers

* fix: add uniqueness test

* fix: lint

* fix: test

* fix: coderabbit comment

* fix: coderabbit comment

* fix: coderabbit comment

* fix: update

* fix: lint

* fix: update

* fix: update
2026-03-19 15:17:55 +01:00

77 lines
2.0 KiB
Ruby

require "test_helper"
class MercuryAccountTest < ActiveSupport::TestCase
setup do
@family_a = families(:dylan_family)
@family_b = families(:empty)
@item_a = MercuryItem.create!(
family: @family_a,
name: "Family A Mercury",
token: "token_a",
base_url: "https://api-sandbox.mercury.com/api/v1",
status: "good"
)
@item_b = MercuryItem.create!(
family: @family_b,
name: "Family B Mercury",
token: "token_b",
base_url: "https://api-sandbox.mercury.com/api/v1",
status: "good"
)
end
test "same account_id can be linked under different mercury_items" do
MercuryAccount.create!(
mercury_item: @item_a,
account_id: "shared_merc_acc_1",
name: "Checking",
currency: "USD",
current_balance: 5000
)
# A second family connecting the same Mercury account must succeed and produce
# an independent ledger (separate MercuryAccount row, separate Account).
assert_difference "MercuryAccount.count", 1 do
MercuryAccount.create!(
mercury_item: @item_b,
account_id: "shared_merc_acc_1",
name: "Checking",
currency: "USD",
current_balance: 5000
)
end
end
test "same account_id cannot appear twice under the same mercury_item" do
MercuryAccount.create!(
mercury_item: @item_a,
account_id: "duplicate_acc",
name: "Checking",
currency: "USD",
current_balance: 1000
)
duplicate = MercuryAccount.new(
mercury_item: @item_a,
account_id: "duplicate_acc",
name: "Checking",
currency: "USD",
current_balance: 1000
)
refute duplicate.valid?
assert_includes duplicate.errors[:account_id], "has already been taken"
assert_raises(ActiveRecord::RecordInvalid) do
MercuryAccount.create!(
mercury_item: @item_a,
account_id: "duplicate_acc",
name: "Checking",
currency: "USD",
current_balance: 1000
)
end
end
end