2024-07-01 10:49:43 -04:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class Account::EntriesControllerTest < ActionDispatch::IntegrationTest
|
|
|
|
setup do
|
|
|
|
sign_in @user = users(:family_admin)
|
2024-07-10 11:22:59 -04:00
|
|
|
@transaction = account_entries :transaction
|
|
|
|
@valuation = account_entries :valuation
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should edit valuation entry" do
|
2024-07-10 11:22:59 -04:00
|
|
|
get edit_account_entry_url(@valuation.account, @valuation)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should show transaction entry" do
|
2024-07-10 11:22:59 -04:00
|
|
|
get account_entry_url(@transaction.account, @transaction)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should show valuation entry" do
|
2024-07-10 11:22:59 -04:00
|
|
|
get account_entry_url(@valuation.account, @valuation)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should get list of transaction entries" do
|
2024-07-10 11:22:59 -04:00
|
|
|
get transaction_account_entries_url(@transaction.account)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should get list of valuation entries" do
|
2024-07-10 11:22:59 -04:00
|
|
|
get valuation_account_entries_url(@valuation.account)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "gets new entry by type" do
|
2024-07-10 11:22:59 -04:00
|
|
|
get new_account_entry_url(@valuation.account, entryable_type: "Account::Valuation")
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should create valuation" do
|
|
|
|
assert_difference [ "Account::Entry.count", "Account::Valuation.count" ], 1 do
|
2024-07-10 11:22:59 -04:00
|
|
|
post account_entries_url(@valuation.account), params: {
|
2024-07-01 10:49:43 -04:00
|
|
|
account_entry: {
|
|
|
|
name: "Manual valuation",
|
|
|
|
amount: 19800,
|
|
|
|
date: Date.current,
|
2024-07-10 11:22:59 -04:00
|
|
|
currency: @valuation.account.currency,
|
2024-07-01 10:49:43 -04:00
|
|
|
entryable_type: "Account::Valuation",
|
|
|
|
entryable_attributes: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Valuation created", flash[:notice]
|
|
|
|
assert_enqueued_with job: AccountSyncJob
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_redirected_to account_path(@valuation.account)
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "error when valuation already exists for date" do
|
|
|
|
assert_no_difference_in_entries do
|
2024-07-10 11:22:59 -04:00
|
|
|
post account_entries_url(@valuation.account), params: {
|
2024-07-01 10:49:43 -04:00
|
|
|
account_entry: {
|
|
|
|
amount: 19800,
|
2024-07-10 11:22:59 -04:00
|
|
|
date: @valuation.date,
|
|
|
|
currency: @valuation.currency,
|
2024-07-01 10:49:43 -04:00
|
|
|
entryable_type: "Account::Valuation",
|
|
|
|
entryable_attributes: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-07-18 14:39:38 -04:00
|
|
|
assert_equal "Date has already been taken", flash[:alert]
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_redirected_to account_path(@valuation.account)
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "can update entry without entryable attributes" do
|
|
|
|
assert_no_difference_in_entries do
|
2024-07-10 11:22:59 -04:00
|
|
|
patch account_entry_url(@valuation.account, @valuation), params: {
|
2024-07-01 10:49:43 -04:00
|
|
|
account_entry: {
|
|
|
|
name: "Updated name"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_redirected_to account_entry_url(@valuation.account, @valuation)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_enqueued_with(job: AccountSyncJob)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should update transaction entry with entryable attributes" do
|
|
|
|
assert_no_difference_in_entries do
|
2024-07-10 11:22:59 -04:00
|
|
|
patch account_entry_url(@transaction.account, @transaction), params: {
|
2024-07-01 10:49:43 -04:00
|
|
|
account_entry: {
|
|
|
|
name: "Updated name",
|
|
|
|
date: Date.current,
|
|
|
|
currency: "USD",
|
|
|
|
amount: 20,
|
2024-07-10 11:22:59 -04:00
|
|
|
entryable_type: @transaction.entryable_type,
|
2024-07-01 10:49:43 -04:00
|
|
|
entryable_attributes: {
|
2024-07-10 11:22:59 -04:00
|
|
|
id: @transaction.entryable_id,
|
2024-07-01 10:49:43 -04:00
|
|
|
tag_ids: [ Tag.first.id, Tag.second.id ],
|
|
|
|
category_id: Category.first.id,
|
|
|
|
merchant_id: Merchant.first.id,
|
|
|
|
notes: "test notes",
|
|
|
|
excluded: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_redirected_to account_entry_url(@transaction.account, @transaction)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_enqueued_with(job: AccountSyncJob)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should destroy transaction entry" do
|
2024-07-10 11:22:59 -04:00
|
|
|
[ @transaction, @valuation ].each do |entry|
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_difference -> { Account::Entry.count } => -1, -> { entry.entryable_class.count } => -1 do
|
2024-07-10 11:22:59 -04:00
|
|
|
delete account_entry_url(entry.account, entry)
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_redirected_to account_url(entry.account)
|
2024-07-01 10:49:43 -04:00
|
|
|
assert_enqueued_with(job: AccountSyncJob)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Simple guard to verify that nested attributes are passed the record ID to avoid new creation of record
|
|
|
|
# See `update_only` option of accepts_nested_attributes_for
|
|
|
|
def assert_no_difference_in_entries(&block)
|
|
|
|
assert_no_difference [ "Account::Entry.count", "Account::Transaction.count", "Account::Valuation.count" ], &block
|
|
|
|
end
|
|
|
|
end
|