1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-23 07:09: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

@ -78,6 +78,6 @@ class Account::Balance::CalculatorTest < ActiveSupport::TestCase
end
def calculated_balances_for(account_key)
Account::Balance::Calculator.new(accounts(account_key)).calculate.daily_balances
Account::Balance::Calculator.new(accounts(account_key)).daily_balances
end
end

View file

@ -0,0 +1,75 @@
require "test_helper"
class Account::EntryTest < ActiveSupport::TestCase
setup do
@entry = account_entries :checking_one
@family = families :dylan_family
end
test "valuations cannot have more than one entry per day" do
new_entry = Account::Entry.new \
entryable: Account::Valuation.new,
date: @entry.date, # invalid
currency: @entry.currency,
amount: @entry.amount
assert new_entry.invalid?
end
test "triggers sync with correct start date when transaction is set to prior date" do
prior_date = @entry.date - 1
@entry.update! date: prior_date
@entry.account.expects(:sync_later).with(prior_date)
@entry.sync_account_later
end
test "triggers sync with correct start date when transaction is set to future date" do
prior_date = @entry.date
@entry.update! date: @entry.date + 1
@entry.account.expects(:sync_later).with(prior_date)
@entry.sync_account_later
end
test "triggers sync with correct start date when transaction deleted" do
prior_entry = account_entries(:checking_two) # 12 days ago
current_entry = account_entries(:checking_one) # 5 days ago
current_entry.destroy!
current_entry.account.expects(:sync_later).with(prior_entry.date)
current_entry.sync_account_later
end
test "can search entries" do
params = { search: "a" }
assert_equal 12, Account::Entry.search(params).size
params = params.merge(categories: [ "Food & Drink" ]) # transaction specific search param
assert_equal 2, Account::Entry.search(params).size
end
test "can calculate total spending for a group of transactions" do
assert_equal Money.new(2135), @family.entries.expense_total("USD")
assert_equal Money.new(1010.85, "EUR"), @family.entries.expense_total("EUR")
end
test "can calculate total income for a group of transactions" do
assert_equal -Money.new(2075), @family.entries.income_total("USD")
assert_equal -Money.new(250, "EUR"), @family.entries.income_total("EUR")
end
# See: https://github.com/maybe-finance/maybe/wiki/vision#signage-of-money
test "transactions with negative amounts are inflows, positive amounts are outflows to an account" do
inflow_transaction = account_entries(:checking_four)
outflow_transaction = account_entries(:checking_five)
assert inflow_transaction.amount < 0
assert inflow_transaction.inflow?
assert outflow_transaction.amount >= 0
assert outflow_transaction.outflow?
end
end

View file

@ -7,6 +7,32 @@ class Account::SyncableTest < ActiveSupport::TestCase
@account = accounts(:savings)
end
test "calculates effective start date of an account" do
assert_equal 31.days.ago.to_date, accounts(:collectable).effective_start_date
assert_equal 31.days.ago.to_date, @account.effective_start_date
end
test "syncs regular account" do
@account.sync
assert_equal "ok", @account.status
assert_equal 32, @account.balances.count
end
test "syncs foreign currency account" do
account = accounts(:eur_checking)
account.sync
assert_equal "ok", account.status
assert_equal 32, account.balances.where(currency: "USD").count
assert_equal 32, account.balances.where(currency: "EUR").count
end
test "syncs multi currency account" do
account = accounts(:multi_currency)
account.sync
assert_equal "ok", account.status
assert_equal 32, account.balances.where(currency: "USD").count
end
test "triggers sync job" do
assert_enqueued_with(job: AccountSyncJob, args: [ @account, Date.current ]) do
@account.sync_later(Date.current)
@ -42,31 +68,26 @@ class Account::SyncableTest < ActiveSupport::TestCase
assert_equal 19500, account.balances.find_by(date: balance_date)[:balance]
end
test "balances before sync start date are not updated after syncing" do
account = accounts(:savings)
balance_date = 10.days.ago
account.balances.create!(date: balance_date, balance: 1000)
account.sync 5.days.ago.to_date
test "can perform a partial sync with a given sync start date" do
# Perform a full sync to populate all balances
@account.sync
assert_equal 1000, account.balances.find_by(date: balance_date)[:balance]
end
# Perform partial sync
sync_start_date = 5.days.ago.to_date
balances_before_sync = @account.balances.to_a
@account.sync sync_start_date
balances_after_sync = @account.reload.balances.to_a
test "balances after sync start date are updated after syncing" do
account = accounts(:savings)
balance_date = 10.days.ago
account.balances.create!(date: balance_date, balance: 1000)
account.sync 20.days.ago.to_date
# Balances on or after should be updated
balances_after_sync.each do |balance_after_sync|
balance_before_sync = balances_before_sync.find { |b| b.date == balance_after_sync.date }
assert_equal 19500, account.balances.find_by(date: balance_date)[:balance]
end
test "balance on the sync date is updated after syncing" do
account = accounts(:savings)
balance_date = 5.days.ago
account.balances.create!(date: balance_date, balance: 1000)
account.sync balance_date.to_date
assert_equal 19700, account.balances.find_by(date: balance_date)[:balance]
if balance_after_sync.date >= sync_start_date
assert balance_before_sync.updated_at < balance_after_sync.updated_at
else
assert_equal balance_before_sync.updated_at, balance_after_sync.updated_at
end
end
end
test "foreign currency account has balances in each currency after syncing" do

