1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00

Account::Sync model and test fixture simplifications (#968)

* Add sync model

* Fresh fixtures for sync tests

* Sync tests overhaul

* Fix entry tests

* Complete remaining model test updates

* Update system tests

* Update demo data task

* Add system tests back to PR checks

* More simplifications, add empty family to fixtures for easier testing
This commit is contained in:
Zach Gollwitzer 2024-07-10 11:22:59 -04:00 committed by GitHub
parent de5a2e55b3
commit c6bdf49f10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
60 changed files with 929 additions and 1353 deletions

View file

@ -1,10 +1,11 @@
require "test_helper"
class TransactionsControllerTest < ActionDispatch::IntegrationTest
include Account::EntriesTestHelper
setup do
sign_in @user = users(:family_admin)
@transaction_entry = account_entries(:checking_one)
@recent_transaction_entries = @user.family.entries.account_transactions.reverse_chronological.limit(20).to_a
@transaction = account_entries(:transaction)
end
test "should get new" do
@ -13,9 +14,9 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
end
test "prefills account_id" do
get new_transaction_url(account_id: @transaction_entry.account.id)
get new_transaction_url(account_id: @transaction.account.id)
assert_response :success
assert_select "option[selected][value='#{@transaction_entry.account.id}']"
assert_select "option[selected][value='#{@transaction.account.id}']"
end
test "should create transaction" do
@ -45,11 +46,11 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
post transactions_url, params: {
account_entry: {
nature: "expense",
account_id: @transaction_entry.account_id,
amount: @transaction_entry.amount,
currency: @transaction_entry.currency,
date: @transaction_entry.date,
name: @transaction_entry.name,
account_id: @transaction.account_id,
amount: @transaction.amount,
currency: @transaction.currency,
date: @transaction.date,
name: @transaction.name,
entryable_type: "Account::Transaction",
entryable_attributes: {}
}
@ -58,7 +59,7 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
created_entry = Account::Entry.order(created_at: :desc).first
assert_redirected_to account_url(@transaction_entry.account)
assert_redirected_to account_url(@transaction.account)
assert created_entry.amount.positive?, "Amount should be positive"
end
@ -67,11 +68,11 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
post transactions_url, params: {
account_entry: {
nature: "income",
account_id: @transaction_entry.account_id,
amount: @transaction_entry.amount,
currency: @transaction_entry.currency,
date: @transaction_entry.date,
name: @transaction_entry.name,
account_id: @transaction.account_id,
amount: @transaction.amount,
currency: @transaction.currency,
date: @transaction.date,
name: @transaction.name,
entryable_type: "Account::Transaction",
entryable_attributes: { category_id: categories(:food_and_drink).id }
}
@ -80,83 +81,79 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
created_entry = Account::Entry.order(created_at: :desc).first
assert_redirected_to account_url(@transaction_entry.account)
assert_redirected_to account_url(@transaction.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(per_page: 10)
assert_response :success
@recent_transaction_entries.first(10).each do |transaction|
assert_dom "#" + dom_id(transaction), count: 1
end
end
test "transaction count represents filtered total" do
family = families(:empty)
sign_in family.users.first
account = family.accounts.create! name: "Test", balance: 0, accountable: Depository.new
3.times do
create_transaction(account: account)
end
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.entries.create! \
entryable: Account::Transaction.new,
name: "Transaction to search for",
date: Date.current,
amount: 0,
currency: "USD"
assert_dom "#total-transactions", count: 1, text: family.entries.account_transactions.size.to_s
get transactions_url(q: { search: new_transaction.name })
searchable_transaction = create_transaction(account: account, name: "Unique test name")
get transactions_url(q: { search: searchable_transaction.name })
# Only finds 1 transaction that matches filter
assert_dom "#" + dom_id(new_transaction), count: 1
assert_dom "#" + dom_id(searchable_transaction), count: 1
assert_dom "#total-transactions", count: 1, text: "1"
end
test "can navigate to paginated result" do
get transactions_url(page: 2, per_page: 10)
test "can paginate" do
family = families(:empty)
sign_in family.users.first
account = family.accounts.create! name: "Test", balance: 0, accountable: Depository.new
11.times do
create_transaction(account: account)
end
sorted_transactions = family.entries.account_transactions.reverse_chronological.to_a
assert_equal 11, sorted_transactions.count
get transactions_url(page: 1, per_page: 10)
assert_response :success
visible_transaction_entries = @recent_transaction_entries[10, 10].reject { |e| e.transfer.present? }
visible_transaction_entries.each do |transaction|
sorted_transactions.first(10).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_entry = @user.family.entries.account_transactions.chronological.first
get transactions_url(page: 9999999999)
get transactions_url(page: 2, per_page: 10)
assert_response :success
assert_dom "#" + dom_id(user_oldest_transaction_entry), count: 1
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
end
test "can destroy many transactions at once" do
delete_count = 10
transactions = @user.family.entries.account_transactions
delete_count = transactions.size
assert_difference([ "Account::Transaction.count", "Account::Entry.count" ], -delete_count) do
post bulk_delete_transactions_url, params: {
bulk_delete: {
entry_ids: @recent_transaction_entries.first(delete_count).pluck(:id)
entry_ids: transactions.pluck(:id)
}
}
end
assert_redirected_to transactions_url
assert_equal "10 transactions deleted", flash[:notice]
assert_equal "#{delete_count} transactions deleted", flash[:notice]
end
test "can update many transactions at once" do
transactions = @user.family.entries.account_transactions.reverse_chronological.limit(20)
transactions.each do |transaction|
transaction.update! \
date: Date.current,
entryable_attributes: {
id: transaction.account_transaction.id,
category_id: Category.first.id,
merchant_id: Merchant.first.id,
notes: "Starting note"
}
end
transactions = @user.family.entries.account_transactions
assert_difference [ "Account::Entry.count", "Account::Transaction.count" ], 0 do
post bulk_update_transactions_url, params: {
@ -173,9 +170,7 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to transactions_url
assert_equal "#{transactions.count} transactions updated", flash[:notice]
transactions.reload
transactions.each do |transaction|
transactions.reload.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