Skip to content

Commit

Permalink
add mock helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rverdile committed Oct 30, 2023
1 parent 5175e54 commit 08bc4e6
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 72 deletions.
13 changes: 13 additions & 0 deletions pkg/dao/mock_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dao

import "github.com/stretchr/testify/mock"

func (m *MockRepositoryConfigDao) WithContextMock() *MockRepositoryConfigDao {
m.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(m)
return m
}

func (m *MockSnapshotDao) WithContextMock() *MockSnapshotDao {
m.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(m)
return m
}
6 changes: 3 additions & 3 deletions pkg/dao/repository_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ func (suite *RepositoryConfigSuite) TestRefreshRedHatRepo() {
}

func (suite *RepositoryConfigSuite) mockPulpForListOrFetch(times int) {
suite.mockPulpClient.On("GetContentPath").Return(testContentPath, nil).Times(times)
suite.mockPulpClient.On("WithContext", context.Background()).Return(suite.mockPulpClient).Times(times)
suite.mockPulpClient.On("WithDomain", "").Return(suite.mockPulpClient).Times(times)
if config.Get().Features.Snapshots.Enabled {
suite.mockPulpClient.WithContextMock().WithDomainMock().On("GetContentPath").Return(testContentPath, nil).Times(times)
}
}
22 changes: 9 additions & 13 deletions pkg/dao/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/config"
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/pulp_client"
Expand Down Expand Up @@ -89,9 +90,7 @@ func (s *SnapshotsSuite) TestCreateAndList() {
sDao := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
sDao.WithContext(context.Background())

mockPulpClient.On("WithContext", context.Background()).Return(mockPulpClient).Twice()
mockPulpClient.On("GetContentPath").Return(testContentPath, nil).Twice()
mockPulpClient.On("WithDomain", "").Return(mockPulpClient)
mockPulpClient.WithContextMock().On("GetContentPath").Return(testContentPath, nil)

repoDao := repositoryConfigDaoImpl{db: tx, yumRepo: &mockExt.YumRepositoryMock{}, pulpClient: mockPulpClient}
repoDao.WithContext(context.Background())
Expand Down Expand Up @@ -181,8 +180,7 @@ func (s *SnapshotsSuite) TestListPageLimit() {
sDao := snapshotDaoImpl{db: tx, pulpClient: mockPulpClient}
sDao.WithContext(context.Background())

mockPulpClient.On("WithContext", context.Background()).Return(mockPulpClient)
mockPulpClient.On("GetContentPath").Return(testContentPath, nil).Once()
mockPulpClient.WithContextMock().On("GetContentPath").Return(testContentPath, nil)

rConfig := s.createRepository()
pageData := api.PaginationData{
Expand Down Expand Up @@ -286,16 +284,14 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFile() {
snapshot := s.createSnapshot(repoConfig)

// Test happy scenario
mockPulpClient.On("WithContext", context.Background()).Return(mockPulpClient).Once()
mockPulpClient.On("GetContentPath").Return(testContentPath, nil).Once()
mockPulpClient.WithContextMock().On("GetContentPath").Return(testContentPath, nil).Once()
repoConfigFile, err := sDao.GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, repoConfig.UUID)
assert.NoError(t, err)
assert.Contains(t, repoConfigFile, repoConfig.Name)
assert.Contains(t, repoConfigFile, testContentPath)

// Test error from pulp call
mockPulpClient.On("WithContext", context.Background()).Return(mockPulpClient).Once()
mockPulpClient.On("GetContentPath").Return("", fmt.Errorf("some error")).Once()
mockPulpClient.WithContextMock().On("GetContentPath").Return("", fmt.Errorf("some error")).Once()
repoConfigFile, err = sDao.GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, repoConfig.UUID)
assert.Error(t, err)
assert.Empty(t, repoConfigFile)
Expand All @@ -312,8 +308,11 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFileNotFound() {
repoConfig := s.createRepository()
snapshot := s.createSnapshot(repoConfig)

if config.Get().Features.Snapshots.Enabled {
mockPulpClient.WithContextMock().On("GetContentPath").Return(testContentPath, nil).Times(3)
}

// Test bad repo UUID
mockPulpClient.On("GetContentPath").Return(testContentPath, nil).Once()
repoConfigFile, err := sDao.GetRepositoryConfigurationFile(repoConfig.OrgID, snapshot.UUID, uuid2.NewString())
assert.Error(t, err)
if err != nil {
Expand All @@ -325,7 +324,6 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFileNotFound() {
assert.Empty(t, repoConfigFile)

// Test bad snapshot UUID
mockPulpClient.On("GetContentPath").Return(testContentPath, nil).Once()
repoConfigFile, err = sDao.GetRepositoryConfigurationFile(repoConfig.OrgID, uuid2.NewString(), repoConfig.UUID)
assert.Error(t, err)
if err != nil {
Expand All @@ -337,8 +335,6 @@ func (s *SnapshotsSuite) TestGetRepositoryConfigurationFileNotFound() {
assert.Empty(t, repoConfigFile)

// Test bad org ID
mockPulpClient.On("WithContext", context.Background())
mockPulpClient.On("GetContentPath").Return(testContentPath, nil).Once()
repoConfigFile, err = sDao.GetRepositoryConfigurationFile("bad orgID", snapshot.UUID, repoConfig.UUID)
assert.Error(t, err)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/dao/task_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,9 @@ func (suite *TaskInfoSuite) TestTaskCleanup() {

mockPulpClient := pulp_client.NewMockPulpClient(suite.T())
repoConfigDao := GetRepositoryConfigDao(suite.tx, mockPulpClient).WithContext(context.Background())
mockPulpClient.On("WithContext", context.Background()).Return(mockPulpClient)
mockPulpClient.On("WithDomain", "").Return(mockPulpClient)
mockPulpClient.On("GetContentPath").Return(testContentPath, nil)
if config.Get().Features.Snapshots.Enabled {
mockPulpClient.WithContextMock().WithDomainMock().On("GetContentPath").Return(testContentPath, nil)
}
results, _, _ := repoConfigDao.List(orgIDTest, api.PaginationData{Limit: 2}, api.FilterData{})
if len(results.Data) != 2 {
assert.Fail(suite.T(), "Expected to create 2 repo configs")
Expand Down
13 changes: 4 additions & 9 deletions pkg/handler/popular_repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/labstack/echo/v4"
"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -80,10 +79,9 @@ func (s *PopularReposSuite) servePopularRepositoriesRouter(req *http.Request) (i
func (s *PopularReposSuite) TestPopularRepos() {
collection := createRepoCollection(0, 10, 0)
paginationData := api.PaginationData{Limit: 1}
s.dao.RepositoryConfig.On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: "https://dl.fedoraproject.org/pub/epel/9/Everything/x86_64/"}).Return(collection, int64(0), nil)
s.dao.RepositoryConfig.WithContextMock().On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: "https://dl.fedoraproject.org/pub/epel/9/Everything/x86_64/"}).Return(collection, int64(0), nil)
s.dao.RepositoryConfig.On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: "https://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/"}).Return(collection, int64(0), nil)
s.dao.RepositoryConfig.On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: "https://dl.fedoraproject.org/pub/epel/7/x86_64/"}).Return(collection, int64(0), nil)
s.dao.RepositoryConfig.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(&s.dao.RepositoryConfig).Times(3)

path := fmt.Sprintf("%s/popular_repositories/?limit=%d", fullRootPath(), 10)
req := httptest.NewRequest(http.MethodGet, path, nil)
Expand All @@ -110,8 +108,7 @@ func (s *PopularReposSuite) TestPopularReposSearchWithExisting() {
existingName := "bestNameEver"
collection := api.RepositoryCollectionResponse{Data: []api.RepositoryResponse{{UUID: magicalUUID, Name: existingName, URL: popularRepository.URL, DistributionVersions: popularRepository.DistributionVersions, DistributionArch: popularRepository.DistributionArch}}}
paginationData := api.PaginationData{Limit: 1}
s.dao.RepositoryConfig.On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: popularRepository.URL}).Return(collection, int64(0), nil)
s.dao.RepositoryConfig.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(&s.dao.RepositoryConfig).Once()
s.dao.RepositoryConfig.WithContextMock().On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: popularRepository.URL}).Return(collection, int64(0), nil)

