Skip to content

Commit

Permalink
feat: sorting of categories and envelopes for /v3/months (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
morremeyer authored Jan 1, 2024
1 parent 2b6c52f commit b6ef99f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
22 changes: 16 additions & 6 deletions pkg/controllers/month_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ func (co Controller) GetMonthV3(c *gin.Context) {

// Get all categories for the budget
var categories []models.Category
err = co.DB.Where(&models.Category{CategoryCreate: models.CategoryCreate{BudgetID: b.ID}}).Find(&categories).Error
err = co.DB.
Where(&models.Category{CategoryCreate: models.CategoryCreate{BudgetID: b.ID}}).
Order("name ASC").
Find(&categories).
Error

if err != nil {
e := httperrors.Parse(c, err)
s := e.Error()
Expand All @@ -146,11 +151,16 @@ func (co Controller) GetMonthV3(c *gin.Context) {

var envelopes []models.Envelope

err = co.DB.Where(&models.Envelope{
EnvelopeCreate: models.EnvelopeCreate{
CategoryID: category.ID,
},
}).Find(&envelopes).Error
err = co.DB.
Where(&models.Envelope{
EnvelopeCreate: models.EnvelopeCreate{
CategoryID: category.ID,
},
}).
Order("name asc").
Find(&envelopes).
Error

if err != nil {
e := httperrors.Parse(c, err)
s := e.Error()
Expand Down
29 changes: 29 additions & 0 deletions pkg/controllers/month_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,35 @@ func (suite *TestSuiteStandard) TestMonthsV3PostFails() {
}
}

// TestMonthsV3Sorting verifies that categories and months are sorted correctly
func (suite *TestSuiteStandard) TestMonthsV3Sorting() {
budget := suite.createTestBudgetV3(suite.T(), models.BudgetCreate{})
categoryU := suite.createTestCategoryV3(suite.T(), controllers.CategoryCreateV3{BudgetID: budget.Data.ID, Name: "Upkeep"})
envelopeU := suite.createTestEnvelopeV3(suite.T(), controllers.EnvelopeCreateV3{CategoryID: categoryU.Data.ID, Name: "Utilities"})
envelopeM := suite.createTestEnvelopeV3(suite.T(), controllers.EnvelopeCreateV3{CategoryID: categoryU.Data.ID, Name: "Muppets"})

categoryA := suite.createTestCategoryV3(suite.T(), controllers.CategoryCreateV3{BudgetID: budget.Data.ID, Name: "Alphabetically first"})
envelopeB := suite.createTestEnvelopeV3(suite.T(), controllers.EnvelopeCreateV3{CategoryID: categoryA.Data.ID, Name: "Batteries"})
envelopeC := suite.createTestEnvelopeV3(suite.T(), controllers.EnvelopeCreateV3{CategoryID: categoryA.Data.ID, Name: "Chargers"})

// Get month data
recorder := test.Request(suite.controller, suite.T(), http.MethodGet, strings.Replace(budget.Data.Links.Month, "YYYY-MM", types.MonthOf(time.Now()).String(), 1), "")
assertHTTPStatus(suite.T(), &recorder, http.StatusOK)

// Parse month data
var response controllers.MonthResponseV3
suite.decodeResponse(&recorder, &response)
month := response.Data

assert.Equal(suite.T(), categoryU.Data.ID, month.Categories[1].ID)
assert.Equal(suite.T(), envelopeU.Data.ID, month.Categories[1].Envelopes[1].ID)
assert.Equal(suite.T(), envelopeM.Data.ID, month.Categories[1].Envelopes[0].ID)

assert.Equal(suite.T(), categoryA.Data.ID, month.Categories[0].ID)
assert.Equal(suite.T(), envelopeB.Data.ID, month.Categories[0].Envelopes[0].ID)
assert.Equal(suite.T(), envelopeC.Data.ID, month.Categories[0].Envelopes[1].ID)
}

// TestMonthsV3 verifies that the monthly calculations are correct.
func (suite *TestSuiteStandard) TestMonthsV3() {
budget := suite.createTestBudgetV3(suite.T(), models.BudgetCreate{})
Expand Down

0 comments on commit b6ef99f

Please sign in to comment.