...

Source file src/github.com/google/go-github/v45/github/issue_import_test.go

Documentation: github.com/google/go-github/v45/github

     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_badResponse(t *testing.T) {
   171  	client, mux, _, teardown := setup()
   172  	defer teardown()
   173  
   174  	mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) {
   175  		testMethod(t, r, "GET")
   176  		testHeader(t, r, "Accept", mediaTypeIssueImportAPI)
   177  		w.WriteHeader(http.StatusOK)
   178  		w.Write([]byte("{badly-formed JSON"))
   179  	})
   180  
   181  	ctx := context.Background()
   182  	if _, _, err := client.IssueImport.CheckStatusSince(ctx, "o", "r", time.Now()); err == nil {
   183  		t.Errorf("CheckStatusSince returned no error, want JSON err")
   184  	}
   185  }
   186  
   187  func TestIssueImportService_CheckStatusSince_invalidOwner(t *testing.T) {
   188  	client, _, _, teardown := setup()
   189  	defer teardown()
   190  
   191  	ctx := context.Background()
   192  	_, _, err := client.IssueImport.CheckStatusSince(ctx, "%", "r", time.Now())
   193  	testURLParseError(t, err)
   194  }
   195  
   196  var issueImportResponseJSON = []byte(`{
   197  	"id": 3,
   198  	"status": "pending",
   199  	"url": "https://api.github.com/repos/o/r/import/issues/3",
   200  	"import_issues_url": "https://api.github.com/repos/o/r/import/issues",
   201  	"repository_url": "https://api.github.com/repos/o/r"
   202  }`)
   203  
   204  var wantIssueImportResponse = &IssueImportResponse{
   205  	ID:              Int(3),
   206  	Status:          String("pending"),
   207  	URL:             String("https://api.github.com/repos/o/r/import/issues/3"),
   208  	ImportIssuesURL: String("https://api.github.com/repos/o/r/import/issues"),
   209  	RepositoryURL:   String("https://api.github.com/repos/o/r"),
   210  }
   211  
   212  func TestIssueImportError_Marshal(t *testing.T) {
   213  	testJSONMarshal(t, &IssueImportError{}, "{}")
   214  
   215  	u := &IssueImportError{
   216  		Location: String("loc"),
   217  		Resource: String("res"),
   218  		Field:    String("field"),
   219  		Value:    String("value"),
   220  		Code:     String("code"),
   221  	}
   222  
   223  	want := `{
   224  		"location": "loc",
   225  		"resource": "res",
   226  		"field": "field",
   227  		"value": "value",
   228  		"code": "code"
   229  	}`
   230  
   231  	testJSONMarshal(t, u, want)
   232  }
   233  
   234  func TestIssueImportResponse_Marshal(t *testing.T) {
   235  	testJSONMarshal(t, &IssueImportResponse{}, "{}")
   236  
   237  	u := &IssueImportResponse{
   238  		ID:               Int(1),
   239  		Status:           String("status"),
   240  		URL:              String("url"),
   241  		ImportIssuesURL:  String("iiu"),
   242  		RepositoryURL:    String("ru"),
   243  		CreatedAt:        &referenceTime,
   244  		UpdatedAt:        &referenceTime,
   245  		Message:          String("msg"),
   246  		DocumentationURL: String("durl"),
   247  		Errors: []*IssueImportError{
   248  			{
   249  				Location: String("loc"),
   250  				Resource: String("res"),
   251  				Field:    String("field"),
   252  				Value:    String("value"),
   253  				Code:     String("code"),
   254  			},
   255  		},
   256  	}
   257  
   258  	want := `{
   259  		"id": 1,
   260  		"status": "status",
   261  		"url": "url",
   262  		"import_issues_url": "iiu",
   263  		"repository_url": "ru",
   264  		"created_at": ` + referenceTimeStr + `,
   265  		"updated_at": ` + referenceTimeStr + `,
   266  		"message": "msg",
   267  		"documentation_url": "durl",
   268  		"errors": [
   269  			{
   270  				"location": "loc",
   271  				"resource": "res",
   272  				"field": "field",
   273  				"value": "value",
   274  				"code": "code"
   275  			}
   276  		]
   277  	}`
   278  
   279  	testJSONMarshal(t, u, want)
   280  }
   281  
   282  func TestComment_Marshal(t *testing.T) {
   283  	testJSONMarshal(t, &Comment{}, "{}")
   284  
   285  	u := &Comment{
   286  		CreatedAt: &referenceTime,
   287  		Body:      "body",
   288  	}
   289  
   290  	want := `{
   291  		"created_at": ` + referenceTimeStr + `,
   292  		"body": "body"
   293  	}`
   294  
   295  	testJSONMarshal(t, u, want)
   296  }
   297  
   298  func TestIssueImport_Marshal(t *testing.T) {
   299  	testJSONMarshal(t, &IssueImport{}, "{}")
   300  
   301  	u := &IssueImport{
   302  		Title:     "title",
   303  		Body:      "body",
   304  		CreatedAt: &referenceTime,
   305  		ClosedAt:  &referenceTime,
   306  		UpdatedAt: &referenceTime,
   307  		Assignee:  String("a"),
   308  		Milestone: Int(1),
   309  		Closed:    Bool(false),
   310  		Labels:    []string{"l"},
   311  	}
   312  
   313  	want := `{
   314  		"title": "title",
   315  		"body": "body",
   316  		"created_at": ` + referenceTimeStr + `,
   317  		"closed_at": ` + referenceTimeStr + `,
   318  		"updated_at": ` + referenceTimeStr + `,
   319  		"assignee": "a",
   320  		"milestone": 1,
   321  		"closed": false,
   322  		"labels": [
   323  			"l"
   324  		]
   325  	}`
   326  
   327  	testJSONMarshal(t, u, want)
   328  }
   329  
   330  func TestIssueImportRequest_Marshal(t *testing.T) {
   331  	testJSONMarshal(t, &IssueImportRequest{}, "{}")
   332  
   333  	u := &IssueImportRequest{
   334  		IssueImport: IssueImport{
   335  			Title:     "title",
   336  			Body:      "body",
   337  			CreatedAt: &referenceTime,
   338  			ClosedAt:  &referenceTime,
   339  			UpdatedAt: &referenceTime,
   340  			Assignee:  String("a"),
   341  			Milestone: Int(1),
   342  			Closed:    Bool(false),
   343  			Labels:    []string{"l"},
   344  		},
   345  		Comments: []*Comment{
   346  			{
   347  				CreatedAt: &referenceTime,
   348  				Body:      "body",
   349  			},
   350  		},
   351  	}
   352  
   353  	want := `{
   354  		"issue": {
   355  			"title": "title",
   356  			"body": "body",
   357  			"created_at": ` + referenceTimeStr + `,
   358  			"closed_at": ` + referenceTimeStr + `,
   359  			"updated_at": ` + referenceTimeStr + `,
   360  			"assignee": "a",
   361  			"milestone": 1,
   362  			"closed": false,
   363  			"labels": [
   364  				"l"
   365  			]
   366  		},
   367  		"comments": [
   368  			{
   369  				"created_at": ` + referenceTimeStr + `,
   370  				"body": "body"
   371  			}
   372  		]
   373  	}`
   374  
   375  	testJSONMarshal(t, u, want)
   376  }
   377  

View as plain text