github.com/google/go-github/v69@v69.2.0/github/migrations_source_import_test.go (about) 1 // Copyright 2016 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 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestMigrationService_StartImport(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 input := &Import{ 23 VCS: Ptr("git"), 24 VCSURL: Ptr("url"), 25 VCSUsername: Ptr("u"), 26 VCSPassword: Ptr("p"), 27 } 28 29 mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { 30 v := new(Import) 31 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 32 33 testMethod(t, r, "PUT") 34 if !cmp.Equal(v, input) { 35 t.Errorf("Request body = %+v, want %+v", v, input) 36 } 37 38 w.WriteHeader(http.StatusCreated) 39 fmt.Fprint(w, `{"status":"importing"}`) 40 }) 41 42 ctx := context.Background() 43 got, _, err := client.Migrations.StartImport(ctx, "o", "r", input) 44 if err != nil { 45 t.Errorf("StartImport returned error: %v", err) 46 } 47 want := &Import{Status: Ptr("importing")} 48 if !cmp.Equal(got, want) { 49 t.Errorf("StartImport = %+v, want %+v", got, want) 50 } 51 52 const methodName = "StartImport" 53 testBadOptions(t, methodName, func() (err error) { 54 _, _, err = client.Migrations.StartImport(ctx, "\n", "\n", input) 55 return err 56 }) 57 58 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 59 got, resp, err := client.Migrations.StartImport(ctx, "o", "r", input) 60 if got != nil { 61 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 62 } 63 return resp, err 64 }) 65 } 66 67 func TestMigrationService_ImportProgress(t *testing.T) { 68 t.Parallel() 69 client, mux, _ := setup(t) 70 71 mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { 72 testMethod(t, r, "GET") 73 fmt.Fprint(w, `{"status":"complete"}`) 74 }) 75 76 ctx := context.Background() 77 got, _, err := client.Migrations.ImportProgress(ctx, "o", "r") 78 if err != nil { 79 t.Errorf("ImportProgress returned error: %v", err) 80 } 81 want := &Import{Status: Ptr("complete")} 82 if !cmp.Equal(got, want) { 83 t.Errorf("ImportProgress = %+v, want %+v", got, want) 84 } 85 86 const methodName = "ImportProgress" 87 testBadOptions(t, methodName, func() (err error) { 88 _, _, err = client.Migrations.ImportProgress(ctx, "\n", "\n") 89 return err 90 }) 91 92 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 93 got, resp, err := client.Migrations.ImportProgress(ctx, "o", "r") 94 if got != nil { 95 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 96 } 97 return resp, err 98 }) 99 } 100 101 func TestMigrationService_UpdateImport(t *testing.T) { 102 t.Parallel() 103 client, mux, _ := setup(t) 104 105 input := &Import{ 106 VCS: Ptr("git"), 107 VCSURL: Ptr("url"), 108 VCSUsername: Ptr("u"), 109 VCSPassword: Ptr("p"), 110 } 111 112 mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { 113 v := new(Import) 114 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 115 116 testMethod(t, r, "PATCH") 117 if !cmp.Equal(v, input) { 118 t.Errorf("Request body = %+v, want %+v", v, input) 119 } 120 121 w.WriteHeader(http.StatusCreated) 122 fmt.Fprint(w, `{"status":"importing"}`) 123 }) 124 125 ctx := context.Background() 126 got, _, err := client.Migrations.UpdateImport(ctx, "o", "r", input) 127 if err != nil { 128 t.Errorf("UpdateImport returned error: %v", err) 129 } 130 want := &Import{Status: Ptr("importing")} 131 if !cmp.Equal(got, want) { 132 t.Errorf("UpdateImport = %+v, want %+v", got, want) 133 } 134 135 const methodName = "UpdateImport" 136 testBadOptions(t, methodName, func() (err error) { 137 _, _, err = client.Migrations.UpdateImport(ctx, "\n", "\n", input) 138 return err 139 }) 140 141 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 142 got, resp, err := client.Migrations.UpdateImport(ctx, "o", "r", input) 143 if got != nil { 144 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 145 } 146 return resp, err 147 }) 148 } 149 150 func TestMigrationService_CommitAuthors(t *testing.T) { 151 t.Parallel() 152 client, mux, _ := setup(t) 153 154 mux.HandleFunc("/repos/o/r/import/authors", func(w http.ResponseWriter, r *http.Request) { 155 testMethod(t, r, "GET") 156 fmt.Fprint(w, `[{"id":1,"name":"a"},{"id":2,"name":"b"}]`) 157 }) 158 159 ctx := context.Background() 160 got, _, err := client.Migrations.CommitAuthors(ctx, "o", "r") 161 if err != nil { 162 t.Errorf("CommitAuthors returned error: %v", err) 163 } 164 want := []*SourceImportAuthor{ 165 {ID: Ptr(int64(1)), Name: Ptr("a")}, 166 {ID: Ptr(int64(2)), Name: Ptr("b")}, 167 } 168 if !cmp.Equal(got, want) { 169 t.Errorf("CommitAuthors = %+v, want %+v", got, want) 170 } 171 172 const methodName = "CommitAuthors" 173 testBadOptions(t, methodName, func() (err error) { 174 _, _, err = client.Migrations.CommitAuthors(ctx, "\n", "\n") 175 return err 176 }) 177 178 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 179 got, resp, err := client.Migrations.CommitAuthors(ctx, "o", "r") 180 if got != nil { 181 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 182 } 183 return resp, err 184 }) 185 } 186 187 func TestMigrationService_MapCommitAuthor(t *testing.T) { 188 t.Parallel() 189 client, mux, _ := setup(t) 190 191 input := &SourceImportAuthor{Name: Ptr("n"), Email: Ptr("e")} 192 193 mux.HandleFunc("/repos/o/r/import/authors/1", func(w http.ResponseWriter, r *http.Request) { 194 v := new(SourceImportAuthor) 195 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 196 197 testMethod(t, r, "PATCH") 198 if !cmp.Equal(v, input) { 199 t.Errorf("Request body = %+v, want %+v", v, input) 200 } 201 202 fmt.Fprint(w, `{"id": 1}`) 203 }) 204 205 ctx := context.Background() 206 got, _, err := client.Migrations.MapCommitAuthor(ctx, "o", "r", 1, input) 207 if err != nil { 208 t.Errorf("MapCommitAuthor returned error: %v", err) 209 } 210 want := &SourceImportAuthor{ID: Ptr(int64(1))} 211 if !cmp.Equal(got, want) { 212 t.Errorf("MapCommitAuthor = %+v, want %+v", got, want) 213 } 214 215 const methodName = "MapCommitAuthor" 216 testBadOptions(t, methodName, func() (err error) { 217 _, _, err = client.Migrations.MapCommitAuthor(ctx, "\n", "\n", 1, input) 218 return err 219 }) 220 221 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 222 got, resp, err := client.Migrations.MapCommitAuthor(ctx, "o", "r", 1, input) 223 if got != nil { 224 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 225 } 226 return resp, err 227 }) 228 } 229 230 func TestMigrationService_SetLFSPreference(t *testing.T) { 231 t.Parallel() 232 client, mux, _ := setup(t) 233 234 input := &Import{UseLFS: Ptr("opt_in")} 235 236 mux.HandleFunc("/repos/o/r/import/lfs", func(w http.ResponseWriter, r *http.Request) { 237 v := new(Import) 238 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 239 240 testMethod(t, r, "PATCH") 241 if !cmp.Equal(v, input) { 242 t.Errorf("Request body = %+v, want %+v", v, input) 243 } 244 245 w.WriteHeader(http.StatusCreated) 246 fmt.Fprint(w, `{"status":"importing"}`) 247 }) 248 249 ctx := context.Background() 250 got, _, err := client.Migrations.SetLFSPreference(ctx, "o", "r", input) 251 if err != nil { 252 t.Errorf("SetLFSPreference returned error: %v", err) 253 } 254 want := &Import{Status: Ptr("importing")} 255 if !cmp.Equal(got, want) { 256 t.Errorf("SetLFSPreference = %+v, want %+v", got, want) 257 } 258 259 const methodName = "SetLFSPreference" 260 testBadOptions(t, methodName, func() (err error) { 261 _, _, err = client.Migrations.SetLFSPreference(ctx, "\n", "\n", input) 262 return err 263 }) 264 265 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 266 got, resp, err := client.Migrations.SetLFSPreference(ctx, "o", "r", input) 267 if got != nil { 268 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 269 } 270 return resp, err 271 }) 272 } 273 274 func TestMigrationService_LargeFiles(t *testing.T) { 275 t.Parallel() 276 client, mux, _ := setup(t) 277 278 mux.HandleFunc("/repos/o/r/import/large_files", func(w http.ResponseWriter, r *http.Request) { 279 testMethod(t, r, "GET") 280 fmt.Fprint(w, `[{"oid":"a"},{"oid":"b"}]`) 281 }) 282 283 ctx := context.Background() 284 got, _, err := client.Migrations.LargeFiles(ctx, "o", "r") 285 if err != nil { 286 t.Errorf("LargeFiles returned error: %v", err) 287 } 288 want := []*LargeFile{ 289 {OID: Ptr("a")}, 290 {OID: Ptr("b")}, 291 } 292 if !cmp.Equal(got, want) { 293 t.Errorf("LargeFiles = %+v, want %+v", got, want) 294 } 295 296 const methodName = "LargeFiles" 297 testBadOptions(t, methodName, func() (err error) { 298 _, _, err = client.Migrations.LargeFiles(ctx, "\n", "\n") 299 return err 300 }) 301 302 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 303 got, resp, err := client.Migrations.LargeFiles(ctx, "o", "r") 304 if got != nil { 305 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 306 } 307 return resp, err 308 }) 309 } 310 311 func TestMigrationService_CancelImport(t *testing.T) { 312 t.Parallel() 313 client, mux, _ := setup(t) 314 315 mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { 316 testMethod(t, r, "DELETE") 317 w.WriteHeader(http.StatusNoContent) 318 }) 319 320 ctx := context.Background() 321 _, err := client.Migrations.CancelImport(ctx, "o", "r") 322 if err != nil { 323 t.Errorf("CancelImport returned error: %v", err) 324 } 325 326 const methodName = "CancelImport" 327 testBadOptions(t, methodName, func() (err error) { 328 _, err = client.Migrations.CancelImport(ctx, "\n", "\n") 329 return err 330 }) 331 332 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 333 return client.Migrations.CancelImport(ctx, "o", "r") 334 }) 335 } 336 337 func TestLargeFile_Marshal(t *testing.T) { 338 t.Parallel() 339 testJSONMarshal(t, &LargeFile{}, "{}") 340 341 u := &LargeFile{ 342 RefName: Ptr("rn"), 343 Path: Ptr("p"), 344 OID: Ptr("oid"), 345 Size: Ptr(1), 346 } 347 348 want := `{ 349 "ref_name": "rn", 350 "path": "p", 351 "oid": "oid", 352 "size": 1 353 }` 354 355 testJSONMarshal(t, u, want) 356 } 357 358 func TestSourceImportAuthor_Marshal(t *testing.T) { 359 t.Parallel() 360 testJSONMarshal(t, &SourceImportAuthor{}, "{}") 361 362 u := &SourceImportAuthor{ 363 ID: Ptr(int64(1)), 364 RemoteID: Ptr("rid"), 365 RemoteName: Ptr("rn"), 366 Email: Ptr("e"), 367 Name: Ptr("n"), 368 URL: Ptr("url"), 369 ImportURL: Ptr("iurl"), 370 } 371 372 want := `{ 373 "id": 1, 374 "remote_id": "rid", 375 "remote_name": "rn", 376 "email": "e", 377 "name": "n", 378 "url": "url", 379 "import_url": "iurl" 380 }` 381 382 testJSONMarshal(t, u, want) 383 } 384 385 func TestImport_Marshal(t *testing.T) { 386 t.Parallel() 387 testJSONMarshal(t, &Import{}, "{}") 388 389 u := &Import{ 390 VCSURL: Ptr("vcsurl"), 391 VCS: Ptr("vcs"), 392 VCSUsername: Ptr("vcsusr"), 393 VCSPassword: Ptr("vcspass"), 394 TFVCProject: Ptr("tfvcp"), 395 UseLFS: Ptr("uselfs"), 396 HasLargeFiles: Ptr(false), 397 LargeFilesSize: Ptr(1), 398 LargeFilesCount: Ptr(1), 399 Status: Ptr("status"), 400 CommitCount: Ptr(1), 401 StatusText: Ptr("statustxt"), 402 AuthorsCount: Ptr(1), 403 Percent: Ptr(1), 404 PushPercent: Ptr(1), 405 URL: Ptr("url"), 406 HTMLURL: Ptr("hurl"), 407 AuthorsURL: Ptr("aurl"), 408 RepositoryURL: Ptr("rurl"), 409 Message: Ptr("msg"), 410 FailedStep: Ptr("fs"), 411 HumanName: Ptr("hn"), 412 ProjectChoices: []*Import{{VCSURL: Ptr("vcsurl")}}, 413 } 414 415 want := `{ 416 "vcs_url": "vcsurl", 417 "vcs": "vcs", 418 "vcs_username": "vcsusr", 419 "vcs_password": "vcspass", 420 "tfvc_project": "tfvcp", 421 "use_lfs": "uselfs", 422 "has_large_files": false, 423 "large_files_size": 1, 424 "large_files_count": 1, 425 "status": "status", 426 "commit_count": 1, 427 "status_text": "statustxt", 428 "authors_count": 1, 429 "percent": 1, 430 "push_percent": 1, 431 "url": "url", 432 "html_url": "hurl", 433 "authors_url": "aurl", 434 "repository_url": "rurl", 435 "message": "msg", 436 "failed_step": "fs", 437 "human_name": "hn", 438 "project_choices": [ 439 { 440 "vcs_url": "vcsurl" 441 } 442 ] 443 }` 444 445 testJSONMarshal(t, u, want) 446 }