1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-22 06:39:39 +02:00

Account::Entry Delegated Type (namespace updates part 7) (#923)

* Initial entryable models

* Update transfer and tests

* Update transaction controllers and tests

* Update sync process to use new entries model

* Get dashboard working again

* Update transfers, imports, and accounts to use Account::Entry

* Update system tests

* Consolidate transaction management into entries controller

* Add permitted partial key helper

* Move account transactions list to entries controller

* Delegate transaction entries search

* Move transfer relation to entry

* Update bulk transaction management flows to use entries

* Remove test code

* Test fix attempt

* Update demo data script

* Consolidate remaining transaction partials to entries

* Consolidate valuations controller to entries controller

* Lint fix

* Remove unused files, additional cleanup

* Add back valuation creation

* Make migrations fully reversible

* Stale routes cleanup

* Migrations reversible fix

* Move types to entryable concern

* Fix search when no entries found

* Remove more unused code
This commit is contained in:
Zach Gollwitzer 2024-07-01 10:49:43 -04:00 committed by GitHub
parent 320954282a
commit c3314e62d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
105 changed files with 2150 additions and 1576 deletions

View file

@ -0,0 +1,133 @@
require "test_helper"
class Account::EntriesControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@account = accounts(:savings)
@transaction_entry = @account.entries.account_transactions.first
@valuation_entry = @account.entries.account_valuations.first
end
test "should edit valuation entry" do
get edit_account_entry_url(@account, @valuation_entry)
assert_response :success
end
test "should show transaction entry" do
get account_entry_url(@account, @transaction_entry)
assert_response :success
end
test "should show valuation entry" do
get account_entry_url(@account, @valuation_entry)
assert_response :success
end
test "should get list of transaction entries" do
get transaction_account_entries_url(@account)
assert_response :success
end
test "should get list of valuation entries" do
get valuation_account_entries_url(@account)
assert_response :success
end
test "gets new entry by type" do
get new_account_entry_url(@account, entryable_type: "Account::Valuation")
assert_response :success
end
test "should create valuation" do
assert_difference [ "Account::Entry.count", "Account::Valuation.count" ], 1 do
post account_entries_url(@account), params: {
account_entry: {
name: "Manual valuation",
amount: 19800,
date: Date.current,
currency: @account.currency,
entryable_type: "Account::Valuation",
entryable_attributes: {}
}
}
end
assert_equal "Valuation created", flash[:notice]
assert_enqueued_with job: AccountSyncJob
assert_redirected_to account_path(@account)
end
test "error when valuation already exists for date" do
assert_no_difference_in_entries do
post account_entries_url(@account), params: {
account_entry: {
amount: 19800,
date: @valuation_entry.date,
currency: @valuation_entry.currency,
entryable_type: "Account::Valuation",
entryable_attributes: {}
}
}
end
assert_equal "Date has already been taken", flash[:error]
assert_redirected_to account_path(@account)
end
test "can update entry without entryable attributes" do
assert_no_difference_in_entries do
patch account_entry_url(@account, @valuation_entry), params: {
account_entry: {
name: "Updated name"
}
}
end
assert_redirected_to account_entry_url(@account, @valuation_entry)
assert_enqueued_with(job: AccountSyncJob)
end
test "should update transaction entry with entryable attributes" do
assert_no_difference_in_entries do
patch account_entry_url(@account, @transaction_entry), params: {
account_entry: {
name: "Updated name",
date: Date.current,
currency: "USD",
amount: 20,
entryable_type: @transaction_entry.entryable_type,
entryable_attributes: {
id: @transaction_entry.entryable_id,
tag_ids: [ Tag.first.id, Tag.second.id ],
category_id: Category.first.id,
merchant_id: Merchant.first.id,
notes: "test notes",
excluded: false
}
}
}
end
assert_redirected_to account_entry_url(@account, @transaction_entry)
assert_enqueued_with(job: AccountSyncJob)
end
test "should destroy transaction entry" do
[ @transaction_entry, @valuation_entry ].each do |entry|
assert_difference -> { Account::Entry.count } => -1, -> { entry.entryable_class.count } => -1 do
delete account_entry_url(@account, entry)
end
assert_redirected_to account_url(@account)
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

View file

@ -1,40 +0,0 @@
require "test_helper"
class Account::TransactionsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@transaction = account_transactions(:checking_one)
@account = @transaction.account
@recent_transactions = @user.family.transactions.ordered.limit(20).to_a
end
test "should show transaction" do
get account_transaction_url(@transaction.account, @transaction)
assert_response :success
end
test "should update transaction" do
patch account_transaction_url(@transaction.account, @transaction), params: {
account_transaction: {
account_id: @transaction.account_id,
amount: @transaction.amount,
currency: @transaction.currency,
date: @transaction.date,
name: @transaction.name,
tag_ids: [ Tag.first.id, Tag.second.id ]
}
}
assert_redirected_to account_transaction_url(@transaction.account, @transaction)
assert_enqueued_with(job: AccountSyncJob)
end
test "should destroy transaction" do
assert_difference("Account::Transaction.count", -1) do
delete account_transaction_url(@transaction.account, @transaction)
end
assert_redirected_to account_url(@transaction.account)
assert_enqueued_with(job: AccountSyncJob)
end
end

View file

@ -1,71 +0,0 @@
require "test_helper"
class Account::ValuationsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@valuation = account_valuations(:savings_one)
@account = @valuation.account
end
test "get valuations for an account" do
get account_valuations_url(@account)
assert_response :success
end
test "new" do
get new_account_valuation_url(@account)
assert_response :success
end
test "should create valuation" do
assert_difference("Account::Valuation.count") do
post account_valuations_url(@account), params: {
account_valuation: {
value: 19800,
date: Date.current
}
}
end
assert_equal "Valuation created", flash[:notice]
assert_enqueued_with job: AccountSyncJob
assert_redirected_to account_path(@account)
end
test "error when valuation already exists for date" do
assert_difference("Account::Valuation.count", 0) do
post account_valuations_url(@account), params: {
account_valuation: {
value: 19800,
date: @valuation.date
}
}
end
assert_equal "Date has already been taken", flash[:error]
assert_redirected_to account_path(@account)
end
test "should update valuation" do
patch account_valuation_url(@account, @valuation), params: {
account_valuation: {
value: 19550,
date: Date.current
}
}
assert_equal "Valuation updated", flash[:notice]
assert_enqueued_with job: AccountSyncJob
assert_redirected_to account_path(@account)
end
test "should destroy valuation" do
assert_difference("Account::Valuation.count", -1) do
delete account_valuation_url(@account, @valuation)
end
assert_equal "Valuation deleted", flash[:notice]
assert_enqueued_with job: AccountSyncJob
assert_redirected_to account_path(@account)
end
end

View file

@ -44,7 +44,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end
test "should create an account" do
assert_difference [ "Account.count", "Account::Valuation.count" ], 1 do
assert_difference [ "Account.count", "Account::Valuation.count", "Account::Entry.count" ], 1 do
post accounts_path, params: {
account: {
accountable_type: "Depository",

View file

@ -3,8 +3,8 @@ require "test_helper"
class TransactionsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@transaction = account_transactions(:checking_one)
@recent_transactions = @user.family.transactions.ordered.limit(20).to_a
@transaction_entry = account_entries(:checking_one)
@recent_transaction_entries = @user.family.entries.account_transactions.reverse_chronological.limit(20).to_a
end
test "should get new" do
@ -13,88 +13,96 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
end
test "prefills account_id" do
get new_transaction_url(account_id: @transaction.account.id)
get new_transaction_url(account_id: @transaction_entry.account.id)
assert_response :success
assert_select "option[selected][value='#{@transaction.account.id}']"
assert_select "option[selected][value='#{@transaction_entry.account.id}']"
end
test "should create transaction" do
account = @user.family.accounts.first
transaction_params = {
entry_params = {
account_id: account.id,
amount: 100.45,
currency: "USD",
date: Date.current,
name: "Test transaction"
name: "Test transaction",
entryable_type: "Account::Transaction",
entryable_attributes: { category_id: categories(:food_and_drink).id }
}
assert_difference("Account::Transaction.count") do
post transactions_url, params: { transaction: transaction_params }
assert_difference [ "Account::Entry.count", "Account::Transaction.count" ], 1 do
post transactions_url, params: { account_entry: entry_params }
end
assert_equal transaction_params[:amount].to_d, Account::Transaction.order(created_at: :desc).first.amount
assert_equal entry_params[:amount].to_d, Account::Transaction.order(created_at: :desc).first.entry.amount
assert_equal "New transaction created successfully", flash[:notice]
assert_enqueued_with(job: AccountSyncJob)
assert_redirected_to account_url(account)
end
test "expenses are positive" do
assert_difference("Account::Transaction.count") do
assert_difference([ "Account::Transaction.count", "Account::Entry.count" ], 1) do
post transactions_url, params: {
transaction: {
account_entry: {
nature: "expense",
account_id: @transaction.account_id,
amount: @transaction.amount,
currency: @transaction.currency,
date: @transaction.date,
name: @transaction.name
account_id: @transaction_entry.account_id,
amount: @transaction_entry.amount,
currency: @transaction_entry.currency,
date: @transaction_entry.date,
name: @transaction_entry.name,
entryable_type: "Account::Transaction",
entryable_attributes: {}
}
}
end
created_transaction = Account::Transaction.order(created_at: :desc).first
created_entry = Account::Entry.order(created_at: :desc).first
assert_redirected_to account_url(@transaction.account)
assert created_transaction.amount.positive?, "Amount should be positive"
assert_redirected_to account_url(@transaction_entry.account)
assert created_entry.amount.positive?, "Amount should be positive"
end
test "incomes are negative" do
assert_difference("Account::Transaction.count") do
post transactions_url, params: {
transaction: {
account_entry: {
nature: "income",
account_id: @transaction.account_id,
amount: @transaction.amount,
currency: @transaction.currency,
date: @transaction.date,
name: @transaction.name
account_id: @transaction_entry.account_id,
amount: @transaction_entry.amount,
currency: @transaction_entry.currency,
date: @transaction_entry.date,
name: @transaction_entry.name,
entryable_type: "Account::Transaction",
entryable_attributes: { category_id: categories(:food_and_drink).id }
}
}
end
created_transaction = Account::Transaction.order(created_at: :desc).first
created_entry = Account::Entry.order(created_at: :desc).first
assert_redirected_to account_url(@transaction.account)
assert created_transaction.amount.negative?, "Amount should be negative"
assert_redirected_to account_url(@transaction_entry.account)
assert created_entry.amount.negative?, "Amount should be negative"
end
test "should get paginated index with most recent transactions first" do
get transactions_url
get transactions_url(per_page: 10)
assert_response :success
@recent_transactions.first(10).each do |transaction|
@recent_transaction_entries.first(10).each do |transaction|
assert_dom "#" + dom_id(transaction), count: 1
end
end
test "transaction count represents filtered total" do
get transactions_url
assert_dom "#total-transactions", count: 1, text: @user.family.transactions.select { |t| t.currency == "USD" }.count.to_s
get transactions_url(per_page: 10)
assert_dom "#total-transactions", count: 1, text: @user.family.entries.account_transactions.select { |t| t.currency == "USD" }.count.to_s
new_transaction = @user.family.accounts.first.transactions.create! \
new_transaction = @user.family.accounts.first.entries.create! \
entryable: Account::Transaction.new,
name: "Transaction to search for",
date: Date.current,
amount: 0
amount: 0,
currency: "USD"
get transactions_url(q: { search: new_transaction.name })
@ -104,28 +112,30 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
end
test "can navigate to paginated result" do
get transactions_url(page: 2)
get transactions_url(page: 2, per_page: 10)
assert_response :success
@recent_transactions[10, 10].select { |t| t.transfer_id == nil }.each do |transaction|
visible_transaction_entries = @recent_transaction_entries[10, 10].reject { |e| e.transfer.present? }
visible_transaction_entries.each do |transaction|
assert_dom "#" + dom_id(transaction), count: 1
end
end
test "loads last page when page is out of range" do
user_oldest_transaction = @user.family.transactions.ordered.reject(&:transfer?).last
user_oldest_transaction_entry = @user.family.entries.account_transactions.chronological.first
get transactions_url(page: 9999999999)
assert_response :success
assert_dom "#" + dom_id(user_oldest_transaction), count: 1
assert_dom "#" + dom_id(user_oldest_transaction_entry), count: 1
end
test "can destroy many transactions at once" do
delete_count = 10
assert_difference("Account::Transaction.count", -delete_count) do
assert_difference([ "Account::Transaction.count", "Account::Entry.count" ], -delete_count) do
post bulk_delete_transactions_url, params: {
bulk_delete: {
transaction_ids: @recent_transactions.first(delete_count).pluck(:id)
entry_ids: @recent_transaction_entries.first(delete_count).pluck(:id)
}
}
end
@ -135,36 +145,41 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
end
test "can update many transactions at once" do
transactions = @user.family.transactions.ordered.limit(20)
transactions = @user.family.entries.account_transactions.reverse_chronological.limit(20)
transactions.each do |transaction|
transaction.update! \
excluded: false,
category_id: Category.first.id,
merchant_id: Merchant.first.id,
notes: "Starting note"
date: Date.current,
entryable_attributes: {
id: transaction.account_transaction.id,
category_id: Category.first.id,
merchant_id: Merchant.first.id,
notes: "Starting note"
}
end
post bulk_update_transactions_url, params: {
bulk_update: {
date: Date.current,
transaction_ids: transactions.map(&:id),
excluded: true,
category_id: Category.second.id,
merchant_id: Merchant.second.id,
notes: "Updated note"
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"
}
}
}
end
assert_redirected_to transactions_url
assert_equal "#{transactions.count} transactions updated", flash[:notice]
transactions.reload.each do |transaction|
assert_equal Date.current, transaction.date
assert transaction.excluded
assert_equal Category.second, transaction.category
assert_equal Merchant.second, transaction.merchant
assert_equal "Updated note", transaction.notes
transactions.reload
transactions.each do |transaction|
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
assert_equal "Updated note", transaction.account_transaction.notes
end
end
end