mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 03:54:08 +00:00
Handle investment_contribution classification and update related logic.
This commit is contained in:
@@ -9,7 +9,14 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
|
||||
@amount = 100
|
||||
end
|
||||
|
||||
test "creates basic transfer" do
|
||||
test "creates investment contribution when transferring from depository to investment" do
|
||||
# Ensure the Investment Contributions category exists with all required attributes
|
||||
investment_category = @family.categories.find_or_create_by!(name: "Investment Contributions") do |c|
|
||||
c.color = "#0d9488"
|
||||
c.lucide_icon = "trending-up"
|
||||
c.classification = "expense"
|
||||
end
|
||||
|
||||
creator = Transfer::Creator.new(
|
||||
family: @family,
|
||||
source_account_id: @source_account.id,
|
||||
@@ -22,17 +29,16 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
|
||||
|
||||
assert transfer.persisted?
|
||||
assert_equal "confirmed", transfer.status
|
||||
assert transfer.regular_transfer?
|
||||
assert_equal "transfer", transfer.transfer_type
|
||||
|
||||
# Verify outflow transaction (from source account)
|
||||
# Verify outflow transaction is marked as investment_contribution
|
||||
outflow = transfer.outflow_transaction
|
||||
assert_equal "funds_movement", outflow.kind
|
||||
assert_equal "investment_contribution", outflow.kind
|
||||
assert_equal @amount, outflow.entry.amount
|
||||
assert_equal @source_account.currency, outflow.entry.currency
|
||||
assert_equal "Transfer to #{@destination_account.name}", outflow.entry.name
|
||||
assert_equal investment_category, outflow.category, "Should auto-assign Investment Contributions category"
|
||||
|
||||
# Verify inflow transaction (to destination account)
|
||||
# Verify inflow transaction (always funds_movement)
|
||||
inflow = transfer.inflow_transaction
|
||||
assert_equal "funds_movement", inflow.kind
|
||||
assert_equal(@amount * -1, inflow.entry.amount)
|
||||
@@ -40,9 +46,42 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
|
||||
assert_equal "Transfer from #{@source_account.name}", inflow.entry.name
|
||||
end
|
||||
|
||||
test "creates multi-currency transfer" do
|
||||
# Use crypto account which has USD currency but different from source
|
||||
test "creates basic transfer between depository accounts" do
|
||||
other_depository = @family.accounts.create!(name: "Savings", balance: 1000, currency: "USD", accountable: Depository.new)
|
||||
|
||||
creator = Transfer::Creator.new(
|
||||
family: @family,
|
||||
source_account_id: @source_account.id,
|
||||
destination_account_id: other_depository.id,
|
||||
date: @date,
|
||||
amount: @amount
|
||||
)
|
||||
|
||||
transfer = creator.create
|
||||
|
||||
assert transfer.persisted?
|
||||
assert_equal "confirmed", transfer.status
|
||||
assert transfer.regular_transfer?
|
||||
assert_equal "transfer", transfer.transfer_type
|
||||
|
||||
# Verify outflow transaction (depository to depository = funds_movement)
|
||||
outflow = transfer.outflow_transaction
|
||||
assert_equal "funds_movement", outflow.kind
|
||||
assert_nil outflow.category, "Should NOT auto-assign category for regular transfers"
|
||||
|
||||
# Verify inflow transaction
|
||||
inflow = transfer.inflow_transaction
|
||||
assert_equal "funds_movement", inflow.kind
|
||||
end
|
||||
|
||||
test "creates investment contribution when transferring from depository to crypto" do
|
||||
# Use crypto account - transfer from depository should be investment_contribution
|
||||
crypto_account = accounts(:crypto)
|
||||
investment_category = @family.categories.find_or_create_by!(name: "Investment Contributions") do |c|
|
||||
c.color = "#0d9488"
|
||||
c.lucide_icon = "trending-up"
|
||||
c.classification = "expense"
|
||||
end
|
||||
|
||||
creator = Transfer::Creator.new(
|
||||
family: @family,
|
||||
@@ -55,13 +94,12 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
|
||||
transfer = creator.create
|
||||
|
||||
assert transfer.persisted?
|
||||
assert transfer.regular_transfer?
|
||||
assert_equal "transfer", transfer.transfer_type
|
||||
|
||||
# Verify outflow transaction
|
||||
# Verify outflow transaction is investment_contribution (not funds_movement)
|
||||
outflow = transfer.outflow_transaction
|
||||
assert_equal "funds_movement", outflow.kind
|
||||
assert_equal "investment_contribution", outflow.kind
|
||||
assert_equal "Transfer to #{crypto_account.name}", outflow.entry.name
|
||||
assert_equal investment_category, outflow.category
|
||||
|
||||
# Verify inflow transaction with currency handling
|
||||
inflow = transfer.inflow_transaction
|
||||
@@ -70,6 +108,32 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
|
||||
assert_equal crypto_account.currency, inflow.entry.currency
|
||||
end
|
||||
|
||||
test "creates funds_movement for investment to investment transfer (rollover)" do
|
||||
# Rollover case: investment → investment should stay as funds_movement
|
||||
other_investment = @family.accounts.create!(name: "IRA", balance: 5000, currency: "USD", accountable: Investment.new)
|
||||
|
||||
creator = Transfer::Creator.new(
|
||||
family: @family,
|
||||
source_account_id: @destination_account.id, # investment account
|
||||
destination_account_id: other_investment.id,
|
||||
date: @date,
|
||||
amount: @amount
|
||||
)
|
||||
|
||||
transfer = creator.create
|
||||
|
||||
assert transfer.persisted?
|
||||
|
||||
# Verify outflow is funds_movement (NOT investment_contribution for rollovers)
|
||||
outflow = transfer.outflow_transaction
|
||||
assert_equal "funds_movement", outflow.kind
|
||||
assert_nil outflow.category, "Should NOT auto-assign category for investment→investment transfers"
|
||||
|
||||
# Verify inflow
|
||||
inflow = transfer.inflow_transaction
|
||||
assert_equal "funds_movement", inflow.kind
|
||||
end
|
||||
|
||||
test "creates loan payment" do
|
||||
loan_account = accounts(:loan)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user