forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries_pr_test.go
138 lines (130 loc) · 2.96 KB
/
queries_pr_test.go
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
package api
import (
"encoding/json"
"testing"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert"
)
func TestBranchDeleteRemote(t *testing.T) {
var tests = []struct {
name string
branch string
httpStubs func(*httpmock.Registry)
expectError bool
}{
{
name: "success",
branch: "owner/branch#123",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/owner%2Fbranch%23123"),
httpmock.StatusStringResponse(204, ""))
},
expectError: false,
},
{
name: "error",
branch: "my-branch",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/my-branch"),
httpmock.StatusStringResponse(500, `{"message": "oh no"}`))
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
http := &httpmock.Registry{}
if tt.httpStubs != nil {
tt.httpStubs(http)
}
client := newTestClient(http)
repo, _ := ghrepo.FromFullName("OWNER/REPO")
err := BranchDeleteRemote(client, repo, tt.branch)
if (err != nil) != tt.expectError {
t.Fatalf("unexpected result: %v", err)
}
})
}
}
func Test_Logins(t *testing.T) {
rr := ReviewRequests{}
var tests = []struct {
name string
requestedReviews string
want []string
}{
{
name: "no requested reviewers",
requestedReviews: `{"nodes": []}`,
want: []string{},
},
{
name: "user",
requestedReviews: `{"nodes": [
{
"requestedreviewer": {
"__typename": "User", "login": "testuser"
}
}
]}`,
want: []string{"testuser"},
},
{
name: "team",
requestedReviews: `{"nodes": [
{
"requestedreviewer": {
"__typename": "Team",
"name": "Test Team",
"slug": "test-team",
"organization": {"login": "myorg"}
}
}
]}`,
want: []string{"myorg/test-team"},
},
{
name: "multiple users and teams",
requestedReviews: `{"nodes": [
{
"requestedreviewer": {
"__typename": "User", "login": "user1"
}
},
{
"requestedreviewer": {
"__typename": "User", "login": "user2"
}
},
{
"requestedreviewer": {
"__typename": "Team",
"name": "Test Team",
"slug": "test-team",
"organization": {"login": "myorg"}
}
},
{
"requestedreviewer": {
"__typename": "Team",
"name": "Dev Team",
"slug": "dev-team",
"organization": {"login": "myorg"}
}
}
]}`,
want: []string{"user1", "user2", "myorg/test-team", "myorg/dev-team"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := json.Unmarshal([]byte(tt.requestedReviews), &rr)
assert.NoError(t, err, "Failed to unmarshal json string as ReviewRequests")
logins := rr.Logins()
assert.Equal(t, tt.want, logins)
})
}
}