Skip to content

Commit 12237ed

Browse files
authored
fix: only archive envelopes in category when archiving a category (#846)
1 parent 807501d commit 12237ed

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

pkg/models/category.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (c Category) Self() string {
2727
func (c *Category) BeforeUpdate(tx *gorm.DB) (err error) {
2828
if tx.Statement.Changed("Hidden") && !c.Hidden {
2929
var envelopes []Envelope
30-
err = tx.Model(&Envelope{EnvelopeCreate: EnvelopeCreate{
30+
err = tx.Where(&Envelope{EnvelopeCreate: EnvelopeCreate{
3131
CategoryID: c.ID,
3232
}}).
3333
Find(&envelopes).Error

pkg/models/category_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,39 @@ func (suite *TestSuiteStandard) TestCategoryArchiveArchivesEnvelopes() {
2020
err := suite.db.Model(&category).Select("Hidden").Updates(models.Category{CategoryCreate: models.CategoryCreate{Hidden: true}}).Error
2121
assert.Nil(suite.T(), err)
2222

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

29+
func (suite *TestSuiteStandard) TestCategoryArchiveNoEnvelopes() {
30+
budget := suite.createTestBudget(models.BudgetCreate{})
31+
category := suite.createTestCategory(models.CategoryCreate{
32+
BudgetID: budget.ID,
33+
Name: "TestCategoryArchiveNoEnvelopes",
34+
})
35+
36+
category2 := suite.createTestCategory(models.CategoryCreate{
37+
BudgetID: budget.ID,
38+
})
39+
40+
envelope := suite.createTestEnvelope(models.EnvelopeCreate{
41+
CategoryID: category2.ID,
42+
Hidden: false,
43+
})
44+
assert.False(suite.T(), envelope.Hidden, "Envelope archived on creation, it should not be")
45+
46+
// Archive the empty category
47+
err := suite.db.Model(&category).Select("Hidden").Updates(models.Category{CategoryCreate: models.CategoryCreate{Hidden: true}}).Error
48+
assert.Nil(suite.T(), err)
49+
50+
// Verify that the envelope is not archived
51+
err = suite.db.First(&envelope, envelope.ID).Error
52+
assert.Nil(suite.T(), err)
53+
assert.False(suite.T(), envelope.Hidden, "Envelope was archived together with category")
54+
}
55+
2956
func (suite *TestSuiteStandard) TestCategorySetEnvelopes() {
3057
category := suite.createTestCategory(models.CategoryCreate{
3158
BudgetID: suite.createTestBudget(models.BudgetCreate{}).ID,

0 commit comments

Comments
 (0)