Skip to content

Commit

Permalink
Merge pull request #31 from Jibaru/fix-04
Browse files Browse the repository at this point in the history
Perform box update validations
  • Loading branch information
Jibaru authored Feb 14, 2024
2 parents 1b1343e + e298ae8 commit 2b35e3e
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 15 deletions.
6 changes: 4 additions & 2 deletions internal/app/application/services/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,10 @@ func (s *BoxService) Update(
return nil, err
}

box.Name = name
box.Description = description
err = box.Update(name, description)
if err != nil {
return nil, err
}

err = s.boxRepository.Update(box)
if err != nil {
Expand Down
67 changes: 54 additions & 13 deletions internal/app/domain/entities/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,75 @@ func NewBox(
description *string,
roomID string,
) (*Box, error) {
box := &Box{
ID: uuid.NewString(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

err := box.Update(name, description)
if err != nil {
return nil, err
}

err = box.ChangeRoomID(roomID)
if err != nil {
return nil, err
}

return box, nil
}

func (b *Box) ChangeName(name string) error {
if strings.TrimSpace(name) == "" {
return nil, ErrBoxNameShouldNotBeEmpty
return ErrBoxNameShouldNotBeEmpty
}

if len(name) > 100 {
return nil, ErrBoxNameShouldHave100OrLessChars
return ErrBoxNameShouldHave100OrLessChars
}

b.Name = name
return nil
}

func (b *Box) ChangeDescription(description *string) error {
if description != nil {
if strings.TrimSpace(*description) == "" {
return nil, ErrBoxDescriptionShouldNotBeEmpty
return ErrBoxDescriptionShouldNotBeEmpty
}

if len(*description) > 255 {
return nil, ErrBoxDescriptionShouldHave255OrLessChars
return ErrBoxDescriptionShouldHave255OrLessChars
}
}

b.Description = description
return nil
}

func (b *Box) ChangeRoomID(roomID string) error {
if strings.TrimSpace(roomID) == "" {
return nil, ErrBoxRoomIDShouldNotBeEmpty
return ErrBoxRoomIDShouldNotBeEmpty
}

b.RoomID = roomID
return nil
}

func (b *Box) Update(
name string,
description *string,
) error {
err := b.ChangeName(name)
if err != nil {
return err
}

err = b.ChangeDescription(description)
if err != nil {
return err
}

return &Box{
ID: uuid.NewString(),
Name: name,
Description: description,
RoomID: roomID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}, nil
return nil
}
97 changes: 97 additions & 0 deletions internal/app/domain/entities/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,100 @@ func TestNewBoxErrorRoomIDShouldNotBeEmpty(t *testing.T) {
assert.Nil(t, box)
assert.ErrorIs(t, err, ErrBoxRoomIDShouldNotBeEmpty)
}

func TestBoxUpdate(t *testing.T) {
name := random.String(100, random.Alphanumeric)
description := random.String(255, random.Alphanumeric)

box := &Box{
ID: uuid.NewString(),
Name: name,
Description: &description,
}

newName := random.String(100, random.Alphanumeric)
newDescription := random.String(255, random.Alphanumeric)

err := box.Update(newName, &newDescription)

assert.NoError(t, err)
assert.Equal(t, newName, box.Name)
assert.NotNil(t, box.Description)
assert.Equal(t, newDescription, *box.Description)
}

func TestBoxUpdateErrorNameShouldNotBeEmpty(t *testing.T) {
name := random.String(100, random.Alphanumeric)
description := random.String(255, random.Alphanumeric)

box := &Box{
ID: uuid.NewString(),
Name: name,
Description: &description,
}

newName := ""
newDescription := random.String(255, random.Alphanumeric)

err := box.Update(newName, &newDescription)

assert.Error(t, err)
assert.ErrorIs(t, err, ErrBoxNameShouldNotBeEmpty)
}

func TestBoxUpdateErrorNameShouldHave100OrLessChars(t *testing.T) {
name := random.String(100, random.Alphanumeric)
description := random.String(255, random.Alphanumeric)

box := &Box{
ID: uuid.NewString(),
Name: name,
Description: &description,
}

newName := random.String(101, random.Alphanumeric)
newDescription := random.String(255, random.Alphanumeric)

err := box.Update(newName, &newDescription)

assert.Error(t, err)
assert.ErrorIs(t, err, ErrBoxNameShouldHave100OrLessChars)
}

func TestBoxUpdateErrorDescriptionShouldNotBeEmpty(t *testing.T) {
name := random.String(100, random.Alphanumeric)
description := random.String(255, random.Alphanumeric)

box := &Box{
ID: uuid.NewString(),
Name: name,
Description: &description,
}

newName := random.String(100, random.Alphanumeric)
newDescription := ""

err := box.Update(newName, &newDescription)

assert.Error(t, err)
assert.ErrorIs(t, err, ErrBoxDescriptionShouldNotBeEmpty)
}

func TestBoxUpdateErrorDescriptionShouldHave255OrLessChars(t *testing.T) {
name := random.String(100, random.Alphanumeric)
description := random.String(255, random.Alphanumeric)

box := &Box{
ID: uuid.NewString(),
Name: name,
Description: &description,
}

newName := random.String(100, random.Alphanumeric)
newDescription := random.String(254, random.Alphanumeric) + random.String(10, random.Alphanumeric)

err := box.Update(newName, &newDescription)

assert.Error(t, err)
assert.ErrorIs(t, err, ErrBoxDescriptionShouldHave255OrLessChars)
}

0 comments on commit 2b35e3e

Please sign in to comment.