1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestRepositoriesService_ListDeploymentBranchPolicies(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"total_count":2, "branch_policies":[{"id":1}, {"id": 2}]}`)
})
ctx := context.Background()
got, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, "o", "r", "e")
if err != nil {
t.Errorf("Repositories.ListDeploymentBranchPolicies returned error: %v", err)
}
want := &DeploymentBranchPolicyResponse{
BranchPolicies: []*DeploymentBranchPolicy{
{ID: Int64(1)},
{ID: Int64(2)},
},
TotalCount: Int(2),
}
if !reflect.DeepEqual(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")
if got != nil {
t.Errorf("got non-nil Repositories.ListDeploymentBranchPolicies response: %+v", got)
}
return resp, err
})
}
func TestRepositoriesService_GetDeploymentBranchPolicy(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"id":1}`)
})
ctx := context.Background()
got, _, err := client.Repositories.GetDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
if err != nil {
t.Errorf("Repositories.GetDeploymentBranchPolicy returned error: %v", err)
}
want := &DeploymentBranchPolicy{ID: Int64(1)}
if !reflect.DeepEqual(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)
if got != nil {
t.Errorf("got non-nil Repositories.GetDeploymentBranchPolicy response: %+v", got)
}
return resp, err
})
}
func TestRepositoriesService_CreateDeploymentBranchPolicy(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":1, "type":"branch"}`)
})
ctx := context.Background()
got, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, "o", "r", "e", &DeploymentBranchPolicyRequest{Name: String("n"), Type: String("branch")})
if err != nil {
t.Errorf("Repositories.CreateDeploymentBranchPolicy returned error: %v", err)
}
want := &DeploymentBranchPolicy{ID: Int64(1), Type: String("branch")}
if !reflect.DeepEqual(got, want) {
t.Errorf("Repositories.CreateDeploymentBranchPolicy = %+v, want %+v", got, want)
}
const methodName = "CreateDeploymentBranchPolicy"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, "o", "r", "e", &DeploymentBranchPolicyRequest{Name: String("n")})
if got != nil {
t.Errorf("got non-nil Repositories.CreateDeploymentBranchPolicy response: %+v", got)
}
return resp, err
})
}
func TestRepositoriesService_UpdateDeploymentBranchPolicy(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprint(w, `{"id":1}`)
})
ctx := context.Background()
got, _, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, "o", "r", "e", 1, &DeploymentBranchPolicyRequest{Name: String("n")})
if err != nil {
t.Errorf("Repositories.UpdateDeploymentBranchPolicy returned error: %v", err)
}
want := &DeploymentBranchPolicy{ID: Int64(1)}
if !reflect.DeepEqual(got, want) {
t.Errorf("Repositories.UpdateDeploymentBranchPolicy = %+v, want %+v", got, want)
}
const methodName = "UpdateDeploymentBranchPolicy"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, "o", "r", "e", 1, &DeploymentBranchPolicyRequest{Name: String("n")})
if got != nil {
t.Errorf("got non-nil Repositories.UpdateDeploymentBranchPolicy response: %+v", got)
}
return resp, err
})
}
func TestRepositoriesService_DeleteDeploymentBranchPolicy(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/environments/e/deployment-branch-policies/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
ctx := context.Background()
_, err := client.Repositories.DeleteDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
if err != nil {
t.Errorf("Repositories.DeleteDeploymentBranchPolicy returned error: %v", err)
}
const methodName = "DeleteDeploymentBranchPolicy"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Repositories.DeleteDeploymentBranchPolicy(ctx, "o", "r", "e", 1)
})
}
|