github.com/google/go-github/v42@v42.0.0/github/issue_import_test.go (about) 1 // Copyright 2020 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "encoding/json" 11 "fmt" 12 "net/http" 13 "testing" 14 "time" 15 16 "github.com/google/go-cmp/cmp" 17 ) 18 19 func TestIssueImportService_Create(t *testing.T) { 20 client, mux, _, teardown := setup() 21 defer teardown() 22 23 createdAt := time.Date(2020, time.August, 11, 15, 30, 0, 0, time.UTC) 24 input := &IssueImportRequest{ 25 IssueImport: IssueImport{ 26 Assignee: String("developer"), 27 Body: "Dummy description", 28 CreatedAt: &createdAt, 29 Labels: []string{"l1", "l2"}, 30 Milestone: Int(1), 31 Title: "Dummy Issue", 32 }, 33 Comments: []*Comment{{ 34 CreatedAt: &createdAt, 35 Body: "Comment body", 36 }}, 37 } 38 39 mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { 40 v := new(IssueImportRequest) 41 json.NewDecoder(r.Body).Decode(v) 42 testMethod(t, r, "POST") 43 testHeader(t, r, "Accept", mediaTypeIssueImportAPI) 44 if !cmp.Equal(v, input) { 45 t.Errorf("Request body = %+v, want %+v", v, input) 46 } 47 48 w.WriteHeader(http.StatusAccepted) 49 w.Write(issueImportResponseJSON) 50 }) 51 52 ctx := context.Background() 53 got, _, err := client.IssueImport.Create(ctx, "o", "r", input) 54 if err != nil { 55 t.Errorf("Create returned error: %v", err) 56 } 57 58 want := wantIssueImportResponse 59 if !cmp.Equal(got, want) { 60 t.Errorf("Create = %+v, want %+v", got, want) 61 } 62 63 const methodName = "Create" 64 testBadOptions(t, methodName, func() (err error) { 65 _, _, err = client.IssueImport.Create(ctx, "\n", "\n", input) 66 return err 67 }) 68 69 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 70 got, resp, err := client.IssueImport.Create(ctx, "o", "r", input) 71 if got != nil { 72 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 73 } 74 return resp, err 75 }) 76 } 77 78 func TestIssueImportService_Create_invalidOwner(t *testing.T) { 79 client, _, _, teardown := setup() 80 defer teardown() 81 82 ctx := context.Background() 83 _, _, err := client.IssueImport.Create(ctx, "%", "r", nil) 84 testURLParseError(t, err) 85 } 86 87 func TestIssueImportService_CheckStatus(t *testing.T) { 88 client, mux, _, teardown := setup() 89 defer teardown() 90 91 mux.HandleFunc("/repos/o/r/import/issues/3", func(w http.ResponseWriter, r *http.Request) { 92 testMethod(t, r, "GET") 93 testHeader(t, r, "Accept", mediaTypeIssueImportAPI) 94 w.WriteHeader(http.StatusOK) 95 w.Write(issueImportResponseJSON) 96 }) 97 98 ctx := context.Background() 99 got, _, err := client.IssueImport.CheckStatus(ctx, "o", "r", 3) 100 if err != nil { 101 t.Errorf("CheckStatus returned error: %v", err) 102 } 103 104 want := wantIssueImportResponse 105 if !cmp.Equal(got, want) { 106 t.Errorf("CheckStatus = %+v, want %+v", got, want) 107 } 108 109 const methodName = "CheckStatus" 110 testBadOptions(t, methodName, func() (err error) { 111 _, _, err = client.IssueImport.CheckStatus(ctx, "\n", "\n", -3) 112 return err 113 }) 114 115 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 116 got, resp, err := client.IssueImport.CheckStatus(ctx, "o", "r", 3) 117 if got != nil { 118 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 119 } 120 return resp, err 121 }) 122 } 123 124 func TestIssueImportService_CheckStatus_invalidOwner(t *testing.T) { 125 client, _, _, teardown := setup() 126 defer teardown() 127 128 ctx := context.Background() 129 _, _, err := client.IssueImport.CheckStatus(ctx, "%", "r", 1) 130 testURLParseError(t, err) 131 } 132 133 func TestIssueImportService_CheckStatusSince(t *testing.T) { 134 client, mux, _, teardown := setup() 135 defer teardown() 136 137 mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { 138 testMethod(t, r, "GET") 139 testHeader(t, r, "Accept", mediaTypeIssueImportAPI) 140 w.WriteHeader(http.StatusOK) 141 w.Write([]byte(fmt.Sprintf("[%s]", issueImportResponseJSON))) 142 }) 143 144 ctx := context.Background() 145 got, _, err := client.IssueImport.CheckStatusSince(ctx, "o", "r", time.Now()) 146 if err != nil { 147 t.Errorf("CheckStatusSince returned error: %v", err) 148 } 149 150 want := []*IssueImportResponse{wantIssueImportResponse} 151 if !cmp.Equal(want, got) { 152 t.Errorf("CheckStatusSince = %v, want = %v", got, want) 153 } 154 155 const methodName = "CheckStatusSince" 156 testBadOptions(t, methodName, func() (err error) { 157 _, _, err = client.IssueImport.CheckStatusSince(ctx, "\n", "\n", time.Now()) 158 return err 159 }) 160 161 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 162 got, resp, err := client.IssueImport.CheckStatusSince(ctx, "o", "r", time.Now()) 163 if got != nil { 164 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 165 } 166 return resp, err 167 }) 168 } 169 170 func TestIssueImportService_CheckStatusSince_invalidOwner(t *testing.T) { 171 client, _, _, teardown := setup() 172 defer teardown() 173 174 ctx := context.Background() 175 _, _, err := client.IssueImport.CheckStatusSince(ctx, "%", "r", time.Now()) 176 testURLParseError(t, err) 177 } 178 179 var issueImportResponseJSON = []byte(`{ 180 "id": 3, 181 "status": "pending", 182 "url": "https://api.github.com/repos/o/r/import/issues/3", 183 "import_issues_url": "https://api.github.com/repos/o/r/import/issues", 184 "repository_url": "https://api.github.com/repos/o/r" 185 }`) 186 187 var wantIssueImportResponse = &IssueImportResponse{ 188 ID: Int(3), 189 Status: String("pending"), 190 URL: String("https://api.github.com/repos/o/r/import/issues/3"), 191 ImportIssuesURL: String("https://api.github.com/repos/o/r/import/issues"), 192 RepositoryURL: String("https://api.github.com/repos/o/r"), 193 } 194 195 func TestIssueImportError_Marshal(t *testing.T) { 196 testJSONMarshal(t, &IssueImportError{}, "{}") 197 198 u := &IssueImportError{ 199 Location: String("loc"), 200 Resource: String("res"), 201 Field: String("field"), 202 Value: String("value"), 203 Code: String("code"), 204 } 205 206 want := `{ 207 "location": "loc", 208 "resource": "res", 209 "field": "field", 210 "value": "value", 211 "code": "code" 212 }` 213 214 testJSONMarshal(t, u, want) 215 } 216 217 func TestIssueImportResponse_Marshal(t *testing.T) { 218 testJSONMarshal(t, &IssueImportResponse{}, "{}") 219 220 u := &IssueImportResponse{ 221 ID: Int(1), 222 Status: String("status"), 223 URL: String("url"), 224 ImportIssuesURL: String("iiu"), 225 RepositoryURL: String("ru"), 226 CreatedAt: &referenceTime, 227 UpdatedAt: &referenceTime, 228 Message: String("msg"), 229 DocumentationURL: String("durl"), 230 Errors: []*IssueImportError{ 231 { 232 Location: String("loc"), 233 Resource: String("res"), 234 Field: String("field"), 235 Value: String("value"), 236 Code: String("code"), 237 }, 238 }, 239 } 240 241 want := `{ 242 "id": 1, 243 "status": "status", 244 "url": "url", 245 "import_issues_url": "iiu", 246 "repository_url": "ru", 247 "created_at": ` + referenceTimeStr + `, 248 "updated_at": ` + referenceTimeStr + `, 249 "message": "msg", 250 "documentation_url": "durl", 251 "errors": [ 252 { 253 "location": "loc", 254 "resource": "res", 255 "field": "field", 256 "value": "value", 257 "code": "code" 258 } 259 ] 260 }` 261 262 testJSONMarshal(t, u, want) 263 } 264 265 func TestComment_Marshal(t *testing.T) { 266 testJSONMarshal(t, &Comment{}, "{}") 267 268 u := &Comment{ 269 CreatedAt: &referenceTime, 270 Body: "body", 271 } 272 273 want := `{ 274 "created_at": ` + referenceTimeStr + `, 275 "body": "body" 276 }` 277 278 testJSONMarshal(t, u, want) 279 } 280 281 func TestIssueImport_Marshal(t *testing.T) { 282 testJSONMarshal(t, &IssueImport{}, "{}") 283 284 u := &IssueImport{ 285 Title: "title", 286 Body: "body", 287 CreatedAt: &referenceTime, 288 ClosedAt: &referenceTime, 289 UpdatedAt: &referenceTime, 290 Assignee: String("a"), 291 Milestone: Int(1), 292 Closed: Bool(false), 293 Labels: []string{"l"}, 294 } 295 296 want := `{ 297 "title": "title", 298 "body": "body", 299 "created_at": ` + referenceTimeStr + `, 300 "closed_at": ` + referenceTimeStr + `, 301 "updated_at": ` + referenceTimeStr + `, 302 "assignee": "a", 303 "milestone": 1, 304 "closed": false, 305 "labels": [ 306 "l" 307 ] 308 }` 309 310 testJSONMarshal(t, u, want) 311 } 312 313 func TestIssueImportRequest_Marshal(t *testing.T) { 314 testJSONMarshal(t, &IssueImportRequest{}, "{}") 315 316 u := &IssueImportRequest{ 317 IssueImport: IssueImport{ 318 Title: "title", 319 Body: "body", 320 CreatedAt: &referenceTime, 321 ClosedAt: &referenceTime, 322 UpdatedAt: &referenceTime, 323 Assignee: String("a"), 324 Milestone: Int(1), 325 Closed: Bool(false), 326 Labels: []string{"l"}, 327 }, 328 Comments: []*Comment{ 329 { 330 CreatedAt: &referenceTime, 331 Body: "body", 332 }, 333 }, 334 } 335 336 want := `{ 337 "issue": { 338 "title": "title", 339 "body": "body", 340 "created_at": ` + referenceTimeStr + `, 341 "closed_at": ` + referenceTimeStr + `, 342 "updated_at": ` + referenceTimeStr + `, 343 "assignee": "a", 344 "milestone": 1, 345 "closed": false, 346 "labels": [ 347 "l" 348 ] 349 }, 350 "comments": [ 351 { 352 "created_at": ` + referenceTimeStr + `, 353 "body": "body" 354 } 355 ] 356 }` 357 358 testJSONMarshal(t, u, want) 359 }