forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams_test.go
More file actions
194 lines (186 loc) · 4.25 KB
/
params_test.go
File metadata and controls
194 lines (186 loc) · 4.25 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package shared
import (
"github.com/stretchr/testify/assert"
"net/http"
"reflect"
"testing"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/httpmock"
)
func Test_listURLWithQuery(t *testing.T) {
type args struct {
listURL string
options FilterOptions
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "blank",
args: args{
listURL: "https://example.com/path?a=b",
options: FilterOptions{
Entity: "issue",
State: "open",
},
},
want: "https://example.com/path?a=b&q=is%3Aissue+is%3Aopen",
wantErr: false,
},
{
name: "all",
args: args{
listURL: "https://example.com/path",
options: FilterOptions{
Entity: "issue",
State: "open",
Assignee: "bo",
Author: "ka",
BaseBranch: "trunk",
Mention: "nu",
},
},
want: "https://example.com/path?q=is%3Aissue+is%3Aopen+assignee%3Abo+author%3Aka+mentions%3Anu+base%3Atrunk",
wantErr: false,
},
{
name: "spaces in values",
args: args{
listURL: "https://example.com/path",
options: FilterOptions{
Entity: "pr",
State: "open",
Labels: []string{"docs", "help wanted"},
Milestone: `Codename "What Was Missing"`,
},
},
want: "https://example.com/path?q=is%3Apr+is%3Aopen+label%3Adocs+label%3A%22help+wanted%22+milestone%3A%22Codename+%5C%22What+Was+Missing%5C%22%22",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ListURLWithQuery(tt.args.listURL, tt.args.options)
if (err != nil) != tt.wantErr {
t.Errorf("listURLWithQuery() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("listURLWithQuery() = %v, want %v", got, tt.want)
}
})
}
}
func TestMeReplacer_Replace(t *testing.T) {
rtSuccess := &httpmock.Registry{}
rtSuccess.Register(
httpmock.GraphQL(`query UserCurrent\b`),
httpmock.StringResponse(`
{ "data": {
"viewer": { "login": "ResolvedLogin" }
} }
`),
)
rtFailure := &httpmock.Registry{}
rtFailure.Register(
httpmock.GraphQL(`query UserCurrent\b`),
httpmock.StatusStringResponse(500, `
{ "data": {
"viewer": { }
} }
`),
)
type args struct {
logins []string
client *api.Client
repo ghrepo.Interface
}
tests := []struct {
name string
args args
verify func(t httpmock.Testing)
want []string
wantErr bool
}{
{
name: "succeeds resolving the userlogin",
args: args{
client: api.NewClientFromHTTP(&http.Client{Transport: rtSuccess}),
repo: ghrepo.New("OWNER", "REPO"),
logins: []string{"some", "@me", "other"},
},
verify: rtSuccess.Verify,
want: []string{"some", "ResolvedLogin", "other"},
},
{
name: "fails resolving the userlogin",
args: args{
client: api.NewClientFromHTTP(&http.Client{Transport: rtFailure}),
repo: ghrepo.New("OWNER", "REPO"),
logins: []string{"some", "@me", "other"},
},
verify: rtFailure.Verify,
want: []string(nil),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
me := NewMeReplacer(tt.args.client, tt.args.repo.RepoHost())
got, err := me.ReplaceSlice(tt.args.logins)
if (err != nil) != tt.wantErr {
t.Errorf("ReplaceAtMeLogin() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReplaceAtMeLogin() = %v, want %v", got, tt.want)
}
if tt.verify != nil {
tt.verify(t)
}
})
}
}
func Test_QueryHasStateClause(t *testing.T) {
tests := []struct {
searchQuery string
hasState bool
}{
{
searchQuery: "is:closed is:merged",
hasState: true,
},
{
searchQuery: "author:mislav",
hasState: false,
},
{
searchQuery: "assignee:g14a mentions:vilmibm",
hasState: false,
},
{
searchQuery: "merged:>2021-05-20",
hasState: true,
},
{
searchQuery: "state:merged state:open",
hasState: true,
},
{
searchQuery: "assignee:g14a is:closed",
hasState: true,
},
{
searchQuery: "state:closed label:bug",
hasState: true,
},
}
for _, tt := range tests {
gotState := QueryHasStateClause(tt.searchQuery)
assert.Equal(t, tt.hasState, gotState)
}
}