Repository: google/go-github
File: github/repos_deployment_branch_policies_test.go (Go)

Code before:

	opts := &ListOptions{Page: 1, PerPage: 30}
	ctx := t.Context()
	got, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e", opts)
	if err != nil {
		t.Errorf("Repositories.ListDeploymentBranchPolicies returned error: %v", err)
	}

	want := &DeploymentBranchPolicyResponse{
		BranchPolicies: []*DeploymentBranchPolicy{
			{ID: Ptr(int64(1))},
			{ID: Ptr(int64(2))},
		},
		TotalCount: Ptr(2),
	}
	if !cmp.Equal(got, want) {
		t.Errorf("Repositories.ListDeploymentBranchPolicies = %+v, want %+v", got, want)
	}

	const methodName = "ListDeploymentBranchPolicies"
	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		got, resp, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e", opts)
		if got != nil {
			t.Errorf("got non-nil Repositories.ListDeploymentBranchPolicies response: %+v", got)
		}
		return resp, err
	})
}

func TestRepositoriesService_GetDeploymentBranchPolicy(t *testing.T) {
	t.Parallel()
	client, mux, _ := setup(t)

	mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprint(w, `{"id":1}`)
	})

	ctx := t.Context()
	got, _, err := client.Repositories.GetDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
	if err != nil {
		t.Errorf("Repositories.GetDeploymentBranchPolicy returned error: %v", err)
	}

	want := &DeploymentBranchPolicy{ID: Ptr(int64(1))}
	if !cmp.Equal(got, want) {
		t.Errorf("Repositories.GetDeploymentBranchPolicy = %+v, want %+v", got, want)
	}

	const methodName = "GetDeploymentBranchPolicy"
	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		got, resp, err := client.Repositories.GetDeploymentBranchPolicy(ctx, "o", "r", "e", 1)

Review comment (bug):
When you add `ListOptions` to a method that has string params, you can also typically add `testBadOptions` here before `testNewRequestAndDoFailure` and pass in a newline (`\n`) for each string parameter to cause it to fail.

Code after:

	opts := &ListOptions{Page: 1, PerPage: 30}
	ctx := t.Context()
	got, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e", opts)
	if err != nil {
		t.Errorf("Repositories.ListDeploymentBranchPolicies returned error: %v", err)
	}

	want := &DeploymentBranchPolicyResponse{
		BranchPolicies: []*DeploymentBranchPolicy{
			{ID: Ptr(int64(1))},
			{ID: Ptr(int64(2))},
		},
		TotalCount: Ptr(2),
	}
	if !cmp.Equal(got, want) {
		t.Errorf("Repositories.ListDeploymentBranchPolicies = %+v, want %+v", got, want)
	}

	const methodName = "ListDeploymentBranchPolicies"
	testBadOptions(t, methodName, func() (err error) {
		_, _, err = client.Repositories.ListDeploymentBranchPolicies(ctx, "\n", "\n", "\n", opts)
		return err
	})

	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		got, resp, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e", opts)
		if got != nil {
			t.Errorf("got non-nil Repositories.ListDeploymentBranchPolicies response: %+v", got)
		}
		return resp, err
	})
}

func TestRepositoriesService_GetDeploymentBranchPolicy(t *testing.T) {
	t.Parallel()
	client, mux, _ := setup(t)

	mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprint(w, `{"id":1}`)
	})

	ctx := t.Context()
	got, _, err := client.Repositories.GetDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
	if err != nil {
		t.Errorf("Repositories.GetDeploymentBranchPolicy returned error: %v", err)
	}

	want := &DeploymentBranchPolicy{ID: Ptr(int64(1))}
	if !cmp.Equal(got, want) {
		t.Errorf("Repositories.GetDeploymentBranchPolicy = %+v, want %+v", got, want)