2024-08-09 11:22:57 -04:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class Account::TradesControllerTest < ActionDispatch::IntegrationTest
|
2024-11-27 16:01:50 -05:00
|
|
|
include EntryableResourceInterfaceTest
|
|
|
|
|
2024-08-09 11:22:57 -04:00
|
|
|
setup do
|
|
|
|
sign_in @user = users(:family_admin)
|
2024-11-27 16:01:50 -05:00
|
|
|
@entry = account_entries(:trade)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
test "updates trade entry" do
|
|
|
|
assert_no_difference [ "Account::Entry.count", "Account::Trade.count" ] do
|
|
|
|
patch account_trade_url(@entry), params: {
|
|
|
|
account_entry: {
|
|
|
|
currency: "USD",
|
|
|
|
entryable_attributes: {
|
|
|
|
id: @entry.entryable_id,
|
|
|
|
qty: 20,
|
|
|
|
price: 20
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@entry.reload
|
2024-08-09 11:22:57 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
assert_enqueued_with job: SyncJob
|
|
|
|
|
|
|
|
assert_equal 20, @entry.account_trade.qty
|
|
|
|
assert_equal 20, @entry.account_trade.price
|
|
|
|
assert_equal "USD", @entry.currency
|
|
|
|
|
|
|
|
assert_redirected_to account_url(@entry.account)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
|
2024-08-09 20:11:27 -04:00
|
|
|
test "creates deposit entry" do
|
|
|
|
from_account = accounts(:depository) # Account the deposit is coming from
|
|
|
|
|
|
|
|
assert_difference -> { Account::Entry.count } => 2,
|
|
|
|
-> { Account::Transaction.count } => 2,
|
|
|
|
-> { Account::Transfer.count } => 1 do
|
2024-11-27 16:01:50 -05:00
|
|
|
post account_trades_url, params: {
|
2024-08-09 20:11:27 -04:00
|
|
|
account_entry: {
|
2024-11-27 16:01:50 -05:00
|
|
|
account_id: @entry.account_id,
|
|
|
|
type: "deposit",
|
2024-08-09 20:11:27 -04:00
|
|
|
date: Date.current,
|
|
|
|
amount: 10,
|
2024-11-18 15:50:47 -05:00
|
|
|
currency: "USD",
|
2024-08-09 20:11:27 -04:00
|
|
|
transfer_account_id: from_account.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
assert_redirected_to @entry.account
|
2024-08-09 20:11:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "creates withdrawal entry" do
|
|
|
|
to_account = accounts(:depository) # Account the withdrawal is going to
|
|
|
|
|
|
|
|
assert_difference -> { Account::Entry.count } => 2,
|
|
|
|
-> { Account::Transaction.count } => 2,
|
|
|
|
-> { Account::Transfer.count } => 1 do
|
2024-11-27 16:01:50 -05:00
|
|
|
post account_trades_url, params: {
|
2024-08-09 20:11:27 -04:00
|
|
|
account_entry: {
|
2024-11-27 16:01:50 -05:00
|
|
|
account_id: @entry.account_id,
|
|
|
|
type: "withdrawal",
|
2024-08-09 20:11:27 -04:00
|
|
|
date: Date.current,
|
|
|
|
amount: 10,
|
2024-11-18 15:50:47 -05:00
|
|
|
currency: "USD",
|
2024-08-09 20:11:27 -04:00
|
|
|
transfer_account_id: to_account.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
assert_redirected_to @entry.account
|
2024-08-09 20:11:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "deposit and withdrawal has optional transfer account" do
|
|
|
|
assert_difference -> { Account::Entry.count } => 1,
|
|
|
|
-> { Account::Transaction.count } => 1,
|
|
|
|
-> { Account::Transfer.count } => 0 do
|
2024-11-27 16:01:50 -05:00
|
|
|
post account_trades_url, params: {
|
2024-08-09 20:11:27 -04:00
|
|
|
account_entry: {
|
2024-11-27 16:01:50 -05:00
|
|
|
account_id: @entry.account_id,
|
|
|
|
type: "withdrawal",
|
2024-08-09 20:11:27 -04:00
|
|
|
date: Date.current,
|
2024-11-18 15:50:47 -05:00
|
|
|
amount: 10,
|
|
|
|
currency: "USD"
|
2024-08-09 20:11:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
created_entry = Account::Entry.order(created_at: :desc).first
|
|
|
|
|
|
|
|
assert created_entry.amount.positive?
|
2024-12-30 17:29:59 -05:00
|
|
|
assert created_entry.entryable.category.transfer?
|
2024-11-04 20:27:31 -05:00
|
|
|
assert_redirected_to @entry.account
|
2024-08-09 20:11:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "creates interest entry" do
|
|
|
|
assert_difference [ "Account::Entry.count", "Account::Transaction.count" ], 1 do
|
2024-11-27 16:01:50 -05:00
|
|
|
post account_trades_url, params: {
|
2024-08-09 20:11:27 -04:00
|
|
|
account_entry: {
|
2024-11-27 16:01:50 -05:00
|
|
|
account_id: @entry.account_id,
|
2024-08-09 20:11:27 -04:00
|
|
|
type: "interest",
|
|
|
|
date: Date.current,
|
2024-11-18 15:50:47 -05:00
|
|
|
amount: 10,
|
|
|
|
currency: "USD"
|
2024-08-09 20:11:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
created_entry = Account::Entry.order(created_at: :desc).first
|
|
|
|
|
|
|
|
assert created_entry.amount.negative?
|
2024-11-04 20:27:31 -05:00
|
|
|
assert_redirected_to @entry.account
|
2024-08-09 20:11:27 -04:00
|
|
|
end
|
|
|
|
|
2024-08-09 11:22:57 -04:00
|
|
|
test "creates trade buy entry" do
|
|
|
|
assert_difference [ "Account::Entry.count", "Account::Trade.count", "Security.count" ], 1 do
|
2024-11-27 16:01:50 -05:00
|
|
|
post account_trades_url, params: {
|
2024-08-09 11:22:57 -04:00
|
|
|
account_entry: {
|
2024-11-27 16:01:50 -05:00
|
|
|
account_id: @entry.account_id,
|
2024-08-09 11:22:57 -04:00
|
|
|
type: "buy",
|
|
|
|
date: Date.current,
|
2024-10-30 09:23:44 -04:00
|
|
|
ticker: "NVDA (NASDAQ)",
|
2024-08-09 11:22:57 -04:00
|
|
|
qty: 10,
|
2024-11-27 16:01:50 -05:00
|
|
|
price: 10,
|
|
|
|
currency: "USD"
|
2024-08-09 11:22:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
created_entry = Account::Entry.order(created_at: :desc).first
|
|
|
|
|
|
|
|
assert created_entry.amount.positive?
|
|
|
|
assert created_entry.account_trade.qty.positive?
|
2024-11-27 16:01:50 -05:00
|
|
|
assert_equal "Entry created", flash[:notice]
|
2024-11-15 13:49:37 -05:00
|
|
|
assert_enqueued_with job: SyncJob
|
2024-11-27 16:01:50 -05:00
|
|
|
assert_redirected_to account_url(created_entry.account)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "creates trade sell entry" do
|
|
|
|
assert_difference [ "Account::Entry.count", "Account::Trade.count" ], 1 do
|
2024-11-27 16:01:50 -05:00
|
|
|
post account_trades_url, params: {
|
2024-08-09 11:22:57 -04:00
|
|
|
account_entry: {
|
2024-11-27 16:01:50 -05:00
|
|
|
account_id: @entry.account_id,
|
2024-08-09 11:22:57 -04:00
|
|
|
type: "sell",
|
2024-10-30 09:23:44 -04:00
|
|
|
ticker: "AAPL (NYSE)",
|
2024-08-09 11:22:57 -04:00
|
|
|
date: Date.current,
|
|
|
|
currency: "USD",
|
|
|
|
qty: 10,
|
|
|
|
price: 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
created_entry = Account::Entry.order(created_at: :desc).first
|
|
|
|
|
|
|
|
assert created_entry.amount.negative?
|
|
|
|
assert created_entry.account_trade.qty.negative?
|
2024-11-27 16:01:50 -05:00
|
|
|
assert_equal "Entry created", flash[:notice]
|
2024-11-15 13:49:37 -05:00
|
|
|
assert_enqueued_with job: SyncJob
|
2024-11-27 16:01:50 -05:00
|
|
|
assert_redirected_to account_url(created_entry.account)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
end
|