2024-02-23 21:34:33 -05:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class TransactionsControllerTest < ActionDispatch::IntegrationTest
|
2024-07-10 11:22:59 -04:00
|
|
|
include Account::EntriesTestHelper
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
setup do
|
2024-02-27 12:43:49 -05:00
|
|
|
sign_in @user = users(:family_admin)
|
2024-07-10 11:22:59 -04:00
|
|
|
@transaction = account_entries(:transaction)
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should get new" do
|
|
|
|
get new_transaction_url
|
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
2024-06-24 11:58:39 -04:00
|
|
|
test "prefills account_id" do
|
2024-07-10 11:22:59 -04:00
|
|
|
get new_transaction_url(account_id: @transaction.account.id)
|
2024-04-16 12:44:31 -06:00
|
|
|
assert_response :success
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_select "option[selected][value='#{@transaction.account.id}']"
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
test "should create transaction" do
|
2024-05-30 20:55:18 -04:00
|
|
|
account = @user.family.accounts.first
|
2024-07-01 10:49:43 -04:00
|
|
|
entry_params = {
|
2024-05-30 20:55:18 -04:00
|
|
|
account_id: account.id,
|
|
|
|
amount: 100.45,
|
|
|
|
currency: "USD",
|
|
|
|
date: Date.current,
|
2024-07-01 10:49:43 -04:00
|
|
|
name: "Test transaction",
|
|
|
|
entryable_type: "Account::Transaction",
|
|
|
|
entryable_attributes: { category_id: categories(:food_and_drink).id }
|
2024-05-30 20:55:18 -04:00
|
|
|
}
|
2024-04-24 21:55:52 +02:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_difference [ "Account::Entry.count", "Account::Transaction.count" ], 1 do
|
|
|
|
post transactions_url, params: { account_entry: entry_params }
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_equal entry_params[:amount].to_d, Account::Transaction.order(created_at: :desc).first.entry.amount
|
2024-06-21 16:23:28 -04:00
|
|
|
assert_equal "New transaction created successfully", flash[:notice]
|
2024-05-30 20:55:18 -04:00
|
|
|
assert_enqueued_with(job: AccountSyncJob)
|
2024-06-24 11:58:39 -04:00
|
|
|
assert_redirected_to account_url(account)
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "expenses are positive" do
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_difference([ "Account::Transaction.count", "Account::Entry.count" ], 1) do
|
2024-06-24 11:58:39 -04:00
|
|
|
post transactions_url, params: {
|
2024-07-01 10:49:43 -04:00
|
|
|
account_entry: {
|
2024-06-24 11:58:39 -04:00
|
|
|
nature: "expense",
|
2024-07-10 11:22:59 -04:00
|
|
|
account_id: @transaction.account_id,
|
|
|
|
amount: @transaction.amount,
|
|
|
|
currency: @transaction.currency,
|
|
|
|
date: @transaction.date,
|
|
|
|
name: @transaction.name,
|
2024-07-01 10:49:43 -04:00
|
|
|
entryable_type: "Account::Transaction",
|
|
|
|
entryable_attributes: {}
|
2024-06-24 11:58:39 -04:00
|
|
|
}
|
|
|
|
}
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
created_entry = Account::Entry.order(created_at: :desc).first
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_redirected_to account_url(@transaction.account)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert created_entry.amount.positive?, "Amount should be positive"
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "incomes are negative" do
|
2024-06-24 11:58:39 -04:00
|
|
|
assert_difference("Account::Transaction.count") do
|
2024-06-07 16:56:30 -04:00
|
|
|
post transactions_url, params: {
|
2024-07-01 10:49:43 -04:00
|
|
|
account_entry: {
|
2024-06-07 16:56:30 -04:00
|
|
|
nature: "income",
|
2024-07-10 11:22:59 -04:00
|
|
|
account_id: @transaction.account_id,
|
|
|
|
amount: @transaction.amount,
|
|
|
|
currency: @transaction.currency,
|
|
|
|
date: @transaction.date,
|
|
|
|
name: @transaction.name,
|
2024-07-01 10:49:43 -04:00
|
|
|
entryable_type: "Account::Transaction",
|
|
|
|
entryable_attributes: { category_id: categories(:food_and_drink).id }
|
2024-06-07 16:56:30 -04:00
|
|
|
}
|
|
|
|
}
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
created_entry = Account::Entry.order(created_at: :desc).first
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_redirected_to account_url(@transaction.account)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert created_entry.amount.negative?, "Amount should be negative"
|
2024-04-16 12:44:31 -06:00
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
test "transaction count represents filtered total" do
|
|
|
|
family = families(:empty)
|
|
|
|
sign_in family.users.first
|
2024-07-17 14:18:12 -04:00
|
|
|
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
3.times do
|
|
|
|
create_transaction(account: account)
|
2024-06-24 11:58:39 -04:00
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
get transactions_url(per_page: 10)
|
2024-04-24 21:55:52 +02:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_dom "#total-transactions", count: 1, text: family.entries.account_transactions.size.to_s
|
|
|
|
|
|
|
|
searchable_transaction = create_transaction(account: account, name: "Unique test name")
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
get transactions_url(q: { search: searchable_transaction.name })
|
2024-06-24 11:58:39 -04:00
|
|
|
|
|
|
|
# Only finds 1 transaction that matches filter
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_dom "#" + dom_id(searchable_transaction), count: 1
|
2024-06-24 11:58:39 -04:00
|
|
|
assert_dom "#total-transactions", count: 1, text: "1"
|
2024-04-24 21:55:52 +02:00
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
test "can paginate" do
|
|
|
|
family = families(:empty)
|
|
|
|
sign_in family.users.first
|
2024-07-17 14:18:12 -04:00
|
|
|
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
|
2024-07-10 11:22:59 -04:00
|
|
|
|
|
|
|
11.times do
|
|
|
|
create_transaction(account: account)
|
|
|
|
end
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
sorted_transactions = family.entries.account_transactions.reverse_chronological.to_a
|
2024-07-01 10:49:43 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_equal 11, sorted_transactions.count
|
|
|
|
|
|
|
|
get transactions_url(page: 1, per_page: 10)
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
sorted_transactions.first(10).each do |transaction|
|
2024-06-24 11:58:39 -04:00
|
|
|
assert_dom "#" + dom_id(transaction), count: 1
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
get transactions_url(page: 2, per_page: 10)
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_dom "#" + dom_id(sorted_transactions.last), count: 1
|
|
|
|
|
|
|
|
get transactions_url(page: 9999999, per_page: 10) # out of range loads last page
|
|
|
|
|
|
|
|
assert_dom "#" + dom_id(sorted_transactions.last), count: 1
|
2024-04-24 21:55:52 +02:00
|
|
|
end
|
2024-06-07 16:56:30 -04:00
|
|
|
|
|
|
|
test "can destroy many transactions at once" do
|
2024-07-10 11:22:59 -04:00
|
|
|
transactions = @user.family.entries.account_transactions
|
|
|
|
delete_count = transactions.size
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_difference([ "Account::Transaction.count", "Account::Entry.count" ], -delete_count) do
|
2024-06-24 11:58:39 -04:00
|
|
|
post bulk_delete_transactions_url, params: {
|
|
|
|
bulk_delete: {
|
2024-07-10 11:22:59 -04:00
|
|
|
entry_ids: transactions.pluck(:id)
|
2024-06-24 11:58:39 -04:00
|
|
|
}
|
|
|
|
}
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_redirected_to transactions_url
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_equal "#{delete_count} transactions deleted", flash[:notice]
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "can update many transactions at once" do
|
2024-07-10 11:22:59 -04:00
|
|
|
transactions = @user.family.entries.account_transactions
|
2024-06-07 16:56:30 -04:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_difference [ "Account::Entry.count", "Account::Transaction.count" ], 0 do
|
|
|
|
post bulk_update_transactions_url, params: {
|
|
|
|
bulk_update: {
|
|
|
|
entry_ids: transactions.map(&:id),
|
|
|
|
date: 1.day.ago.to_date,
|
|
|
|
category_id: Category.second.id,
|
|
|
|
merchant_id: Merchant.second.id,
|
|
|
|
notes: "Updated note"
|
|
|
|
}
|
2024-06-07 16:56:30 -04:00
|
|
|
}
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
2024-06-07 16:56:30 -04:00
|
|
|
|
|
|
|
assert_redirected_to transactions_url
|
|
|
|
assert_equal "#{transactions.count} transactions updated", flash[:notice]
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
transactions.reload.each do |transaction|
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_equal 1.day.ago.to_date, transaction.date
|
|
|
|
assert_equal Category.second, transaction.account_transaction.category
|
|
|
|
assert_equal Merchant.second, transaction.account_transaction.merchant
|
2024-10-09 14:59:18 -04:00
|
|
|
assert_equal "Updated note", transaction.notes
|
2024-06-07 16:56:30 -04:00
|
|
|
end
|
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|