View file

@ -1,55 +0,0 @@
require "test_helper"
class Account::TransactionTest < ActiveSupport::TestCase
setup do
@transaction = account_transactions(:checking_one)
@family = families(:dylan_family)
end
# See: https://github.com/maybe-finance/maybe/wiki/vision#signage-of-money
test "negative amounts are inflows, positive amounts are outflows to an account" do
inflow_transaction = account_transactions(:checking_four)
outflow_transaction = account_transactions(:checking_five)
assert inflow_transaction.amount < 0
assert inflow_transaction.inflow?
assert outflow_transaction.amount >= 0
assert outflow_transaction.outflow?
end
test "triggers sync with correct start date when transaction is set to prior date" do
prior_date = @transaction.date - 1
@transaction.update! date: prior_date
@transaction.account.expects(:sync_later).with(prior_date)
@transaction.sync_account_later
end
test "triggers sync with correct start date when transaction is set to future date" do
prior_date = @transaction.date
@transaction.update! date: @transaction.date + 1
@transaction.account.expects(:sync_later).with(prior_date)
@transaction.sync_account_later
end
test "triggers sync with correct start date when transaction deleted" do
prior_transaction = account_transactions(:checking_two) # 12 days ago
current_transaction = account_transactions(:checking_one) # 5 days ago
current_transaction.destroy!
current_transaction.account.expects(:sync_later).with(prior_transaction.date)
current_transaction.sync_account_later
end
test "can calculate total spending for a group of transactions" do
assert_equal Money.new(2135), @family.transactions.expense_total("USD")
assert_equal Money.new(1010.85, "EUR"), @family.transactions.expense_total("EUR")
end
test "can calculate total income for a group of transactions" do
assert_equal -Money.new(2075), @family.transactions.income_total("USD")
assert_equal -Money.new(250, "EUR"), @family.transactions.income_total("EUR")
end
end

View file

@ -3,19 +3,32 @@ require "test_helper"
class Account::TransferTest < ActiveSupport::TestCase
setup do
# Transfers can be posted on different dates
@outflow = accounts(:checking).transactions.create! date: 1.day.ago.to_date, name: "Transfer to Savings", amount: 100, marked_as_transfer: true
@inflow = accounts(:savings).transactions.create! date: Date.current, name: "Transfer from Savings", amount: -100, marked_as_transfer: true
@outflow = accounts(:checking).entries.create! \
date: 1.day.ago.to_date,
name: "Transfer to Savings",
amount: 100,
currency: "USD",
marked_as_transfer: true,
entryable: Account::Transaction.new
@inflow = accounts(:savings).entries.create! \
date: Date.current,
name: "Transfer from Savings",
amount: -100,
currency: "USD",
marked_as_transfer: true,
entryable: Account::Transaction.new
end
test "transfer valid if it has inflow and outflow from different accounts for the same amount" do
transfer = Account::Transfer.create! transactions: [ @inflow, @outflow ]
transfer = Account::Transfer.create! entries: [ @inflow, @outflow ]
assert transfer.valid?
end
test "transfer must have 2 transactions" do
invalid_transfer_1 = Account::Transfer.new transactions: [ @outflow ]
invalid_transfer_2 = Account::Transfer.new transactions: [ @inflow, @outflow, account_transactions(:savings_four) ]
invalid_transfer_1 = Account::Transfer.new entries: [ @outflow ]
invalid_transfer_2 = Account::Transfer.new entries: [ @inflow, @outflow, account_entries(:savings_four) ]
assert invalid_transfer_1.invalid?
assert invalid_transfer_2.invalid?
@ -23,11 +36,24 @@ class Account::TransferTest < ActiveSupport::TestCase
test "transfer cannot have 2 transactions from the same account" do
account = accounts(:checking)
inflow = account.transactions.create! date: Date.current, name: "Inflow", amount: -100
outflow = account.transactions.create! date: Date.current, name: "Outflow", amount: 100
inflow = account.entries.create! \
date: Date.current,
name: "Inflow",
amount: -100,
currency: "USD",
marked_as_transfer: true,
entryable: Account::Transaction.new
outflow = account.entries.create! \
date: Date.current,
name: "Outflow",
amount: 100,
currency: "USD",
marked_as_transfer: true,
entryable: Account::Transaction.new
assert_raise ActiveRecord::RecordInvalid do
Account::Transfer.create! transactions: [ inflow, outflow ]
Account::Transfer.create! entries: [ inflow, outflow ]
end
end
@ -35,7 +61,7 @@ class Account::TransferTest < ActiveSupport::TestCase
@inflow.update! marked_as_transfer: false
assert_raise ActiveRecord::RecordInvalid do
Account::Transfer.create! transactions: [ @inflow, @outflow ]
Account::Transfer.create! entries: [ @inflow, @outflow ]
end
end
@ -43,13 +69,13 @@ class Account::TransferTest < ActiveSupport::TestCase
@outflow.update! amount: 105
assert_raises ActiveRecord::RecordInvalid do
Account::Transfer.create! transactions: [ @inflow, @outflow ]
Account::Transfer.create! entries: [ @inflow, @outflow ]
end
end
test "multi-currency transfer transactions do not have to net to zero" do
@outflow.update! amount: 105, currency: "EUR"
transfer = Account::Transfer.create! transactions: [ @inflow, @outflow ]
transfer = Account::Transfer.create! entries: [ @inflow, @outflow ]
assert transfer.valid?
end

