github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/formationtemplateconstraintreferences/service_test.go (about)

     1  package formationtemplateconstraintreferences_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplateconstraintreferences"
     8  	"github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplateconstraintreferences/automock"
     9  	"github.com/kyma-incubator/compass/components/director/internal/model"
    10  	"github.com/pkg/errors"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/mock"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestService_Create(t *testing.T) {
    17  	// GIVEN
    18  	ctx := context.TODO()
    19  
    20  	testErr := errors.New("test error")
    21  
    22  	testCases := []struct {
    23  		Name                                   string
    24  		Context                                context.Context
    25  		Input                                  *model.FormationTemplateConstraintReference
    26  		FormationConstraintReferenceRepository func() *automock.FormationTemplateConstraintReferenceRepository
    27  		ExpectedErrorMsg                       string
    28  	}{
    29  		{
    30  			Name:    "Success",
    31  			Context: ctx,
    32  			Input:   constraintReference,
    33  			FormationConstraintReferenceRepository: func() *automock.FormationTemplateConstraintReferenceRepository {
    34  				repo := &automock.FormationTemplateConstraintReferenceRepository{}
    35  				repo.On("Create", ctx, constraintReference).Return(nil).Once()
    36  				return repo
    37  			},
    38  			ExpectedErrorMsg: "",
    39  		},
    40  		{
    41  			Name:    "Error when creating formation constraint",
    42  			Context: ctx,
    43  			Input:   constraintReference,
    44  			FormationConstraintReferenceRepository: func() *automock.FormationTemplateConstraintReferenceRepository {
    45  				repo := &automock.FormationTemplateConstraintReferenceRepository{}
    46  				repo.On("Create", ctx, constraintReference).Return(testErr).Once()
    47  				return repo
    48  			},
    49  			ExpectedErrorMsg: "while creating Formation Template Constraint Reference",
    50  		},
    51  	}
    52  
    53  	for _, testCase := range testCases {
    54  		t.Run(testCase.Name, func(t *testing.T) {
    55  			constraintReferenceRepo := testCase.FormationConstraintReferenceRepository()
    56  
    57  			svc := formationtemplateconstraintreferences.NewService(constraintReferenceRepo, nil)
    58  
    59  			// WHEN
    60  			err := svc.Create(testCase.Context, testCase.Input)
    61  
    62  			// THEN
    63  			if testCase.ExpectedErrorMsg != "" {
    64  				require.Error(t, err)
    65  				assert.Contains(t, err.Error(), testCase.ExpectedErrorMsg)
    66  			} else {
    67  				assert.NoError(t, err)
    68  			}
    69  
    70  			mock.AssertExpectationsForObjects(t, constraintReferenceRepo)
    71  		})
    72  	}
    73  }
    74  
    75  func TestService_Delete(t *testing.T) {
    76  	// GIVEN
    77  	ctx := context.TODO()
    78  
    79  	testErr := errors.New("test error")
    80  
    81  	testCases := []struct {
    82  		Name                                   string
    83  		Context                                context.Context
    84  		FormationConstraintReferenceRepository func() *automock.FormationTemplateConstraintReferenceRepository
    85  		ExpectedErrorMsg                       string
    86  	}{
    87  		{
    88  			Name: "Success",
    89  			FormationConstraintReferenceRepository: func() *automock.FormationTemplateConstraintReferenceRepository {
    90  				repo := &automock.FormationTemplateConstraintReferenceRepository{}
    91  				repo.On("Delete", ctx, templateID, constraintID).Return(nil).Once()
    92  				return repo
    93  			},
    94  			ExpectedErrorMsg: "",
    95  		},
    96  		{
    97  			Name: "Error when creating formation constraint",
    98  			FormationConstraintReferenceRepository: func() *automock.FormationTemplateConstraintReferenceRepository {
    99  				repo := &automock.FormationTemplateConstraintReferenceRepository{}
   100  				repo.On("Delete", ctx, templateID, constraintID).Return(testErr).Once()
   101  				return repo
   102  			},
   103  			ExpectedErrorMsg: "while deleting Formation Template Constraint Reference",
   104  		},
   105  	}
   106  
   107  	for _, testCase := range testCases {
   108  		t.Run(testCase.Name, func(t *testing.T) {
   109  			constraintReferenceRepo := testCase.FormationConstraintReferenceRepository()
   110  
   111  			svc := formationtemplateconstraintreferences.NewService(constraintReferenceRepo, nil)
   112  
   113  			// WHEN
   114  			err := svc.Delete(ctx, constraintID, templateID)
   115  
   116  			// THEN
   117  			if testCase.ExpectedErrorMsg != "" {
   118  				require.Error(t, err)
   119  				assert.Contains(t, err.Error(), testCase.ExpectedErrorMsg)
   120  			} else {
   121  				assert.NoError(t, err)
   122  			}
   123  
   124  			mock.AssertExpectationsForObjects(t, constraintReferenceRepo)
   125  		})
   126  	}
   127  }