Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
  • Loading branch information
manuio committed Oct 26, 2020
1 parent be55ee5 commit 281e5fe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
4 changes: 2 additions & 2 deletions users/postgres/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ func (gr groupRepository) Update(ctx context.Context, group users.Group) error {
q := `UPDATE groups SET name = :name, metadata = :metadata, description = :description WHERE id = :id;`
dbu, err := toDBGroup(group)
if err != nil {
return errors.Wrap(errUpdateDB, err)
return errors.Wrap(users.ErrUpdateGroup, err)
}

if _, err := gr.db.NamedExecContext(ctx, q, dbu); err != nil {
return errors.Wrap(errUpdateDB, err)
return errors.Wrap(users.ErrUpdateGroup, err)
}

return nil
Expand Down
47 changes: 38 additions & 9 deletions users/postgres/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package postgres_test
import (
"context"
"fmt"
"strings"
"testing"

"github.com/mainflux/mainflux/pkg/errors"
Expand All @@ -18,10 +19,15 @@ import (
)

const (
groupName = "Mainflux"
password = "12345678"
maxNameSize = 254
maxDescSize = 1024
groupName = "Mainflux"
password = "12345678"
)

var invalidName = strings.Repeat("m", maxNameSize+1)
var invalidDesc = strings.Repeat("m", maxDescSize+1)

func TestGroupSave(t *testing.T) {
dbMiddleware := postgres.NewDatabase(db)
repo := postgres.NewGroupRepo(dbMiddleware)
Expand Down Expand Up @@ -152,10 +158,6 @@ func TestGroupUpdate(t *testing.T) {
ID: gid,
Name: groupName,
}
groupUp := users.Group{
ID: gid,
Name: groupName + "-TestGroupUpdate",
}

_, err = groupRepo.Save(context.Background(), group)
require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err))
Expand All @@ -166,9 +168,36 @@ func TestGroupUpdate(t *testing.T) {
err error
}{
{
desc: "update group for existing id",
group: groupUp,
err: nil,
desc: "update group for existing id",
group: users.Group{
ID: gid,
Name: groupName + "-1",
},
err: nil,
},
{
desc: "update group for non-existing id",
group: users.Group{
ID: "wrong",
Name: groupName + "-2",
},
err: users.ErrUpdateGroup,
},
{
desc: "update group for invalid name",
group: users.Group{
ID: gid,
Name: invalidName,
},
err: users.ErrUpdateGroup,
},
{
desc: "update group for invalid description",
group: users.Group{
ID: gid,
Description: invalidDesc,
},
err: users.ErrUpdateGroup,
},
}

Expand Down
3 changes: 3 additions & 0 deletions users/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ var (
// ErrCreateGroup indicates error in creating group.
ErrCreateGroup = errors.New("failed to create group")

// ErrUpdateGroup indicates error in updating group.
ErrUpdateGroup = errors.New("failed to update group")

// ErrDeleteGroupMissing indicates in delete operation that group doesnt exist.
ErrDeleteGroupMissing = errors.New("group is not existing, already deleted")

Expand Down

0 comments on commit 281e5fe

Please sign in to comment.