Skip to content

Commit

Permalink
add tests for compare, contributors, merge base functions of reposito…
Browse files Browse the repository at this point in the history
…ries
  • Loading branch information
venky333 committed Sep 23, 2021
1 parent c992a0c commit 22e04c5
Showing 1 changed file with 193 additions and 0 deletions.
193 changes: 193 additions & 0 deletions repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,196 @@ func TestRepositoriesService_StreamArchive(t *testing.T) {
require.Error(t, err)
require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

func TestRepositoriesService_Compare(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/projects/12d65c8dd2b2676fa3ac47d955accc085a37a9c1/repository/compare", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `
{
"commit": {
"id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
"short_id": "12d65c8dd2b",
"title": "JS fix",
"author_name": "Example User",
"author_email": "[email protected]"
},
"commits": [{
"id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
"short_id": "12d65c8dd2b",
"title": "JS fix",
"author_name": "Example User",
"author_email": "[email protected]"
}],
"diffs": [{
"old_path": "files/js/application.js",
"new_path": "files/js/application.js",
"a_mode": null,
"b_mode": "100644",
"diff": "--- a/files/js/application.js\n+++ c/files/js/application.js\n@@ -24,8 +24,10 @@\n //= require g.raphael-min\n //= require g.bar-min\n //= require branch-graph\n-//= require highlightjs.min\n-//= require ace/ace\n //= require_tree .\n //= require d3\n //= require underscore\n+\n+function fix() { \n+ alert(\"Fixed\")\n+}",
"new_file": false,
"renamed_file": false,
"deleted_file": false
}],
"compare_timeout": false,
"compare_same_ref": false,
"web_url": "https://gitlab.example.com/thedude/gitlab-foss/-/compare/ae73cb07c9eeaf35924a10f713b364d32b2dd34f...0b4bc9a49b562e85de7cc9e834518ea6828729b9"
}
`)
})

opt := &CompareOptions{
From: String("master"),
To: String("feature"),
}
want := &Compare{
Commit: &Commit{
ID: "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
ShortID: "12d65c8dd2b",
Title: "JS fix",
AuthorName: "Example User",
AuthorEmail: "[email protected]",
},
Commits: []*Commit{{
ID: "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
ShortID: "12d65c8dd2b",
Title: "JS fix",
AuthorName: "Example User",
AuthorEmail: "[email protected]",
}},
Diffs: []*Diff{{
Diff: "--- a/files/js/application.js\n+++ c/files/js/application.js\n@@ -24,8 +24,10 @@\n //= require g.raphael-min\n //= require g.bar-min\n //= require branch-graph\n-//= require highlightjs.min\n-//= require ace/ace\n //= require_tree .\n //= require d3\n //= require underscore\n+\n+function fix() { \n+ alert(\"Fixed\")\n+}",
NewPath: "files/js/application.js",
OldPath: "files/js/application.js",
AMode: "",
BMode: "100644",
NewFile: false,
RenamedFile: false,
DeletedFile: false,
}},
CompareTimeout: false,
CompareSameRef: false,
}

c, resp, err := client.Repositories.Compare("12d65c8dd2b2676fa3ac47d955accc085a37a9c1", opt, nil)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, c)

c, resp, err = client.Repositories.Compare(1.01, opt, nil)
require.EqualError(t, err, "invalid ID type 1.01, the ID must be an int or a string")
require.Nil(t, resp)
require.Nil(t, c)

c, resp, err = client.Repositories.Compare("12d65c8dd2b2676fa3ac47d955accc085a37a9c1", opt, nil, errorOption)
require.EqualError(t, err, "RequestOptionFunc returns an error")
require.Nil(t, resp)
require.Nil(t, c)

c, resp, err = client.Repositories.Compare("12d65c8dd2b2676fa3ac47d955accc085a37a9c2", opt, nil)
require.Error(t, err)
require.Nil(t, c)
require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

func TestRepositoriesService_Contributors(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/projects/12d65c8dd2b2676fa3ac47d955accc085a37a9c1/repository/contributors", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `
[{
"name": "Example User",
"email": "[email protected]",
"commits": 117,
"additions": 0,
"deletions": 0
}]
`)
})

want := []*Contributor{{
Name: "Example User",
Email: "[email protected]",
Commits: 117,
Additions: 0,
Deletions: 0,
}}

cs, resp, err := client.Repositories.Contributors("12d65c8dd2b2676fa3ac47d955accc085a37a9c1", nil, nil)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, cs)

cs, resp, err = client.Repositories.Contributors(1.01, nil, nil)
require.EqualError(t, err, "invalid ID type 1.01, the ID must be an int or a string")
require.Nil(t, resp)
require.Nil(t, cs)

cs, resp, err = client.Repositories.Contributors("12d65c8dd2b2676fa3ac47d955accc085a37a9c1", nil, nil, errorOption)
require.EqualError(t, err, "RequestOptionFunc returns an error")
require.Nil(t, resp)
require.Nil(t, cs)

cs, resp, err = client.Repositories.Contributors("12d65c8dd2b2676fa3ac47d955accc085a37a9c2", nil, nil)
require.Error(t, err)
require.Nil(t, cs)
require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

func TestRepositoriesService_MergeBase(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/projects/1a0b36b3cdad1d2ee32457c102a8c0b7056fa863/repository/merge_base", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `
{
"id": "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863",
"short_id": "1a0b36b3",
"title": "Initial commit",
"parent_ids": [],
"message": "Initial commit\n",
"author_name": "Example User",
"author_email": "[email protected]",
"committer_name": "Example User",
"committer_email": "[email protected]"
}
`)
})

want := &Commit{
ID: "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863",
ShortID: "1a0b36b3",
Title: "Initial commit",
AuthorName: "Example User",
AuthorEmail: "[email protected]",
CommitterName: "Example User",
CommitterEmail: "[email protected]",
Message: "Initial commit\n",
ParentIDs: []string{},
}

c, resp, err := client.Repositories.MergeBase("1a0b36b3cdad1d2ee32457c102a8c0b7056fa863", nil, nil)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, c)

c, resp, err = client.Repositories.MergeBase(1.01, nil, nil)
require.EqualError(t, err, "invalid ID type 1.01, the ID must be an int or a string")
require.Nil(t, resp)
require.Nil(t, c)

c, resp, err = client.Repositories.MergeBase("1a0b36b3cdad1d2ee32457c102a8c0b7056fa863", nil, nil, errorOption)
require.EqualError(t, err, "RequestOptionFunc returns an error")
require.Nil(t, resp)
require.Nil(t, c)

c, resp, err = client.Repositories.MergeBase("1a0b36b3cdad1d2ee32457c102a8c0b7056fa865", nil, nil)
require.Error(t, err)
require.Nil(t, c)
require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

0 comments on commit 22e04c5

Please sign in to comment.