Skip to content

Commit

Permalink
Remove context as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
oanatmaria committed Oct 23, 2023
1 parent 967e859 commit e02c6b6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 45 deletions.
20 changes: 9 additions & 11 deletions model/diff/diff.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package diff

import (
"context"

"github.com/aserto-dev/go-directory/pkg/derr"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
Expand All @@ -21,27 +19,27 @@ type Changes struct {
}

type Instances interface {
ObjectsExist(ctx context.Context, objectType string) (bool, error)
RelationsExist(ctx context.Context, objectType, relationName string) (bool, error)
ObjectsExist(objectType string) (bool, error)
RelationsExist(objectType, relationName string) (bool, error)
}

func (d *Diff) Validate(ctx context.Context, dv Instances) error {
func (d *Diff) Validate(dv Instances) error {
var errs error
if err := d.validateObjectTypes(ctx, dv); err != nil {
if err := d.validateObjectTypes(dv); err != nil {
errs = multierror.Append(errs, err)
}

if err := d.validateRelationsTypes(ctx, dv); err != nil {
if err := d.validateRelationsTypes(dv); err != nil {
errs = multierror.Append(errs, err)
}

return errs
}

func (d *Diff) validateObjectTypes(ctx context.Context, dv Instances) error {
func (d *Diff) validateObjectTypes(dv Instances) error {
var errs error
for _, objType := range d.Removed.Objects {
hasInstance, err := dv.ObjectsExist(ctx, objType)
hasInstance, err := dv.ObjectsExist(objType)
if err != nil {
errs = multierror.Append(errs, err)
continue
Expand All @@ -53,11 +51,11 @@ func (d *Diff) validateObjectTypes(ctx context.Context, dv Instances) error {
return errs
}

func (d *Diff) validateRelationsTypes(ctx context.Context, dv Instances) error {
func (d *Diff) validateRelationsTypes(dv Instances) error {
var errs error
for objType, rels := range d.Removed.Relations {
for _, rel := range rels {
hasInstance, err := dv.RelationsExist(ctx, objType, rel)
hasInstance, err := dv.RelationsExist(objType, rel)
if err != nil {
errs = multierror.Append(errs, err)
continue
Expand Down
47 changes: 22 additions & 25 deletions model/diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package diff_test

import (
"context"
"errors"
"testing"

Expand All @@ -11,80 +10,78 @@ import (
"github.com/stretchr/testify/require"
)

var ErrBoom = errors.New("Boom")

func TestValidateDiffNoDeletion(t *testing.T) {
ctrl := gomock.NewController(t)
mockDirectoryValidator := diff.NewMockInstances(ctrl)
mockInstances := diff.NewMockInstances(ctrl)

dif := diff.Diff{Removed: diff.Changes{}, Added: diff.Changes{}}
err := dif.Validate(context.Background(), mockDirectoryValidator)
err := dif.Validate(mockInstances)

require.NoError(t, err)
}

func TestValidateDiffWithObjectTypeDeletion(t *testing.T) {
ctrl := gomock.NewController(t)
mockDirectoryValidator := diff.NewMockInstances(ctrl)
mockInstances := diff.NewMockInstances(ctrl)
objType := "user"
bCtx := context.Background()

dif := diff.Diff{Removed: diff.Changes{Objects: []string{objType}}, Added: diff.Changes{}}

mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objType).Return(false, nil)
err := dif.Validate(bCtx, mockDirectoryValidator)
mockInstances.EXPECT().ObjectsExist(objType).Return(false, nil)
err := dif.Validate(mockInstances)

require.NoError(t, err)
}

func TestValidateDiffWith2ObjectTypeDeletion(t *testing.T) {
ctrl := gomock.NewController(t)
mockDirectoryValidator := diff.NewMockInstances(ctrl)
mockInstances := diff.NewMockInstances(ctrl)
objTypes := []string{"user", "member"}
bCtx := context.Background()

dif := diff.Diff{Removed: diff.Changes{Objects: objTypes}, Added: diff.Changes{}}

mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[0]).Return(false, nil)
mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[1]).Return(true, nil)
err := dif.Validate(bCtx, mockDirectoryValidator)
mockInstances.EXPECT().ObjectsExist(objTypes[0]).Return(false, nil)
mockInstances.EXPECT().ObjectsExist(objTypes[1]).Return(true, nil)
err := dif.Validate(mockInstances)

require.Error(t, err)
require.Contains(t, err.Error(), derr.ErrObjectTypeInUse.Message)
}

func TestValidateDiffWithRelationTypeDeletion(t *testing.T) {
ctrl := gomock.NewController(t)
mockDirectoryValidator := diff.NewMockInstances(ctrl)
mockInstances := diff.NewMockInstances(ctrl)
objTypes := []string{"user", "member"}
relationTypes := map[string][]string{"folder": {"parent_folder"}}
bCtx := context.Background()

dif := diff.Diff{Removed: diff.Changes{Objects: objTypes, Relations: relationTypes}, Added: diff.Changes{}}

mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[0]).Return(false, nil)
mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[1]).Return(false, nil)
mockDirectoryValidator.EXPECT().RelationsExist(bCtx, "folder", relationTypes["folder"][0]).Return(true, nil)
err := dif.Validate(bCtx, mockDirectoryValidator)
mockInstances.EXPECT().ObjectsExist(objTypes[0]).Return(false, nil)
mockInstances.EXPECT().ObjectsExist(objTypes[1]).Return(false, nil)
mockInstances.EXPECT().RelationsExist("folder", relationTypes["folder"][0]).Return(true, nil)
err := dif.Validate(mockInstances)

require.Error(t, err)
require.Contains(t, err.Error(), derr.ErrRelationTypeInUse.Message)
}

func TestValidateDiffWithObjectInstances(t *testing.T) {
ctrl := gomock.NewController(t)
mockDirectoryValidator := diff.NewMockInstances(ctrl)
mockInstances := diff.NewMockInstances(ctrl)
objTypes := []string{"user", "member"}
relationTypes := map[string][]string{"folder": {"parent_folder"}}
bCtx := context.Background()

dif := diff.Diff{Removed: diff.Changes{Objects: objTypes, Relations: relationTypes}, Added: diff.Changes{}}

mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[0]).Return(false, errors.New("Boom!"))
mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[1]).Return(true, nil)
mockDirectoryValidator.EXPECT().RelationsExist(bCtx, "folder", relationTypes["folder"][0]).Return(true, nil)
err := dif.Validate(bCtx, mockDirectoryValidator)
mockInstances.EXPECT().ObjectsExist(objTypes[0]).Return(false, ErrBoom)
mockInstances.EXPECT().ObjectsExist(objTypes[1]).Return(true, nil)
mockInstances.EXPECT().RelationsExist("folder", relationTypes["folder"][0]).Return(true, nil)
err := dif.Validate(mockInstances)

require.Error(t, err)
require.Contains(t, err.Error(), derr.ErrRelationTypeInUse.Message)
require.Contains(t, err.Error(), derr.ErrObjectTypeInUse.Message)
require.Contains(t, err.Error(), "Boom!")
require.Contains(t, err.Error(), ErrBoom.Error())
}
17 changes: 8 additions & 9 deletions model/diff/mock_instances.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e02c6b6

Please sign in to comment.