2024-06-19 06:52:08 -04:00
|
|
|
require "test_helper"
|
|
|
|
|
2024-06-21 16:23:28 -04:00
|
|
|
class Account::TransferTest < ActiveSupport::TestCase
|
2024-06-19 06:52:08 -04:00
|
|
|
setup do
|
2024-07-10 11:22:59 -04:00
|
|
|
@outflow = account_entries(:transfer_out)
|
|
|
|
@inflow = account_entries(:transfer_in)
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "transfer valid if it has inflow and outflow from different accounts for the same amount" do
|
2024-07-01 10:49:43 -04:00
|
|
|
transfer = Account::Transfer.create! entries: [ @inflow, @outflow ]
|
2024-06-19 06:52:08 -04:00
|
|
|
|
|
|
|
assert transfer.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "transfer must have 2 transactions" do
|
2024-07-01 10:49:43 -04:00
|
|
|
invalid_transfer_1 = Account::Transfer.new entries: [ @outflow ]
|
2024-07-10 11:22:59 -04:00
|
|
|
invalid_transfer_2 = Account::Transfer.new entries: [ @inflow, @outflow, account_entries(:transaction) ]
|
2024-06-19 06:52:08 -04:00
|
|
|
|
|
|
|
assert invalid_transfer_1.invalid?
|
|
|
|
assert invalid_transfer_2.invalid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "transfer cannot have 2 transactions from the same account" do
|
2024-07-10 11:22:59 -04:00
|
|
|
account = accounts(:depository)
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
inflow = account.entries.create! \
|
|
|
|
date: Date.current,
|
|
|
|
name: "Inflow",
|
|
|
|
amount: -100,
|
|
|
|
currency: "USD",
|
2024-12-30 17:29:59 -05:00
|
|
|
entryable: Account::Transaction.new(
|
|
|
|
category: account.family.default_transfer_category
|
|
|
|
)
|
2024-07-01 10:49:43 -04:00
|
|
|
|
|
|
|
outflow = account.entries.create! \
|
|
|
|
date: Date.current,
|
|
|
|
name: "Outflow",
|
|
|
|
amount: 100,
|
|
|
|
currency: "USD",
|
2024-12-30 17:29:59 -05:00
|
|
|
entryable: Account::Transaction.new(
|
|
|
|
category: account.family.default_transfer_category
|
|
|
|
)
|
2024-06-19 06:52:08 -04:00
|
|
|
|
|
|
|
assert_raise ActiveRecord::RecordInvalid do
|
2024-07-01 10:49:43 -04:00
|
|
|
Account::Transfer.create! entries: [ inflow, outflow ]
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-12-30 17:29:59 -05:00
|
|
|
test "all transfer transactions must have transfer category" do
|
|
|
|
@inflow.entryable.update! category: nil
|
2024-06-19 06:52:08 -04:00
|
|
|
|
2024-12-30 17:29:59 -05:00
|
|
|
transfer = Account::Transfer.new entries: [ @inflow, @outflow ]
|
|
|
|
|
|
|
|
assert_not transfer.valid?
|
|
|
|
assert_equal "Entries must have transfer category", transfer.errors.full_messages.first
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
2024-06-21 23:04:15 +02:00
|
|
|
test "single-currency transfer transactions must net to zero" do
|
2024-06-19 06:52:08 -04:00
|
|
|
@outflow.update! amount: 105
|
|
|
|
|
|
|
|
assert_raises ActiveRecord::RecordInvalid do
|
2024-07-01 10:49:43 -04:00
|
|
|
Account::Transfer.create! entries: [ @inflow, @outflow ]
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|
2024-06-21 23:04:15 +02:00
|
|
|
|
|
|
|
test "multi-currency transfer transactions do not have to net to zero" do
|
|
|
|
@outflow.update! amount: 105, currency: "EUR"
|
2024-07-01 10:49:43 -04:00
|
|
|
transfer = Account::Transfer.create! entries: [ @inflow, @outflow ]
|
2024-06-21 23:04:15 +02:00
|
|
|
|
|
|
|
assert transfer.valid?
|
|
|
|
end
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|