Skip to content

Commit

Permalink
fix: only archive envelopes in category when archiving a category (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekeih authored Nov 18, 2023
1 parent 807501d commit 12237ed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/models/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c Category) Self() string {
func (c *Category) BeforeUpdate(tx *gorm.DB) (err error) {
if tx.Statement.Changed("Hidden") && !c.Hidden {
var envelopes []Envelope
err = tx.Model(&Envelope{EnvelopeCreate: EnvelopeCreate{
err = tx.Where(&Envelope{EnvelopeCreate: EnvelopeCreate{
CategoryID: c.ID,
}}).
Find(&envelopes).Error
Expand Down
29 changes: 28 additions & 1 deletion pkg/models/category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,39 @@ func (suite *TestSuiteStandard) TestCategoryArchiveArchivesEnvelopes() {
err := suite.db.Model(&category).Select("Hidden").Updates(models.Category{CategoryCreate: models.CategoryCreate{Hidden: true}}).Error
assert.Nil(suite.T(), err)

// Verify that the envelope is not archived
// Verify that the envelope is archived
err = suite.db.First(&envelope, envelope.ID).Error
assert.Nil(suite.T(), err)
assert.True(suite.T(), envelope.Hidden, "Envelope was not archived together with category")
}

func (suite *TestSuiteStandard) TestCategoryArchiveNoEnvelopes() {
budget := suite.createTestBudget(models.BudgetCreate{})
category := suite.createTestCategory(models.CategoryCreate{
BudgetID: budget.ID,
Name: "TestCategoryArchiveNoEnvelopes",
})

category2 := suite.createTestCategory(models.CategoryCreate{
BudgetID: budget.ID,
})

envelope := suite.createTestEnvelope(models.EnvelopeCreate{
CategoryID: category2.ID,
Hidden: false,
})
assert.False(suite.T(), envelope.Hidden, "Envelope archived on creation, it should not be")

// Archive the empty category
err := suite.db.Model(&category).Select("Hidden").Updates(models.Category{CategoryCreate: models.CategoryCreate{Hidden: true}}).Error
assert.Nil(suite.T(), err)

// Verify that the envelope is not archived
err = suite.db.First(&envelope, envelope.ID).Error
assert.Nil(suite.T(), err)
assert.False(suite.T(), envelope.Hidden, "Envelope was archived together with category")
}

func (suite *TestSuiteStandard) TestCategorySetEnvelopes() {
category := suite.createTestCategory(models.CategoryCreate{
BudgetID: suite.createTestBudget(models.BudgetCreate{}).ID,
Expand Down

0 comments on commit 12237ed

Please sign in to comment.