Skip to content

Commit

Permalink
feat: trim whitespace from string fields (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
morremeyer authored Dec 24, 2023
1 parent 0b5e1e8 commit d2d9abd
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ func (a Account) BeforeUpdate(tx *gorm.DB) (err error) {
return nil
}

// BeforeSave sets OnBudget to false when External is true.
func (a *Account) BeforeSave(_ *gorm.DB) (err error) {
// BeforeSave ensures consistency for the account
//
// It enforces OnBudget to be false when the account
// is external.
//
// It trims whitespace from all strings
func (a *Account) BeforeSave(_ *gorm.DB) error {
if a.External {
a.OnBudget = false
}

a.Name = strings.TrimSpace(a.Name)
a.Note = strings.TrimSpace(a.Note)
a.ImportHash = strings.TrimSpace(a.ImportHash)

return nil
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/models/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models_test

import (
"strconv"
"strings"
"time"

"github.com/envelope-zero/backend/v3/internal/types"
Expand All @@ -11,6 +12,23 @@ import (
"github.com/stretchr/testify/assert"
)

func (suite *TestSuiteStandard) TestAccountTrimWhitespace() {
name := "\t Whitespace galore! "
note := " Some more whitespace in the notes "
importHash := " 867e3a26dc0baf73f4bff506f31a97f6c32088917e9e5cf1a5ed6f3f84a6fa70 \t"

account := suite.createTestAccount(models.AccountCreate{
Name: name,
Note: note,
ImportHash: importHash,
BudgetID: suite.createTestBudget(models.BudgetCreate{}).ID,
})

assert.Equal(suite.T(), strings.TrimSpace(name), account.Name)
assert.Equal(suite.T(), strings.TrimSpace(note), account.Note)
assert.Equal(suite.T(), strings.TrimSpace(importHash), account.ImportHash)
}

func (suite *TestSuiteStandard) TestAccountCalculations() {
budget := suite.createTestBudget(models.BudgetCreate{})
initialBalanceDate := time.Now()
Expand Down
9 changes: 9 additions & 0 deletions pkg/models/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"fmt"
"strings"

"github.com/envelope-zero/backend/v3/internal/types"
"github.com/envelope-zero/backend/v3/pkg/database"
Expand Down Expand Up @@ -29,6 +30,14 @@ type BudgetCreate struct {
Currency string `json:"currency" example:"€" default:""` // The currency for the budget
}

func (b *Budget) BeforeSave(_ *gorm.DB) error {
b.Name = strings.TrimSpace(b.Name)
b.Note = strings.TrimSpace(b.Note)
b.Currency = strings.TrimSpace(b.Currency)

return nil
}

// Balance calculates the balance for a budget.
func (b Budget) Balance(tx *gorm.DB) (balance decimal.Decimal, err error) {
// Get all OnBudget accounts for the budget
Expand Down
17 changes: 17 additions & 0 deletions pkg/models/budget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models_test

import (
"fmt"
"strings"
"testing"
"time"

Expand All @@ -11,6 +12,22 @@ import (
"github.com/stretchr/testify/assert"
)

func (suite *TestSuiteStandard) TestBudgetTrimWhitespace() {
name := "\t Whitespace galore! "
note := " Some more whitespace in the notes "
currency := " €"

budget := suite.createTestBudget(models.BudgetCreate{
Name: name,
Note: note,
Currency: currency,
})

assert.Equal(suite.T(), strings.TrimSpace(name), budget.Name)
assert.Equal(suite.T(), strings.TrimSpace(note), budget.Note)
assert.Equal(suite.T(), strings.TrimSpace(currency), budget.Currency)
}

func (suite *TestSuiteStandard) TestBudgetAfterFind() {
budget := suite.createTestBudget(models.BudgetCreate{})
err := budget.AfterFind(suite.db)
Expand Down
9 changes: 9 additions & 0 deletions pkg/models/category.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import (
"strings"

"github.com/google/uuid"
"gorm.io/gorm"
)
Expand All @@ -24,6 +26,13 @@ func (c Category) Self() string {
return "Category"
}

func (c *Category) BeforeSave(_ *gorm.DB) error {
c.Name = strings.TrimSpace(c.Name)
c.Note = strings.TrimSpace(c.Note)

return nil
}

func (c *Category) AfterFind(_ *gorm.DB) (err error) {
// Set the Archived field to the value of Hidden
c.Archived = c.Hidden
Expand Down
16 changes: 16 additions & 0 deletions pkg/models/category_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package models_test

import (
"strings"

"github.com/envelope-zero/backend/v3/pkg/models"
"github.com/stretchr/testify/assert"
)

func (suite *TestSuiteStandard) TestCategoryTrimWhitespace() {
name := "\t Whitespace galore! "
note := " Some more whitespace in the notes "

category := suite.createTestCategory(models.CategoryCreate{
Name: name,
Note: note,
BudgetID: suite.createTestBudget(models.BudgetCreate{}).ID,
})

assert.Equal(suite.T(), strings.TrimSpace(name), category.Name)
assert.Equal(suite.T(), strings.TrimSpace(note), category.Note)
}

func (suite *TestSuiteStandard) TestCategoryArchiveArchivesEnvelopes() {
category := suite.createTestCategory(models.CategoryCreate{
BudgetID: suite.createTestBudget(models.BudgetCreate{}).ID,
Expand Down
8 changes: 8 additions & 0 deletions pkg/models/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"sort"
"strings"
"time"

"github.com/envelope-zero/backend/v3/internal/types"
Expand Down Expand Up @@ -29,6 +30,13 @@ func (e Envelope) Self() string {
return "Envelope"
}

func (e *Envelope) BeforeSave(_ *gorm.DB) error {
e.Name = strings.TrimSpace(e.Name)
e.Note = strings.TrimSpace(e.Note)

return nil
}

func (e *Envelope) AfterFind(_ *gorm.DB) (err error) {
// Set the Archived field to the value of Hidden
e.Archived = e.Hidden
Expand Down
17 changes: 17 additions & 0 deletions pkg/models/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models_test

import (
"fmt"
"strings"
"testing"
"time"

Expand All @@ -11,6 +12,22 @@ import (
"github.com/stretchr/testify/assert"
)

func (suite *TestSuiteStandard) TestEnvelopeTrimWhitespace() {
name := "\t Whitespace galore! "
note := " Some more whitespace in the notes "

envelope := suite.createTestEnvelope(models.EnvelopeCreate{
Name: name,
Note: note,
CategoryID: suite.createTestCategory(models.CategoryCreate{
BudgetID: suite.createTestBudget(models.BudgetCreate{}).ID,
}).ID,
})

assert.Equal(suite.T(), strings.TrimSpace(name), envelope.Name)
assert.Equal(suite.T(), strings.TrimSpace(note), envelope.Note)
}

func (suite *TestSuiteStandard) TestEnvelopeMonthSum() {
budget := suite.createTestBudget(models.BudgetCreate{})

Expand Down
7 changes: 7 additions & 0 deletions pkg/models/month_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"errors"
"strings"

"github.com/envelope-zero/backend/v3/internal/types"
"github.com/google/uuid"
Expand Down Expand Up @@ -34,6 +35,12 @@ func (m MonthConfig) Self() string {
return "Month Config"
}

func (m *MonthConfig) BeforeSave(_ *gorm.DB) error {
m.Note = strings.TrimSpace(m.Note)

return nil
}

func (m *MonthConfig) AfterFind(tx *gorm.DB) error {
// Check if there is an allocation for this MonthConfig. If yes, set the value.
// This transparently makes use of the Allocation model
Expand Down
19 changes: 19 additions & 0 deletions pkg/models/month_config_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
package models_test

import (
"strings"

"github.com/envelope-zero/backend/v3/pkg/models"
"github.com/stretchr/testify/assert"
)

func (suite *TestSuiteStandard) TestMonthConfigSelf() {
assert.Equal(suite.T(), "Month Config", models.MonthConfig{}.Self())
}

func (suite *TestSuiteStandard) TestMonthConfigTrimWhitespace() {
note := " Some more whitespace in the notes "

account := suite.createTestMonthConfig(models.MonthConfig{
MonthConfigCreate: models.MonthConfigCreate{
Note: note,
},
EnvelopeID: suite.createTestEnvelope(models.EnvelopeCreate{
CategoryID: suite.createTestCategory(models.CategoryCreate{
BudgetID: suite.createTestBudget(models.BudgetCreate{}).ID,
}).ID,
}).ID,
})

assert.Equal(suite.T(), strings.TrimSpace(note), account.Note)
}
9 changes: 9 additions & 0 deletions pkg/models/test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,12 @@ func (suite *TestSuiteStandard) createTestTransaction(c models.TransactionCreate

return transaction
}

func (suite *TestSuiteStandard) createTestMonthConfig(c models.MonthConfig) models.MonthConfig {
err := suite.db.Save(&c).Error
if err != nil {
suite.Assert().FailNow("MonthConfig could not be saved", "Error: %s, Transaction: %#v", err, c)
}

return c
}
5 changes: 5 additions & 0 deletions pkg/models/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"fmt"
"strings"
"time"

"github.com/envelope-zero/backend/v3/internal/types"
Expand Down Expand Up @@ -61,7 +62,11 @@ func (t *Transaction) AfterFind(tx *gorm.DB) (err error) {
// BeforeSave
// - sets the timezone for the Date for UTC
// - ensures that ReconciledSource and ReconciledDestination are set to valid values
// - trims whitespace from string fields
func (t *Transaction) BeforeSave(tx *gorm.DB) (err error) {
t.Note = strings.TrimSpace(t.Note)
t.ImportHash = strings.TrimSpace(t.ImportHash)

// Ensure that the Envelope ID is nil and not a pointer to a nil UUID
// when it is set
if t.EnvelopeID != nil && *t.EnvelopeID == uuid.Nil {
Expand Down
18 changes: 18 additions & 0 deletions pkg/models/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ import (
"github.com/stretchr/testify/assert"
)

func (suite *TestSuiteStandard) TestTransactionTrimWhitespace() {
note := " Some more whitespace in the notes "
importHash := " 867e3a26dc0baf73f4bff506f31a97f6c32088917e9e5cf1a5ed6f3f84a6fa70 \t"

budgetID := suite.createTestBudget(models.BudgetCreate{}).ID

transaction := suite.createTestTransaction(models.TransactionCreate{
Note: note,
ImportHash: importHash,
BudgetID: budgetID,
SourceAccountID: suite.createTestAccount(models.AccountCreate{BudgetID: budgetID}).ID,
DestinationAccountID: suite.createTestAccount(models.AccountCreate{BudgetID: budgetID}).ID,
})

assert.Equal(suite.T(), strings.TrimSpace(note), transaction.Note)
assert.Equal(suite.T(), strings.TrimSpace(importHash), transaction.ImportHash)
}

func (suite *TestSuiteStandard) TestTransactionFindTimeUTC() {
tz, _ := time.LoadLocation("Europe/Berlin")

Expand Down

0 comments on commit d2d9abd

Please sign in to comment.