github.com/google/go-github/v33@v33.0.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  	"reflect"
    14  	"testing"
    15  )
    16  
    17  func TestMigrationService_StartImport(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	input := &Import{
    22  		VCS:         String("git"),
    23  		VCSURL:      String("url"),
    24  		VCSUsername: String("u"),
    25  		VCSPassword: String("p"),
    26  	}
    27  
    28  	mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) {
    29  		v := new(Import)
    30  		json.NewDecoder(r.Body).Decode(v)
    31  
    32  		testMethod(t, r, "PUT")
    33  		if !reflect.DeepEqual(v, input) {
    34  			t.Errorf("Request body = %+v, want %+v", v, input)
    35  		}
    36  
    37  		w.WriteHeader(http.StatusCreated)
    38  		fmt.Fprint(w, `{"status":"importing"}`)
    39  	})
    40  
    41  	got, _, err := client.Migrations.StartImport(context.Background(), "o", "r", input)
    42  	if err != nil {
    43  		t.Errorf("StartImport returned error: %v", err)
    44  	}
    45  	want := &Import{Status: String("importing")}
    46  	if !reflect.DeepEqual(got, want) {
    47  		t.Errorf("StartImport = %+v, want %+v", got, want)
    48  	}
    49  }
    50  
    51  func TestMigrationService_ImportProgress(t *testing.T) {
    52  	client, mux, _, teardown := setup()
    53  	defer teardown()
    54  
    55  	mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) {
    56  		testMethod(t, r, "GET")
    57  		fmt.Fprint(w, `{"status":"complete"}`)
    58  	})
    59  
    60  	got, _, err := client.Migrations.ImportProgress(context.Background(), "o", "r")
    61  	if err != nil {
    62  		t.Errorf("ImportProgress returned error: %v", err)
    63  	}
    64  	want := &Import{Status: String("complete")}
    65  	if !reflect.DeepEqual(got, want) {
    66  		t.Errorf("ImportProgress = %+v, want %+v", got, want)
    67  	}
    68  }
    69  
    70  func TestMigrationService_UpdateImport(t *testing.T) {
    71  	client, mux, _, teardown := setup()
    72  	defer teardown()
    73  
    74  	input := &Import{
    75  		VCS:         String("git"),
    76  		VCSURL:      String("url"),
    77  		VCSUsername: String("u"),
    78  		VCSPassword: String("p"),
    79  	}
    80  
    81  	mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) {
    82  		v := new(Import)
    83  		json.NewDecoder(r.Body).Decode(v)
    84  
    85  		testMethod(t, r, "PATCH")
    86  		if !reflect.DeepEqual(v, input) {
    87  			t.Errorf("Request body = %+v, want %+v", v, input)
    88  		}
    89  
    90  		w.WriteHeader(http.StatusCreated)
    91  		fmt.Fprint(w, `{"status":"importing"}`)
    92  	})
    93  
    94  	got, _, err := client.Migrations.UpdateImport(context.Background(), "o", "r", input)
    95  	if err != nil {
    96  		t.Errorf("UpdateImport returned error: %v", err)
    97  	}
    98  	want := &Import{Status: String("importing")}
    99  	if !reflect.DeepEqual(got, want) {
   100  		t.Errorf("UpdateImport = %+v, want %+v", got, want)
   101  	}
   102  }
   103  
   104  func TestMigrationService_CommitAuthors(t *testing.T) {
   105  	client, mux, _, teardown := setup()
   106  	defer teardown()
   107  
   108  	mux.HandleFunc("/repos/o/r/import/authors", func(w http.ResponseWriter, r *http.Request) {
   109  		testMethod(t, r, "GET")
   110  		fmt.Fprint(w, `[{"id":1,"name":"a"},{"id":2,"name":"b"}]`)
   111  	})
   112  
   113  	got, _, err := client.Migrations.CommitAuthors(context.Background(), "o", "r")
   114  	if err != nil {
   115  		t.Errorf("CommitAuthors returned error: %v", err)
   116  	}
   117  	want := []*SourceImportAuthor{
   118  		{ID: Int64(1), Name: String("a")},
   119  		{ID: Int64(2), Name: String("b")},
   120  	}
   121  	if !reflect.DeepEqual(got, want) {
   122  		t.Errorf("CommitAuthors = %+v, want %+v", got, want)
   123  	}
   124  }
   125  
   126  func TestMigrationService_MapCommitAuthor(t *testing.T) {
   127  	client, mux, _, teardown := setup()
   128  	defer teardown()
   129  
   130  	input := &SourceImportAuthor{Name: String("n"), Email: String("e")}
   131  
   132  	mux.HandleFunc("/repos/o/r/import/authors/1", func(w http.ResponseWriter, r *http.Request) {
   133  		v := new(SourceImportAuthor)
   134  		json.NewDecoder(r.Body).Decode(v)
   135  
   136  		testMethod(t, r, "PATCH")
   137  		if !reflect.DeepEqual(v, input) {
   138  			t.Errorf("Request body = %+v, want %+v", v, input)
   139  		}
   140  
   141  		fmt.Fprint(w, `{"id": 1}`)
   142  	})
   143  
   144  	got, _, err := client.Migrations.MapCommitAuthor(context.Background(), "o", "r", 1, input)
   145  	if err != nil {
   146  		t.Errorf("MapCommitAuthor returned error: %v", err)
   147  	}
   148  	want := &SourceImportAuthor{ID: Int64(1)}
   149  	if !reflect.DeepEqual(got, want) {
   150  		t.Errorf("MapCommitAuthor = %+v, want %+v", got, want)
   151  	}
   152  }
   153  
   154  func TestMigrationService_SetLFSPreference(t *testing.T) {
   155  	client, mux, _, teardown := setup()
   156  	defer teardown()
   157  
   158  	input := &Import{UseLFS: String("opt_in")}
   159  
   160  	mux.HandleFunc("/repos/o/r/import/lfs", func(w http.ResponseWriter, r *http.Request) {
   161  		v := new(Import)
   162  		json.NewDecoder(r.Body).Decode(v)
   163  
   164  		testMethod(t, r, "PATCH")
   165  		if !reflect.DeepEqual(v, input) {
   166  			t.Errorf("Request body = %+v, want %+v", v, input)
   167  		}
   168  
   169  		w.WriteHeader(http.StatusCreated)
   170  		fmt.Fprint(w, `{"status":"importing"}`)
   171  	})
   172  
   173  	got, _, err := client.Migrations.SetLFSPreference(context.Background(), "o", "r", input)
   174  	if err != nil {
   175  		t.Errorf("SetLFSPreference returned error: %v", err)
   176  	}
   177  	want := &Import{Status: String("importing")}
   178  	if !reflect.DeepEqual(got, want) {
   179  		t.Errorf("SetLFSPreference = %+v, want %+v", got, want)
   180  	}
   181  }
   182  
   183  func TestMigrationService_LargeFiles(t *testing.T) {
   184  	client, mux, _, teardown := setup()
   185  	defer teardown()
   186  
   187  	mux.HandleFunc("/repos/o/r/import/large_files", func(w http.ResponseWriter, r *http.Request) {
   188  		testMethod(t, r, "GET")
   189  		fmt.Fprint(w, `[{"oid":"a"},{"oid":"b"}]`)
   190  	})
   191  
   192  	got, _, err := client.Migrations.LargeFiles(context.Background(), "o", "r")
   193  	if err != nil {
   194  		t.Errorf("LargeFiles returned error: %v", err)
   195  	}
   196  	want := []*LargeFile{
   197  		{OID: String("a")},
   198  		{OID: String("b")},
   199  	}
   200  	if !reflect.DeepEqual(got, want) {
   201  		t.Errorf("LargeFiles = %+v, want %+v", got, want)
   202  	}
   203  }
   204  
   205  func TestMigrationService_CancelImport(t *testing.T) {
   206  	client, mux, _, teardown := setup()
   207  	defer teardown()
   208  
   209  	mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) {
   210  		testMethod(t, r, "DELETE")
   211  		w.WriteHeader(http.StatusNoContent)
   212  	})
   213  
   214  	_, err := client.Migrations.CancelImport(context.Background(), "o", "r")
   215  	if err != nil {
   216  		t.Errorf("CancelImport returned error: %v", err)
   217  	}
   218  }