Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for gitMergeRequests #169

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/Khan/genqlient/graphql"
)

// Testing for newGitLabScraper
/*
* Testing for newGitLabScraper
*/
func TestNewGitLabScraper(t *testing.T) {
factory := Factory{}
defaultConfig := factory.CreateDefaultConfig()
Expand All @@ -24,22 +27,128 @@ func TestNewGitLabScraper(t *testing.T) {
assert.NotNil(t, s)
}

/*
* Mocks
*/
type mockClient struct {
BranchNames []string
RootRef string
err bool
errString string
BranchNames []string
MergeRequests getMergeRequestsProjectMergeRequestsMergeRequestConnection
RootRef string
err bool
errString string
maxPages int
curPage int
}

func (m *mockClient) MakeRequest(ctx context.Context, req *graphql.Request, resp *graphql.Response) error {
if m.err {
return errors.New(m.errString)
}

switch op := req.OpName; op {

case "getBranchNames":
if m.err {
return errors.New(m.errString)
}
r := resp.Data.(*getBranchNamesResponse)
r.Project.Repository.BranchNames = m.BranchNames
r.Project.Repository.RootRef = m.RootRef

case "getMergeRequests":
m.curPage++

if m.curPage == m.maxPages {
m.MergeRequests.PageInfo.HasNextPage = false
}

r := resp.Data.(*getMergeRequestsResponse)
r.Project.MergeRequests = m.MergeRequests
}

return nil
}

/*
* Testing for getMergeRequests
*/
func TestGetMergeRequests(t *testing.T) {
testCases := []struct {
desc string
client graphql.Client
expectedErr error
expectedMergeRequestCount int
}{
{
desc: "empty mergeRequestData",
client: &mockClient{},
expectedErr: nil,
expectedMergeRequestCount: 0,
},
{
desc: "produce error in client",
client: &mockClient{err: true, errString: "An error has occurred"},
expectedErr: errors.New("An error has occurred"),
expectedMergeRequestCount: 0,
},
{
desc: "valid mergeRequestData",
client: &mockClient{
maxPages: 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have another test for multiple pages and no pages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Thank!

MergeRequests: getMergeRequestsProjectMergeRequestsMergeRequestConnection{
PageInfo: getMergeRequestsProjectMergeRequestsMergeRequestConnectionPageInfo{
HasNextPage: true,
},
Nodes: []MergeRequestNode{
{
SourceBranch: "main",
},
{
SourceBranch: "dev",
},
{
SourceBranch: "feature",
},
},
},
},
expectedErr: nil,
expectedMergeRequestCount: 3,
},
{
desc: "valid mergeRequestData",
client: &mockClient{
maxPages: 5,
MergeRequests: getMergeRequestsProjectMergeRequestsMergeRequestConnection{
PageInfo: getMergeRequestsProjectMergeRequestsMergeRequestConnectionPageInfo{
HasNextPage: true,
},
Nodes: []MergeRequestNode{
{
SourceBranch: "main",
},
{
SourceBranch: "dev",
},
{
SourceBranch: "feature",
},
},
},
},
expectedErr: nil,
expectedMergeRequestCount: 15, // 5 pages * 3 merge requests
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
factory := Factory{}
defaultConfig := factory.CreateDefaultConfig()
settings := receivertest.NewNopCreateSettings()
gls := newGitLabScraper(context.Background(), settings, defaultConfig.(*Config))
const state MergeRequestState = "merged"

mergeRequestData, err := gls.getMergeRequests(context.Background(), tc.client, "projectPath", state)

assert.Equal(t, tc.expectedMergeRequestCount, len(mergeRequestData))
assert.Equal(t, tc.expectedErr, err)
})
}
}