Example #1
0
func TestPullRequestsService_CreateComment(t *testing.T) {
	setup()
	defer teardown()

	pullSpec := PullRequestSpec{Repo: RepoSpec{URI: "r.com/foo"}, Number: 22}
	comment := PullRequestComment{
		PullRequestComment: github.PullRequestComment{
			Body:      github.String("this is a comment"),
			Path:      github.String("/"),
			Position:  github.Int(2),
			CommitID:  github.String("54be46135e45be9bd3318b8fd39a456ff1e2895e"),
			User:      &github.User{},
			CreatedAt: timePtr(time.Unix(100, 100).UTC()),
			UpdatedAt: timePtr(time.Unix(200, 200).UTC()),
		},
	}
	wantComment := comment
	wantComment.ID = github.Int(1)

	called := false
	mux.HandleFunc(urlPath(t, router.RepoPullRequestCommentsCreate, pullSpec.RouteVars()), func(w http.ResponseWriter, req *http.Request) {
		called = true
		testMethod(t, req, "POST")

		var unmarshalled PullRequestComment
		err := json.NewDecoder(req.Body).Decode(&unmarshalled)
		if err != nil {
			t.Fatal(err)
		}
		if !reflect.DeepEqual(unmarshalled, comment) {
			t.Errorf("Got unmarshalled comment %+v, want %+v", unmarshalled, comment)
		}

		writeJSON(w, wantComment)
	})

	gotComment, _, err := client.PullRequests.CreateComment(pullSpec, &comment)
	if err != nil {
		t.Fatal(err)
	}

	if !called {
		t.Errorf("!called")
	}

	if !jsonEqual(t, gotComment, wantComment) {
		t.Errorf("Got %+v, want %+v", gotComment, wantComment)
	}
}
Example #2
0
func TestReposService_CreateStatus(t *testing.T) {
	setup()
	defer teardown()

	want := RepoStatus{RepoStatus: github.RepoStatus{State: github.String("s")}}

	var called bool
	mux.HandleFunc(urlPath(t, router.RepoStatusCreate, map[string]string{"RepoSpec": "r.com/x", "Rev": "r"}), func(w http.ResponseWriter, r *http.Request) {
		called = true
		testMethod(t, r, "POST")

		var st RepoStatus
		if err := json.NewDecoder(r.Body).Decode(&st); err != nil {
			t.Error(err)
		}
		if !reflect.DeepEqual(st, want) {
			t.Errorf("got status %+v, want %+v", st, want)
		}

		writeJSON(w, want)
	})

	s, _, err := client.Repos.CreateStatus(RepoRevSpec{RepoSpec: RepoSpec{URI: "r.com/x"}, Rev: "r"}, want)
	if err != nil {
		t.Errorf("Repos.CreateStatus returned error: %v", err)
	}

	if !called {
		t.Fatal("!called")
	}

	if !reflect.DeepEqual(s, &want) {
		t.Errorf("Repos.CreateStatus returned %+v, want %+v", s, &want)
	}
}
Example #3
0
func TestReposService_GetCombinedStatus(t *testing.T) {
	setup()
	defer teardown()

	want := &CombinedStatus{CombinedStatus: github.CombinedStatus{State: github.String("s")}}

	var called bool
	mux.HandleFunc(urlPath(t, router.RepoCombinedStatus, map[string]string{"RepoSpec": "r.com/x", "Rev": "r"}), func(w http.ResponseWriter, r *http.Request) {
		called = true
		testMethod(t, r, "GET")

		writeJSON(w, want)
	})

	cs, _, err := client.Repos.GetCombinedStatus(RepoRevSpec{RepoSpec: RepoSpec{URI: "r.com/x"}, Rev: "r"})
	if err != nil {
		t.Errorf("Repos.GetCombinedStatus returned error: %v", err)
	}

	if !called {
		t.Fatal("!called")
	}

	if !reflect.DeepEqual(cs, want) {
		t.Errorf("Repos.GetCombinedStatus returned %+v, want %+v", cs, want)
	}
}
Example #4
0
func TestPullRequestsService_EditComment_ErrorNoID(t *testing.T) {
	setup()
	defer teardown()

	pullSpec := PullRequestSpec{Repo: RepoSpec{URI: "r.com/foo"}, Number: 22}
	comment := PullRequestComment{
		PullRequestComment: github.PullRequestComment{
			Body:      github.String("this is a comment"),
			Path:      github.String("/"),
			Position:  github.Int(2),
			CommitID:  github.String("54be46135e45be9bd3318b8fd39a456ff1e2895e"),
			User:      &github.User{},
			CreatedAt: timePtr(time.Unix(100, 100).UTC()),
			UpdatedAt: timePtr(time.Unix(200, 200).UTC()),
		},
	}

	_, _, err := client.PullRequests.EditComment(pullSpec, &comment)
	if err.Error() != "comment ID not specified" {
		t.Errorf(`expected error "comment ID not specified", but got none`)
	}
}
Example #5
0
func TestPullRequestsService_Merge(t *testing.T) {
	setup()
	defer teardown()

	pullSpec := PullRequestSpec{Repo: RepoSpec{URI: "r.com/foo"}, Number: 22}
	mergeRequest := &PullRequestMergeRequest{CommitMessage: "message"}
	wantMergeResult := &PullRequestMergeResult{github.PullRequestMergeResult{
		SHA:     github.String("42c066aecc289359dcaaff61e9a85396ed7c376f"),
		Merged:  github.Bool(true),
		Message: github.String("Pull Request successfully merged"),
	}}

	called := false
	mux.HandleFunc(urlPath(t, router.RepoPullRequestMerge, pullSpec.RouteVars()), func(w http.ResponseWriter, req *http.Request) {
		called = true
		testMethod(t, req, "PUT")

		var unmarshalled PullRequestMergeRequest
		err := json.NewDecoder(req.Body).Decode(&unmarshalled)
		if err != nil {
			t.Fatal(err)
		}
		if !reflect.DeepEqual(&unmarshalled, mergeRequest) {
			t.Errorf("Got unmarshalled merge request %+v, want %+v", unmarshalled, mergeRequest)
		}

		writeJSON(w, wantMergeResult)
	})

	mergeResult, _, err := client.PullRequests.Merge(pullSpec, mergeRequest)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(mergeResult, wantMergeResult) {
		t.Errorf("got %+v, want %+v", mergeResult, wantMergeResult)
	}
}