File tree Expand file tree Collapse file tree 13 files changed +164
-2
lines changed Expand file tree Collapse file tree 13 files changed +164
-2
lines changed Original file line number Diff line number Diff line change @@ -72,11 +72,21 @@ func (a Account) BeforeUpdate(tx *gorm.DB) (err error) {
7272 return nil
7373}
7474
75- // BeforeSave sets OnBudget to false when External is true.
76- func (a * Account ) BeforeSave (_ * gorm.DB ) (err error ) {
75+ // BeforeSave ensures consistency for the account
76+ //
77+ // It enforces OnBudget to be false when the account
78+ // is external.
79+ //
80+ // It trims whitespace from all strings
81+ func (a * Account ) BeforeSave (_ * gorm.DB ) error {
7782 if a .External {
7883 a .OnBudget = false
7984 }
85+
86+ a .Name = strings .TrimSpace (a .Name )
87+ a .Note = strings .TrimSpace (a .Note )
88+ a .ImportHash = strings .TrimSpace (a .ImportHash )
89+
8090 return nil
8191}
8292
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package models_test
22
33import (
44 "strconv"
5+ "strings"
56 "time"
67
78 "github.com/envelope-zero/backend/v3/internal/types"
@@ -11,6 +12,23 @@ import (
1112 "github.com/stretchr/testify/assert"
1213)
1314
15+ func (suite * TestSuiteStandard ) TestAccountTrimWhitespace () {
16+ name := "\t Whitespace galore! "
17+ note := " Some more whitespace in the notes "
18+ importHash := " 867e3a26dc0baf73f4bff506f31a97f6c32088917e9e5cf1a5ed6f3f84a6fa70 \t "
19+
20+ account := suite .createTestAccount (models.AccountCreate {
21+ Name : name ,
22+ Note : note ,
23+ ImportHash : importHash ,
24+ BudgetID : suite .createTestBudget (models.BudgetCreate {}).ID ,
25+ })
26+
27+ assert .Equal (suite .T (), strings .TrimSpace (name ), account .Name )
28+ assert .Equal (suite .T (), strings .TrimSpace (note ), account .Note )
29+ assert .Equal (suite .T (), strings .TrimSpace (importHash ), account .ImportHash )
30+ }
31+
1432func (suite * TestSuiteStandard ) TestAccountCalculations () {
1533 budget := suite .createTestBudget (models.BudgetCreate {})
1634 initialBalanceDate := time .Now ()
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package models
22
33import (
44 "fmt"
5+ "strings"
56
67 "github.com/envelope-zero/backend/v3/internal/types"
78 "github.com/envelope-zero/backend/v3/pkg/database"
@@ -29,6 +30,14 @@ type BudgetCreate struct {
2930 Currency string `json:"currency" example:"€" default:""` // The currency for the budget
3031}
3132
33+ func (b * Budget ) BeforeSave (_ * gorm.DB ) error {
34+ b .Name = strings .TrimSpace (b .Name )
35+ b .Note = strings .TrimSpace (b .Note )
36+ b .Currency = strings .TrimSpace (b .Currency )
37+
38+ return nil
39+ }
40+
3241// Balance calculates the balance for a budget.
3342func (b Budget ) Balance (tx * gorm.DB ) (balance decimal.Decimal , err error ) {
3443 // Get all OnBudget accounts for the budget
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package models_test
22
33import (
44 "fmt"
5+ "strings"
56 "testing"
67 "time"
78
@@ -11,6 +12,22 @@ import (
1112 "github.com/stretchr/testify/assert"
1213)
1314
15+ func (suite * TestSuiteStandard ) TestBudgetTrimWhitespace () {
16+ name := "\t Whitespace galore! "
17+ note := " Some more whitespace in the notes "
18+ currency := " €"
19+
20+ budget := suite .createTestBudget (models.BudgetCreate {
21+ Name : name ,
22+ Note : note ,
23+ Currency : currency ,
24+ })
25+
26+ assert .Equal (suite .T (), strings .TrimSpace (name ), budget .Name )
27+ assert .Equal (suite .T (), strings .TrimSpace (note ), budget .Note )
28+ assert .Equal (suite .T (), strings .TrimSpace (currency ), budget .Currency )
29+ }
30+
1431func (suite * TestSuiteStandard ) TestBudgetAfterFind () {
1532 budget := suite .createTestBudget (models.BudgetCreate {})
1633 err := budget .AfterFind (suite .db )
Original file line number Diff line number Diff line change 11package models
22
33import (
4+ "strings"
5+
46 "github.com/google/uuid"
57 "gorm.io/gorm"
68)
@@ -24,6 +26,13 @@ func (c Category) Self() string {
2426 return "Category"
2527}
2628
29+ func (c * Category ) BeforeSave (_ * gorm.DB ) error {
30+ c .Name = strings .TrimSpace (c .Name )
31+ c .Note = strings .TrimSpace (c .Note )
32+
33+ return nil
34+ }
35+
2736func (c * Category ) AfterFind (_ * gorm.DB ) (err error ) {
2837 // Set the Archived field to the value of Hidden
2938 c .Archived = c .Hidden
Original file line number Diff line number Diff line change 11package models_test
22
33import (
4+ "strings"
5+
46 "github.com/envelope-zero/backend/v3/pkg/models"
57 "github.com/stretchr/testify/assert"
68)
79
10+ func (suite * TestSuiteStandard ) TestCategoryTrimWhitespace () {
11+ name := "\t Whitespace galore! "
12+ note := " Some more whitespace in the notes "
13+
14+ category := suite .createTestCategory (models.CategoryCreate {
15+ Name : name ,
16+ Note : note ,
17+ BudgetID : suite .createTestBudget (models.BudgetCreate {}).ID ,
18+ })
19+
20+ assert .Equal (suite .T (), strings .TrimSpace (name ), category .Name )
21+ assert .Equal (suite .T (), strings .TrimSpace (note ), category .Note )
22+ }
23+
824func (suite * TestSuiteStandard ) TestCategoryArchiveArchivesEnvelopes () {
925 category := suite .createTestCategory (models.CategoryCreate {
1026 BudgetID : suite .createTestBudget (models.BudgetCreate {}).ID ,
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package models
22
33import (
44 "sort"
5+ "strings"
56 "time"
67
78 "github.com/envelope-zero/backend/v3/internal/types"
@@ -29,6 +30,13 @@ func (e Envelope) Self() string {
2930 return "Envelope"
3031}
3132
33+ func (e * Envelope ) BeforeSave (_ * gorm.DB ) error {
34+ e .Name = strings .TrimSpace (e .Name )
35+ e .Note = strings .TrimSpace (e .Note )
36+
37+ return nil
38+ }
39+
3240func (e * Envelope ) AfterFind (_ * gorm.DB ) (err error ) {
3341 // Set the Archived field to the value of Hidden
3442 e .Archived = e .Hidden
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package models_test
22
33import (
44 "fmt"
5+ "strings"
56 "testing"
67 "time"
78
@@ -11,6 +12,22 @@ import (
1112 "github.com/stretchr/testify/assert"
1213)
1314
15+ func (suite * TestSuiteStandard ) TestEnvelopeTrimWhitespace () {
16+ name := "\t Whitespace galore! "
17+ note := " Some more whitespace in the notes "
18+
19+ envelope := suite .createTestEnvelope (models.EnvelopeCreate {
20+ Name : name ,
21+ Note : note ,
22+ CategoryID : suite .createTestCategory (models.CategoryCreate {
23+ BudgetID : suite .createTestBudget (models.BudgetCreate {}).ID ,
24+ }).ID ,
25+ })
26+
27+ assert .Equal (suite .T (), strings .TrimSpace (name ), envelope .Name )
28+ assert .Equal (suite .T (), strings .TrimSpace (note ), envelope .Note )
29+ }
30+
1431func (suite * TestSuiteStandard ) TestEnvelopeMonthSum () {
1532 budget := suite .createTestBudget (models.BudgetCreate {})
1633
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package models
22
33import (
44 "errors"
5+ "strings"
56
67 "github.com/envelope-zero/backend/v3/internal/types"
78 "github.com/google/uuid"
@@ -34,6 +35,12 @@ func (m MonthConfig) Self() string {
3435 return "Month Config"
3536}
3637
38+ func (m * MonthConfig ) BeforeSave (_ * gorm.DB ) error {
39+ m .Note = strings .TrimSpace (m .Note )
40+
41+ return nil
42+ }
43+
3744func (m * MonthConfig ) AfterFind (tx * gorm.DB ) error {
3845 // Check if there is an allocation for this MonthConfig. If yes, set the value.
3946 // This transparently makes use of the Allocation model
Original file line number Diff line number Diff line change 11package models_test
22
33import (
4+ "strings"
5+
46 "github.com/envelope-zero/backend/v3/pkg/models"
57 "github.com/stretchr/testify/assert"
68)
79
810func (suite * TestSuiteStandard ) TestMonthConfigSelf () {
911 assert .Equal (suite .T (), "Month Config" , models.MonthConfig {}.Self ())
1012}
13+
14+ func (suite * TestSuiteStandard ) TestMonthConfigTrimWhitespace () {
15+ note := " Some more whitespace in the notes "
16+
17+ account := suite .createTestMonthConfig (models.MonthConfig {
18+ MonthConfigCreate : models.MonthConfigCreate {
19+ Note : note ,
20+ },
21+ EnvelopeID : suite .createTestEnvelope (models.EnvelopeCreate {
22+ CategoryID : suite .createTestCategory (models.CategoryCreate {
23+ BudgetID : suite .createTestBudget (models.BudgetCreate {}).ID ,
24+ }).ID ,
25+ }).ID ,
26+ })
27+
28+ assert .Equal (suite .T (), strings .TrimSpace (note ), account .Note )
29+ }
You can’t perform that action at this time.
0 commit comments