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

Fix query when account has zero income and expense (#1112)

* Reproduce error

* Apply fix

* Remove uneeded helper
This commit is contained in:
Zach Gollwitzer 2024-08-20 15:44:32 -04:00 committed by GitHub
parent 793a6027a3
commit 37ae51f68a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 9 deletions

View file

@ -46,7 +46,8 @@ class Family < ApplicationRecord
.where("account_entries.date <= ?", period.date_range.end)
.where("account_entries.marked_as_transfer = ?", false)
.where("account_entries.entryable_type = ?", "Account::Transaction")
.group("id")
.group("accounts.id")
.having("SUM(ABS(account_entries.amount)) > 0")
.to_a
results.each do |r|

View file

@ -105,25 +105,24 @@ class FamilyTest < ActiveSupport::TestCase
test "calculates top movers" do
checking_account = create_account(balance: 500, accountable: Depository.new)
savings_account = create_account(balance: 1000, accountable: Depository.new)
create_transaction(account: checking_account, date: 2.days.ago.to_date, amount: -1000)
create_transaction(account: checking_account, date: 1.day.ago.to_date, amount: 10)
create_transaction(account: savings_account, date: 2.days.ago.to_date, amount: -5000)
zero_income_zero_expense_account = create_account(balance: 200, accountable: Depository.new)
create_transaction(account: zero_income_zero_expense_account, amount: 0)
snapshot = @family.snapshot_account_transactions
top_spenders = snapshot[:top_spenders]
top_earners = snapshot[:top_earners]
top_savers = snapshot[:top_savers]
assert_equal 10, top_spenders.first.spending
assert_equal 5000, top_earners.first.income
assert_equal 1000, top_earners.second.income
assert_equal 1, top_savers.first.savings_rate
assert_equal ((1000 - 10).to_f / 1000), top_savers.second.savings_rate
assert_equal [ 10 ], top_spenders.map(&:spending)
assert_equal [ 5000, 1000 ], top_earners.map(&:income)
assert_equal [ 1, 0.99 ], top_savers.map(&:savings_rate)
end
test "calculates rolling transaction totals" do
account = create_account(balance: 1000, accountable: Depository.new)
create_transaction(account: account, date: 2.days.ago.to_date, amount: -500)