...

Source file src/github.com/google/go-github/v47/github/issues_timeline_test.go

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

     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  	"fmt"
    11  	"net/http"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestIssuesService_ListIssueTimeline(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	wantAcceptHeaders := []string{mediaTypeTimelinePreview, mediaTypeProjectCardDetailsPreview}
    23  	mux.HandleFunc("/repos/o/r/issues/1/timeline", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
    26  		testFormValues(t, r, values{
    27  			"page":     "1",
    28  			"per_page": "2",
    29  		})
    30  		fmt.Fprint(w, `[{"id":1}]`)
    31  	})
    32  
    33  	opt := &ListOptions{Page: 1, PerPage: 2}
    34  	ctx := context.Background()
    35  	events, _, err := client.Issues.ListIssueTimeline(ctx, "o", "r", 1, opt)
    36  	if err != nil {
    37  		t.Errorf("Issues.ListIssueTimeline returned error: %v", err)
    38  	}
    39  
    40  	want := []*Timeline{{ID: Int64(1)}}
    41  	if !cmp.Equal(events, want) {
    42  		t.Errorf("Issues.ListIssueTimeline = %+v, want %+v", events, want)
    43  	}
    44  
    45  	const methodName = "ListIssueTimeline"
    46  	testBadOptions(t, methodName, func() (err error) {
    47  		_, _, err = client.Issues.ListIssueTimeline(ctx, "\n", "\n", -1, opt)
    48  		return err
    49  	})
    50  
    51  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    52  		got, resp, err := client.Issues.ListIssueTimeline(ctx, "o", "r", 1, opt)
    53  		if got != nil {
    54  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    55  		}
    56  		return resp, err
    57  	})
    58  }
    59  
    60  func TestSource_Marshal(t *testing.T) {
    61  	testJSONMarshal(t, &Source{}, "{}")
    62  
    63  	u := &Source{
    64  		ID:  Int64(1),
    65  		URL: String("url"),
    66  		Actor: &User{
    67  			Login:     String("l"),
    68  			ID:        Int64(1),
    69  			NodeID:    String("n"),
    70  			URL:       String("u"),
    71  			ReposURL:  String("r"),
    72  			EventsURL: String("e"),
    73  			AvatarURL: String("a"),
    74  		},
    75  		Type:  String("type"),
    76  		Issue: &Issue{ID: Int64(1)},
    77  	}
    78  
    79  	want := `{
    80  		"id": 1,
    81  		"url": "url",
    82  		"actor": {
    83  			"login": "l",
    84  			"id": 1,
    85  			"node_id": "n",
    86  			"avatar_url": "a",
    87  			"url": "u",
    88  			"events_url": "e",
    89  			"repos_url": "r"
    90  		},
    91  		"type": "type",
    92  		"issue": {
    93  			"id": 1
    94  		}
    95  	}`
    96  
    97  	testJSONMarshal(t, u, want)
    98  }
    99  
   100  func TestTimeline_Marshal(t *testing.T) {
   101  	testJSONMarshal(t, &Timeline{}, "{}")
   102  
   103  	u := &Timeline{
   104  		ID:        Int64(1),
   105  		URL:       String("url"),
   106  		CommitURL: String("curl"),
   107  		Actor: &User{
   108  			Login:           String("l"),
   109  			ID:              Int64(1),
   110  			URL:             String("u"),
   111  			AvatarURL:       String("a"),
   112  			GravatarID:      String("g"),
   113  			Name:            String("n"),
   114  			Company:         String("c"),
   115  			Blog:            String("b"),
   116  			Location:        String("l"),
   117  			Email:           String("e"),
   118  			Hireable:        Bool(true),
   119  			Bio:             String("b"),
   120  			TwitterUsername: String("t"),
   121  			PublicRepos:     Int(1),
   122  			Followers:       Int(1),
   123  			Following:       Int(1),
   124  			CreatedAt:       &Timestamp{referenceTime},
   125  			SuspendedAt:     &Timestamp{referenceTime},
   126  		},
   127  		Event:     String("event"),
   128  		CommitID:  String("cid"),
   129  		CreatedAt: &referenceTime,
   130  		Label:     &Label{ID: Int64(1)},
   131  		Assignee: &User{
   132  			Login:           String("l"),
   133  			ID:              Int64(1),
   134  			URL:             String("u"),
   135  			AvatarURL:       String("a"),
   136  			GravatarID:      String("g"),
   137  			Name:            String("n"),
   138  			Company:         String("c"),
   139  			Blog:            String("b"),
   140  			Location:        String("l"),
   141  			Email:           String("e"),
   142  			Hireable:        Bool(true),
   143  			Bio:             String("b"),
   144  			TwitterUsername: String("t"),
   145  			PublicRepos:     Int(1),
   146  			Followers:       Int(1),
   147  			Following:       Int(1),
   148  			CreatedAt:       &Timestamp{referenceTime},
   149  			SuspendedAt:     &Timestamp{referenceTime},
   150  		},
   151  		Milestone: &Milestone{ID: Int64(1)},
   152  		Source: &Source{
   153  			ID:  Int64(1),
   154  			URL: String("url"),
   155  			Actor: &User{
   156  				Login:     String("l"),
   157  				ID:        Int64(1),
   158  				NodeID:    String("n"),
   159  				URL:       String("u"),
   160  				ReposURL:  String("r"),
   161  				EventsURL: String("e"),
   162  				AvatarURL: String("a"),
   163  			},
   164  			Type:  String("type"),
   165  			Issue: &Issue{ID: Int64(1)},
   166  		},
   167  		Rename: &Rename{
   168  			From: String("from"),
   169  			To:   String("to"),
   170  		},
   171  		ProjectCard: &ProjectCard{ID: Int64(1)},
   172  		State:       String("state"),
   173  	}
   174  
   175  	want := `{
   176  		"id": 1,
   177  		"url": "url",
   178  		"commit_url": "curl",
   179  		"actor": {
   180  			"login": "l",
   181  			"id": 1,
   182  			"avatar_url": "a",
   183  			"gravatar_id": "g",
   184  			"name": "n",
   185  			"company": "c",
   186  			"blog": "b",
   187  			"location": "l",
   188  			"email": "e",
   189  			"hireable": true,
   190  			"bio": "b",
   191  			"twitter_username": "t",
   192  			"public_repos": 1,
   193  			"followers": 1,
   194  			"following": 1,
   195  			"created_at": ` + referenceTimeStr + `,
   196  			"suspended_at": ` + referenceTimeStr + `,
   197  			"url": "u"
   198  		},
   199  		"event": "event",
   200  		"commit_id": "cid",
   201  		"created_at": ` + referenceTimeStr + `,
   202  		"label": {
   203  			"id": 1
   204  		},
   205  		"assignee": {
   206  			"login": "l",
   207  			"id": 1,
   208  			"avatar_url": "a",
   209  			"gravatar_id": "g",
   210  			"name": "n",
   211  			"company": "c",
   212  			"blog": "b",
   213  			"location": "l",
   214  			"email": "e",
   215  			"hireable": true,
   216  			"bio": "b",
   217  			"twitter_username": "t",
   218  			"public_repos": 1,
   219  			"followers": 1,
   220  			"following": 1,
   221  			"created_at": ` + referenceTimeStr + `,
   222  			"suspended_at": ` + referenceTimeStr + `,
   223  			"url": "u"
   224  		},
   225  		"milestone": {
   226  			"id": 1
   227  		},
   228  		"source": {
   229  			"id": 1,
   230  			"url": "url",
   231  			"actor": {
   232  				"login": "l",
   233  				"id": 1,
   234  				"node_id": "n",
   235  				"avatar_url": "a",
   236  				"url": "u",
   237  				"events_url": "e",
   238  				"repos_url": "r"
   239  			},
   240  			"type": "type",
   241  			"issue": {
   242  				"id": 1
   243  			}
   244  		},
   245  		"rename": {
   246  			"from": "from",
   247  			"to": "to"
   248  		},
   249  		"project_card": {
   250  			"id": 1
   251  		},
   252  		"state": "state"
   253  	}`
   254  
   255  	testJSONMarshal(t, u, want)
   256  }
   257  

View as plain text