Skip to content

Commit 4350a9e

Browse files
authored
test: use createTestX methods of suite (#762)
This uses the createTestX methods of the suite for model tests to make test code shorter and less redundant.
1 parent e2a6eb9 commit 4350a9e

File tree

3 files changed

+115
-265
lines changed

3 files changed

+115
-265
lines changed

pkg/models/account_test.go

Lines changed: 55 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -12,101 +12,56 @@ import (
1212
)
1313

1414
func (suite *TestSuiteStandard) TestAccountCalculations() {
15-
budget := models.Budget{}
16-
err := suite.db.Save(&budget).Error
17-
if err != nil {
18-
suite.Assert().Fail("Resource could not be saved", err)
19-
}
20-
15+
budget := suite.createTestBudget(models.BudgetCreate{})
2116
initialBalanceDate := time.Now()
2217

23-
account := models.Account{
24-
AccountCreate: models.AccountCreate{
25-
Name: "TestAccountCalculations",
26-
BudgetID: budget.ID,
27-
OnBudget: true,
28-
External: false,
29-
InitialBalance: decimal.NewFromFloat(170),
30-
InitialBalanceDate: &initialBalanceDate,
31-
},
32-
}
33-
err = suite.db.Save(&account).Error
34-
if err != nil {
35-
suite.Assert().Fail("Resource could not be saved", err)
36-
}
18+
account := suite.createTestAccount(models.AccountCreate{
19+
Name: "TestAccountCalculations",
20+
BudgetID: budget.ID,
21+
OnBudget: true,
22+
External: false,
23+
InitialBalance: decimal.NewFromFloat(170),
24+
InitialBalanceDate: &initialBalanceDate,
25+
})
3726

38-
externalAccount := models.Account{
39-
AccountCreate: models.AccountCreate{
40-
BudgetID: budget.ID,
41-
External: true,
42-
},
43-
}
44-
err = suite.db.Save(&externalAccount).Error
45-
if err != nil {
46-
suite.Assert().Fail("Resource could not be saved", err)
47-
}
27+
externalAccount := suite.createTestAccount(models.AccountCreate{
28+
BudgetID: budget.ID,
29+
External: true,
30+
})
4831

49-
category := models.Category{
50-
CategoryCreate: models.CategoryCreate{
51-
BudgetID: budget.ID,
52-
},
53-
}
54-
err = suite.db.Save(&category).Error
55-
if err != nil {
56-
suite.Assert().Fail("Resource could not be saved", err)
57-
}
32+
category := suite.createTestCategory(models.CategoryCreate{
33+
BudgetID: budget.ID,
34+
})
5835

59-
envelope := models.Envelope{
60-
EnvelopeCreate: models.EnvelopeCreate{
61-
CategoryID: category.ID,
62-
},
63-
}
64-
err = suite.db.Save(&envelope).Error
65-
if err != nil {
66-
suite.Assert().Fail("Resource could not be saved", err)
67-
}
36+
envelope := suite.createTestEnvelope(models.EnvelopeCreate{
37+
CategoryID: category.ID,
38+
})
6839

69-
incomingTransaction := models.Transaction{
70-
TransactionCreate: models.TransactionCreate{
71-
BudgetID: budget.ID,
72-
EnvelopeID: &envelope.ID,
73-
SourceAccountID: externalAccount.ID,
74-
DestinationAccountID: account.ID,
75-
Reconciled: true,
76-
Amount: decimal.NewFromFloat(32.17),
77-
},
78-
}
79-
err = suite.db.Save(&incomingTransaction).Error
80-
if err != nil {
81-
suite.Assert().Fail("Resource could not be saved", err)
82-
}
40+
incomingTransaction := suite.createTestTransaction(models.TransactionCreate{
41+
BudgetID: budget.ID,
42+
EnvelopeID: &envelope.ID,
43+
SourceAccountID: externalAccount.ID,
44+
DestinationAccountID: account.ID,
45+
Reconciled: true,
46+
Amount: decimal.NewFromFloat(32.17),
47+
})
8348

84-
outgoingTransaction := models.Transaction{
85-
TransactionCreate: models.TransactionCreate{
86-
BudgetID: budget.ID,
87-
EnvelopeID: &envelope.ID,
88-
SourceAccountID: account.ID,
89-
DestinationAccountID: externalAccount.ID,
90-
Amount: decimal.NewFromFloat(17.45),
91-
},
92-
}
93-
err = suite.db.Save(&outgoingTransaction).Error
94-
if err != nil {
95-
suite.Assert().Fail("Resource could not be saved", err)
96-
}
49+
outgoingTransaction := suite.createTestTransaction(models.TransactionCreate{
50+
BudgetID: budget.ID,
51+
EnvelopeID: &envelope.ID,
52+
SourceAccountID: account.ID,
53+
DestinationAccountID: externalAccount.ID,
54+
Amount: decimal.NewFromFloat(17.45),
55+
})
9756

98-
futureIncomeTransaction := suite.createTestTransaction(models.TransactionCreate{
57+
_ = suite.createTestTransaction(models.TransactionCreate{
9958
BudgetID: budget.ID,
10059
SourceAccountID: externalAccount.ID,
10160
DestinationAccountID: account.ID,
10261
Amount: decimal.NewFromFloat(100),
10362
AvailableFrom: types.MonthOf(time.Now()).AddDate(0, 1),
10463
Note: "Future Income Transaction",
10564
})
106-
err = suite.db.Save(&futureIncomeTransaction).Error
107-
if err != nil {
108-
suite.Assert().Fail("Resource could not be saved", err)
109-
}
11065

11166
a, err := account.WithCalculations(suite.db)
11267
assert.Nil(suite.T(), err)
@@ -179,11 +134,7 @@ func (suite *TestSuiteStandard) TestAccountOnBudget() {
179134
}
180135

181136
func (suite *TestSuiteStandard) TestAccountRecentEnvelopes() {
182-
budget := models.Budget{}
183-
err := suite.db.Save(&budget).Error
184-
if err != nil {
185-
suite.Assert().Fail("Budget could not be saved", err)
186-
}
137+
budget := suite.createTestBudget(models.BudgetCreate{})
187138

188139
account := suite.createTestAccount(models.AccountCreate{
189140
BudgetID: budget.ID,
@@ -205,15 +156,10 @@ func (suite *TestSuiteStandard) TestAccountRecentEnvelopes() {
205156

206157
envelopeIDs := []*uuid.UUID{}
207158
for i := 1; i <= 3; i++ {
208-
envelope := &models.Envelope{
209-
EnvelopeCreate: models.EnvelopeCreate{
210-
CategoryID: category.ID,
211-
Name: strconv.Itoa(i),
212-
},
213-
}
214-
if err = suite.db.Save(&envelope).Error; err != nil {
215-
suite.Assert().Fail("Resource could not be saved", err)
216-
}
159+
envelope := suite.createTestEnvelope(models.EnvelopeCreate{
160+
CategoryID: category.ID,
161+
Name: strconv.Itoa(i),
162+
})
217163

218164
envelopeIDs = append(envelopeIDs, &envelope.ID)
219165
}
@@ -227,19 +173,13 @@ func (suite *TestSuiteStandard) TestAccountRecentEnvelopes() {
227173
if i > 5 {
228174
eIndex = 2
229175
}
230-
transaction := models.Transaction{
231-
TransactionCreate: models.TransactionCreate{
232-
BudgetID: budget.ID,
233-
EnvelopeID: envelopeIDs[eIndex%3],
234-
SourceAccountID: account.ID,
235-
DestinationAccountID: externalAccount.ID,
236-
Amount: decimal.NewFromFloat(17.45),
237-
},
238-
}
239-
err = suite.db.Save(&transaction).Error
240-
if err != nil {
241-
suite.Assert().FailNow("Resource could not be saved", err)
242-
}
176+
_ = suite.createTestTransaction(models.TransactionCreate{
177+
BudgetID: budget.ID,
178+
EnvelopeID: envelopeIDs[eIndex%3],
179+
SourceAccountID: account.ID,
180+
DestinationAccountID: externalAccount.ID,
181+
Amount: decimal.NewFromFloat(17.45),
182+
})
243183
}
244184

245185
recent, err := externalAccount.RecentEnvelopes(suite.db)
@@ -272,22 +212,12 @@ func (suite *TestSuiteStandard) TestAccountGetBalanceMonthDBFail() {
272212

273213
// TestAccountDuplicateNames ensures that two accounts cannot have the same name.
274214
func (suite *TestSuiteStandard) TestAccountDuplicateNames() {
275-
budget := models.Budget{}
276-
err := suite.db.Save(&budget).Error
277-
if err != nil {
278-
suite.Assert().Fail("Resource could not be saved", err)
279-
}
215+
budget := suite.createTestBudget(models.BudgetCreate{})
280216

281-
account := models.Account{
282-
AccountCreate: models.AccountCreate{
283-
BudgetID: budget.ID,
284-
Name: "TestAccountDuplicateNames",
285-
},
286-
}
287-
err = suite.db.Save(&account).Error
288-
if err != nil {
289-
suite.Assert().Fail("Resource could not be saved", err)
290-
}
217+
_ = suite.createTestAccount(models.AccountCreate{
218+
BudgetID: budget.ID,
219+
Name: "TestAccountDuplicateNames",
220+
})
291221

292222
externalAccount := models.Account{
293223
AccountCreate: models.AccountCreate{
@@ -296,7 +226,7 @@ func (suite *TestSuiteStandard) TestAccountDuplicateNames() {
296226
External: true,
297227
},
298228
}
299-
err = suite.db.Save(&externalAccount).Error
229+
err := suite.db.Save(&externalAccount).Error
300230
if err == nil {
301231
suite.Assert().Fail("Account with the same name than another account could be saved. This must not be possible", err)
302232
return
@@ -351,11 +281,11 @@ func (suite *TestSuiteStandard) TestAccountOnBudgetToOnBudgetTransactionsNoEnvel
351281

352282
// Update the envelope for the transaction
353283
t.EnvelopeID = nil
354-
err = suite.db.Save(&t).Error
284+
err = suite.db.Model(&t).Updates(&t).Error
355285
assert.Nil(suite.T(), err, "Transaction could not be updated")
356286

357287
// Save again
358-
err = suite.db.Save(&transferTargetAccount).Error
288+
err = suite.db.Model(&transferTargetAccount).Updates(&transferTargetAccount).Error
359289
assert.Nil(suite.T(), err, "Target account could not be updated despite transaction having its envelope removed")
360290
}
361291

pkg/models/budget_test.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,43 +174,29 @@ func (suite *TestSuiteStandard) TestBudgetCalculations() {
174174
}
175175

176176
func (suite *TestSuiteStandard) TestMonthIncomeNoTransactions() {
177-
budget := models.Budget{}
178-
err := suite.db.Save(&budget).Error
179-
if err != nil {
180-
suite.Assert().Fail("Resource could not be saved", err)
181-
}
177+
budget := suite.createTestBudget(models.BudgetCreate{})
182178

183179
income, err := budget.Income(suite.db, types.NewMonth(2022, 3))
184180
assert.Nil(suite.T(), err)
185181
assert.True(suite.T(), income.IsZero(), "Income is %s, should be 0", income)
186182
}
187183

188184
func (suite *TestSuiteStandard) TestBudgetIncomeDBFail() {
189-
budget := models.Budget{}
190-
191-
err := suite.db.Save(&budget).Error
192-
if err != nil {
193-
suite.Assert().Fail("Resource could not be saved", err)
194-
}
185+
budget := suite.createTestBudget(models.BudgetCreate{})
195186

196187
suite.CloseDB()
197188

198-
_, err = budget.Income(suite.db, types.NewMonth(1995, 2))
189+
_, err := budget.Income(suite.db, types.NewMonth(1995, 2))
199190
suite.Assert().NotNil(err)
200191
suite.Assert().Equal("sql: database is closed", err.Error())
201192
}
202193

203194
func (suite *TestSuiteStandard) TestBudgetBudgetedDBFail() {
204-
budget := models.Budget{}
205-
206-
err := suite.db.Save(&budget).Error
207-
if err != nil {
208-
suite.Assert().Fail("Resource could not be saved", err)
209-
}
195+
budget := suite.createTestBudget(models.BudgetCreate{})
210196

211197
suite.CloseDB()
212198

213-
_, err = budget.Allocated(suite.db, types.NewMonth(200, 2))
199+
_, err := budget.Allocated(suite.db, types.NewMonth(200, 2))
214200
suite.Assert().NotNil(err)
215201
suite.Assert().Equal("sql: database is closed", err.Error())
216202
}

0 commit comments

Comments
 (0)