Skip to content

Commit

Permalink
coverage test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifosmin Simon committed Nov 25, 2024
1 parent ba9bd6b commit 886118d
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions core/provider/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *ServiceTestSuite) TestCreate() {
expectedError := errors.New("error from repository")
s.mockProvider.On("GetAccountTypes").Return([]string{"user"}).Once()
s.mockProvider.On("CreateConfig", mock.Anything).Return(nil).Once()
s.mockProviderRepository.On("Create", mockCtx, mock.Anything).Return(expectedError).Once()
s.mockProviderRepository.EXPECT().Create(mockCtx, mock.Anything).Return(expectedError).Once()

actualError := s.service.Create(context.Background(), p)

Expand All @@ -129,13 +129,12 @@ func (s *ServiceTestSuite) TestCreate() {
s.mockProviderRepository.EXPECT().Create(mockCtx, p).Return(nil).Once()
s.mockAuditLogger.On("Log", mock.Anything, provider.AuditKeyCreate, mock.Anything).Return(nil).Once()

expectedResources := []*domain.Resource{}
s.mockResourceService.On("Find", mock.Anything, domain.ListResourcesFilter{
ProviderType: p.Type,
ProviderURN: p.URN,
}).Return([]*domain.Resource{}, nil).Once()
s.mockProvider.On("GetResources", mockCtx, p.Config).Return(expectedResources, nil).Once()
s.mockResourceService.On("BulkUpsert", mock.Anything, expectedResources).Return(nil).Once()
s.mockProvider.On("GetResources", mockCtx, p.Config).Return(nil, nil).Once()
s.mockResourceService.On("BulkUpsert", mock.Anything, nil).Return(nil).Once()

actualError := s.service.Create(context.Background(), p)

Expand Down Expand Up @@ -281,7 +280,7 @@ func (s *ServiceTestSuite) TestUpdate() {
for _, tc := range testCases {
s.mockProvider.On("GetAccountTypes").Return([]string{"user"}).Once()
s.mockProvider.On("CreateConfig", mock.Anything).Return(nil).Once()
s.mockProviderRepository.On("Update", mock.MatchedBy(func(ctx context.Context) bool { return true }), tc.expectedNewProvider).Return(nil)
s.mockProviderRepository.EXPECT().Update(mock.MatchedBy(func(ctx context.Context) bool { return true }), tc.expectedNewProvider).Return(nil)
s.mockAuditLogger.On("Log", mock.Anything, provider.AuditKeyUpdate, mock.Anything).Return(nil).Once()

expectedResources := []*domain.Resource{}
Expand Down Expand Up @@ -430,7 +429,7 @@ func (s *ServiceTestSuite) TestFetchResources() {
}

expectedProvider := providers[0]
s.mockProviderRepository.On("Find", mockCtx).Return([]*domain.Provider{expectedProvider}, nil).Once()
s.mockProviderRepository.EXPECT().Find(mockCtx).Return([]*domain.Provider{expectedProvider}, nil).Once()
s.mockProvider.EXPECT().GetResources(mockCtx, expectedProvider.Config).Return(newResources, nil).Once()
s.mockResourceService.EXPECT().Find(mock.Anything, mock.Anything).Return(existingResources, nil).Once()
actualError := s.service.FetchResources(context.Background())
Expand Down Expand Up @@ -544,9 +543,8 @@ func (s *ServiceTestSuite) TestFetchResources() {
expectedProvider := providers[0]
s.mockProviderRepository.EXPECT().Find(mockCtx).Return([]*domain.Provider{expectedProvider}, nil).Once()
s.mockProvider.EXPECT().GetResources(mockCtx, expectedProvider.Config).Return(newResources, nil).Once()
s.mockResourceService.On("BulkUpsert", mock.Anything, mock.AnythingOfType("[]*domain.Resource")).
Run(func(args mock.Arguments) {
resources := args.Get(1).([]*domain.Resource)
s.mockResourceService.EXPECT().BulkUpsert(mock.Anything, mock.AnythingOfType("[]*domain.Resource")).
Run(func(_a0 context.Context, resources []*domain.Resource) {
s.Empty(cmp.Diff(expectedResources, resources, cmpopts.IgnoreFields(domain.Resource{}, "ID", "CreatedAt", "UpdatedAt")))
}).Return(nil).Once()
s.mockResourceService.EXPECT().Find(mock.Anything, mock.Anything).Return(existingResources, nil).Once()
Expand Down Expand Up @@ -648,7 +646,7 @@ func (s *ServiceTestSuite) TestFetchResources() {
}},
},
}
s.mockProviderRepository.On("Find", mockCtx).Return(providersWithResourceFilter, nil).Once()
s.mockProviderRepository.EXPECT().Find(mockCtx).Return(providersWithResourceFilter, nil).Once()
expectedResources := []*domain.Resource{}
for _, p := range providersWithResourceFilter {
resources := []*domain.Resource{
Expand All @@ -667,7 +665,7 @@ func (s *ServiceTestSuite) TestFetchResources() {
s.mockProvider.On("GetResources", mockCtx, p.Config).Return(resources, nil).Once()
expectedResources = append(expectedResources, resources[1])
}
s.mockResourceService.On("BulkUpsert", mock.Anything, expectedResources).Return(nil).Once()
s.mockResourceService.On("BulkUpsert", mock.Anything, expectedResources).Return(nil)
s.mockResourceService.On("Find", mock.Anything, mock.Anything).Return([]*domain.Resource{}, nil).Once()
actualError := s.service.FetchResources(context.Background())

Expand Down

0 comments on commit 886118d

Please sign in to comment.