View file

@ -1,39 +0,0 @@
require "test_helper"
class Account::ValuationTest < ActiveSupport::TestCase
setup do
@valuation = account_valuations :savings_one
@family = families :dylan_family
end
test "one valuation per day" do
assert_equal 12.days.ago.to_date, account_valuations(:savings_one).date
invalid_valuation = Account::Valuation.new date: 12.days.ago.to_date, value: 20000
assert invalid_valuation.invalid?
end
test "triggers sync with correct start date when valuation is set to prior date" do
prior_date = @valuation.date - 1
@valuation.update! date: prior_date
@valuation.account.expects(:sync_later).with(prior_date)
@valuation.sync_account_later
end
test "triggers sync with correct start date when valuation is set to future date" do
prior_date = @valuation.date
@valuation.update! date: @valuation.date + 1
@valuation.account.expects(:sync_later).with(prior_date)
@valuation.sync_account_later
end
test "triggers sync with correct start date when valuation deleted" do
prior_valuation = account_valuations :savings_two # 25 days ago
current_valuation = account_valuations :savings_one # 12 days ago
current_valuation.destroy!
current_valuation.account.expects(:sync_later).with(prior_valuation.date)
current_valuation.sync_account_later
end
end

View file

@ -1,7 +0,0 @@
require "test_helper"
class Account::BalanceTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -7,12 +7,6 @@ class AccountTest < ActiveSupport::TestCase
@family = families(:dylan_family)
end
test "new account should be valid" do
assert @account.valid?
assert_not_nil @account.accountable_id
assert_not_nil @account.accountable
end
test "recognizes foreign currency account" do
regular_account = accounts(:checking)
foreign_account = accounts(:eur_checking)
@ -34,20 +28,6 @@ class AccountTest < ActiveSupport::TestCase
assert_not multi_currency_account.foreign_currency?
end
test "syncs regular account" do
@account.sync
assert_equal "ok", @account.status
assert_equal 32, @account.balances.count
end
test "syncs foreign currency account" do
account = accounts(:eur_checking)
account.sync
assert_equal "ok", account.status
assert_equal 32, account.balances.where(currency: "USD").count
assert_equal 32, account.balances.where(currency: "EUR").count
end
test "groups accounts by type" do
@family.accounts.each do |account|
account.sync

View file

@ -46,6 +46,7 @@ class ImportTest < ActiveSupport::TestCase
# "Shopping" is a new category, but should only be created 1x during import
assert_difference \
-> { Account::Transaction.count } => 4,
-> { Account::Entry.count } => 4,
-> { Category.count } => 1,
-> { Tagging.count } => 4,
-> { Tag.count } => 2 do
@ -59,11 +60,13 @@ class ImportTest < ActiveSupport::TestCase
test "publishes a valid import with missing data" do
@empty_import.update! raw_csv_str: valid_csv_with_missing_data
assert_difference -> { Category.count } => 1, -> { Account::Transaction.count } => 2 do
assert_difference -> { Category.count } => 1,
-> { Account::Transaction.count } => 2,
-> { Account::Entry.count } => 2 do
@empty_import.publish
end
assert_not_nil Account::Transaction.find_sole_by(name: Import::FALLBACK_TRANSACTION_NAME)
assert_not_nil Account::Entry.find_sole_by(name: Import::FALLBACK_TRANSACTION_NAME)
@empty_import.reload