Skip to content

Commit d44057b

Browse files
committed
fix: only calculate with transactions for the budget
When calculating sums over transactions for a budget, the query should filter transactions by the budget.
1 parent f576b79 commit d44057b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/models/budget.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ func (b Budget) Income(t time.Time) (decimal.Decimal, error) {
6767
Where("transactions.envelope_id IS NULL").
6868
Where("strftime('%m', transactions.date) = ?", fmt.Sprintf("%02d", t.Month())).
6969
Where("strftime('%Y', transactions.date) = ?", fmt.Sprintf("%d", t.Year())).
70+
Where(&Transaction{
71+
TransactionCreate: TransactionCreate{
72+
BudgetID: b.ID,
73+
},
74+
}).
7075
Table("transactions").
7176
Find(&income).
7277
Error
@@ -92,6 +97,11 @@ func (b Budget) TotalIncome() (decimal.Decimal, error) {
9297
Where("source_account.external = 1").
9398
Where("destination_account.external = 0").
9499
Where("transactions.envelope_id IS NULL").
100+
Where(&Transaction{
101+
TransactionCreate: TransactionCreate{
102+
BudgetID: b.ID,
103+
},
104+
}).
95105
Table("transactions").
96106
Find(&income).
97107
Error

pkg/models/budget_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ func (suite *TestSuiteEnv) TestBudgetCalculations() {
1818
suite.Assert().Fail("Resource could not be saved", err)
1919
}
2020

21+
emptyBudget := models.Budget{}
22+
err = database.DB.Save(&emptyBudget).Error
23+
if err != nil {
24+
suite.Assert().Fail("Resource could not be saved", err)
25+
}
26+
2127
bankAccount := models.Account{
2228
AccountCreate: models.AccountCreate{
2329
BudgetID: budget.ID,
@@ -149,15 +155,27 @@ func (suite *TestSuiteEnv) TestBudgetCalculations() {
149155
shouldBalance := decimal.NewFromFloat(5489.38)
150156
assert.True(suite.T(), budget.Balance.Equal(shouldBalance), "Balance for budget is not correct. Should be %s, is %s", shouldBalance, budget.Balance)
151157

158+
// Verify income for used budget in March
152159
shouldIncome := decimal.NewFromFloat(2800)
153160
income, err := budget.Income(marchFifteenthTwentyTwentyTwo)
154161
assert.Nil(suite.T(), err)
155162
assert.True(suite.T(), income.Equal(shouldIncome), "Income is %s, should be %s", income, shouldIncome)
156163

164+
// Verify total income for used budget
157165
shouldIncomeTotal := decimal.NewFromFloat(5600)
158166
income, err = budget.TotalIncome()
159167
assert.Nil(suite.T(), err)
160168
assert.True(suite.T(), income.Equal(shouldIncomeTotal), "Income is %s, should be %s", income, shouldIncomeTotal)
169+
170+
// Verify income for empty budget in March
171+
income, err = emptyBudget.Income(marchFifteenthTwentyTwentyTwo)
172+
assert.Nil(suite.T(), err)
173+
assert.True(suite.T(), income.IsZero(), "Income is %s, should be 0", income)
174+
175+
// Verify total income for empty budget
176+
income, err = emptyBudget.TotalIncome()
177+
assert.Nil(suite.T(), err)
178+
assert.True(suite.T(), income.IsZero(), "Income is %s, should be 0", income)
161179
}
162180

163181
func (suite *TestSuiteEnv) TestMonthIncomeNoTransactions() {

0 commit comments

Comments
 (0)