path := fmt.Sprintf("%s/popular_repositories/?limit=%d&search=%s", fullRootPath(), 10, popularRepository.URL)
req := httptest.NewRequest(http.MethodGet, path, nil)
Expand All @@ -138,8 +135,7 @@ func (s *PopularReposSuite) TestPopularReposSearchWithExisting() {
func (s *PopularReposSuite) TestPopularReposSearchByURL() {
collection := createRepoCollection(0, 10, 0)
paginationData := api.PaginationData{Limit: 1}
s.dao.RepositoryConfig.On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: popularRepository.URL}).Return(collection, int64(0), nil)
s.dao.RepositoryConfig.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(&s.dao.RepositoryConfig).Once()
s.dao.RepositoryConfig.WithContextMock().On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: popularRepository.URL}).Return(collection, int64(0), nil)
path := fmt.Sprintf("%s/popular_repositories/?limit=%d&search=%s", fullRootPath(), 10, popularRepository.URL)
req := httptest.NewRequest(http.MethodGet, path, nil)
req.Header.Set(api.IdentityHeader, test_handler.EncodedIdentity(s.T()))
Expand All @@ -163,8 +159,7 @@ func (s *PopularReposSuite) TestPopularReposSearchByURL() {
func (s *PopularReposSuite) TestPopularReposSearchByName() {
collection := createRepoCollection(0, 10, 0)
paginationData := api.PaginationData{Limit: 1}
s.dao.RepositoryConfig.On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: popularRepository.URL}).Return(collection, int64(0), nil)
s.dao.RepositoryConfig.On("WithContext", mock.AnythingOfType("*context.valueCtx")).Return(&s.dao.RepositoryConfig).Once()
s.dao.RepositoryConfig.WithContextMock().On("List", test_handler.MockOrgId, paginationData, api.FilterData{Search: popularRepository.URL}).Return(collection, int64(0), nil)

path := fmt.Sprintf("%s/popular_repositories/?limit=%d&search=%s", fullRootPath(), 10, url.QueryEscape(popularRepository.SuggestedName))
req := httptest.NewRequest(http.MethodGet, path, nil)
Expand Down
Loading

0 comments on commit 08bc4e6

Please sign in to comment.