github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/pr/shared/params_test.go (about) 1 package shared 2 3 import ( 4 "net/http" 5 "reflect" 6 "testing" 7 8 "github.com/cli/cli/api" 9 "github.com/cli/cli/internal/ghrepo" 10 "github.com/cli/cli/pkg/httpmock" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func Test_listURLWithQuery(t *testing.T) { 15 type args struct { 16 listURL string 17 options FilterOptions 18 } 19 tests := []struct { 20 name string 21 args args 22 want string 23 wantErr bool 24 }{ 25 { 26 name: "blank", 27 args: args{ 28 listURL: "https://example.com/path?a=b", 29 options: FilterOptions{ 30 Entity: "issue", 31 State: "open", 32 }, 33 }, 34 want: "https://example.com/path?a=b&q=is%3Aissue+is%3Aopen", 35 wantErr: false, 36 }, 37 { 38 name: "all", 39 args: args{ 40 listURL: "https://example.com/path", 41 options: FilterOptions{ 42 Entity: "issue", 43 State: "open", 44 Assignee: "bo", 45 Author: "ka", 46 BaseBranch: "trunk", 47 Mention: "nu", 48 }, 49 }, 50 want: "https://example.com/path?q=is%3Aissue+is%3Aopen+assignee%3Abo+author%3Aka+mentions%3Anu+base%3Atrunk", 51 wantErr: false, 52 }, 53 { 54 name: "spaces in values", 55 args: args{ 56 listURL: "https://example.com/path", 57 options: FilterOptions{ 58 Entity: "pr", 59 State: "open", 60 Labels: []string{"docs", "help wanted"}, 61 Milestone: `Codename "What Was Missing"`, 62 }, 63 }, 64 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", 65 wantErr: false, 66 }, 67 } 68 for _, tt := range tests { 69 t.Run(tt.name, func(t *testing.T) { 70 got, err := ListURLWithQuery(tt.args.listURL, tt.args.options) 71 if (err != nil) != tt.wantErr { 72 t.Errorf("listURLWithQuery() error = %v, wantErr %v", err, tt.wantErr) 73 return 74 } 75 if got != tt.want { 76 t.Errorf("listURLWithQuery() = %v, want %v", got, tt.want) 77 } 78 }) 79 } 80 } 81 82 func TestMeReplacer_Replace(t *testing.T) { 83 rtSuccess := &httpmock.Registry{} 84 rtSuccess.Register( 85 httpmock.GraphQL(`query UserCurrent\b`), 86 httpmock.StringResponse(` 87 { "data": { 88 "viewer": { "login": "ResolvedLogin" } 89 } } 90 `), 91 ) 92 93 rtFailure := &httpmock.Registry{} 94 rtFailure.Register( 95 httpmock.GraphQL(`query UserCurrent\b`), 96 httpmock.StatusStringResponse(500, ` 97 { "data": { 98 "viewer": { } 99 } } 100 `), 101 ) 102 103 type args struct { 104 logins []string 105 client *api.Client 106 repo ghrepo.Interface 107 } 108 tests := []struct { 109 name string 110 args args 111 verify func(t httpmock.Testing) 112 want []string 113 wantErr bool 114 }{ 115 { 116 name: "succeeds resolving the userlogin", 117 args: args{ 118 client: api.NewClientFromHTTP(&http.Client{Transport: rtSuccess}), 119 repo: ghrepo.New("OWNER", "REPO"), 120 logins: []string{"some", "@me", "other"}, 121 }, 122 verify: rtSuccess.Verify, 123 want: []string{"some", "ResolvedLogin", "other"}, 124 }, 125 { 126 name: "fails resolving the userlogin", 127 args: args{ 128 client: api.NewClientFromHTTP(&http.Client{Transport: rtFailure}), 129 repo: ghrepo.New("OWNER", "REPO"), 130 logins: []string{"some", "@me", "other"}, 131 }, 132 verify: rtFailure.Verify, 133 want: []string(nil), 134 wantErr: true, 135 }, 136 } 137 for _, tt := range tests { 138 t.Run(tt.name, func(t *testing.T) { 139 me := NewMeReplacer(tt.args.client, tt.args.repo.RepoHost()) 140 got, err := me.ReplaceSlice(tt.args.logins) 141 if (err != nil) != tt.wantErr { 142 t.Errorf("ReplaceAtMeLogin() error = %v, wantErr %v", err, tt.wantErr) 143 return 144 } 145 if !reflect.DeepEqual(got, tt.want) { 146 t.Errorf("ReplaceAtMeLogin() = %v, want %v", got, tt.want) 147 } 148 149 if tt.verify != nil { 150 tt.verify(t) 151 } 152 }) 153 } 154 } 155 156 func Test_QueryHasStateClause(t *testing.T) { 157 tests := []struct { 158 searchQuery string 159 hasState bool 160 }{ 161 { 162 searchQuery: "is:closed is:merged", 163 hasState: true, 164 }, 165 { 166 searchQuery: "author:mislav", 167 hasState: false, 168 }, 169 { 170 searchQuery: "assignee:g14a mentions:vilmibm", 171 hasState: false, 172 }, 173 { 174 searchQuery: "merged:>2021-05-20", 175 hasState: true, 176 }, 177 { 178 searchQuery: "state:merged state:open", 179 hasState: true, 180 }, 181 { 182 searchQuery: "assignee:g14a is:closed", 183 hasState: true, 184 }, 185 { 186 searchQuery: "state:closed label:bug", 187 hasState: true, 188 }, 189 } 190 for _, tt := range tests { 191 gotState := QueryHasStateClause(tt.searchQuery) 192 assert.Equal(t, tt.hasState, gotState) 193 } 194 } 195 196 func Test_WithPrAndIssueQueryParams(t *testing.T) { 197 type args struct { 198 baseURL string 199 state IssueMetadataState 200 } 201 tests := []struct { 202 name string 203 args args 204 want string 205 wantErr bool 206 }{ 207 { 208 name: "blank", 209 args: args{ 210 baseURL: "", 211 state: IssueMetadataState{}, 212 }, 213 want: "?body=", 214 }, 215 { 216 name: "no values", 217 args: args{ 218 baseURL: "http://example.com/hey", 219 state: IssueMetadataState{}, 220 }, 221 want: "http://example.com/hey?body=", 222 }, 223 { 224 name: "title and body", 225 args: args{ 226 baseURL: "http://example.com/hey", 227 state: IssueMetadataState{ 228 Title: "my title", 229 Body: "my bodeh", 230 }, 231 }, 232 want: "http://example.com/hey?body=my+bodeh&title=my+title", 233 }, 234 } 235 for _, tt := range tests { 236 t.Run(tt.name, func(t *testing.T) { 237 got, err := WithPrAndIssueQueryParams(nil, nil, tt.args.baseURL, tt.args.state) 238 if (err != nil) != tt.wantErr { 239 t.Errorf("WithPrAndIssueQueryParams() error = %v, wantErr %v", err, tt.wantErr) 240 return 241 } 242 if got != tt.want { 243 t.Errorf("WithPrAndIssueQueryParams() = %v, want %v", got, tt.want) 244 } 245 }) 246 } 247 }