github.com/google/go-github/v66@v66.0.0/github/issues_timeline_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 "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 t.Parallel() 20 client, mux, _ := setup(t) 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 t.Parallel() 62 testJSONMarshal(t, &Source{}, "{}") 63 64 u := &Source{ 65 ID: Int64(1), 66 URL: String("url"), 67 Actor: &User{ 68 Login: String("l"), 69 ID: Int64(1), 70 NodeID: String("n"), 71 URL: String("u"), 72 ReposURL: String("r"), 73 EventsURL: String("e"), 74 AvatarURL: String("a"), 75 }, 76 Type: String("type"), 77 Issue: &Issue{ID: Int64(1)}, 78 } 79 80 want := `{ 81 "id": 1, 82 "url": "url", 83 "actor": { 84 "login": "l", 85 "id": 1, 86 "node_id": "n", 87 "avatar_url": "a", 88 "url": "u", 89 "events_url": "e", 90 "repos_url": "r" 91 }, 92 "type": "type", 93 "issue": { 94 "id": 1 95 } 96 }` 97 98 testJSONMarshal(t, u, want) 99 } 100 101 func TestTimeline_Marshal(t *testing.T) { 102 t.Parallel() 103 testJSONMarshal(t, &Timeline{}, "{}") 104 105 u := &Timeline{ 106 ID: Int64(1), 107 URL: String("url"), 108 CommitURL: String("curl"), 109 Actor: &User{ 110 Login: String("l"), 111 ID: Int64(1), 112 URL: String("u"), 113 AvatarURL: String("a"), 114 GravatarID: String("g"), 115 Name: String("n"), 116 Company: String("c"), 117 Blog: String("b"), 118 Location: String("l"), 119 Email: String("e"), 120 Hireable: Bool(true), 121 Bio: String("b"), 122 TwitterUsername: String("t"), 123 PublicRepos: Int(1), 124 Followers: Int(1), 125 Following: Int(1), 126 CreatedAt: &Timestamp{referenceTime}, 127 SuspendedAt: &Timestamp{referenceTime}, 128 }, 129 Event: String("event"), 130 CommitID: String("cid"), 131 CreatedAt: &Timestamp{referenceTime}, 132 Label: &Label{ID: Int64(1)}, 133 Assignee: &User{ 134 Login: String("l"), 135 ID: Int64(1), 136 URL: String("u"), 137 AvatarURL: String("a"), 138 GravatarID: String("g"), 139 Name: String("n"), 140 Company: String("c"), 141 Blog: String("b"), 142 Location: String("l"), 143 Email: String("e"), 144 Hireable: Bool(true), 145 Bio: String("b"), 146 TwitterUsername: String("t"), 147 PublicRepos: Int(1), 148 Followers: Int(1), 149 Following: Int(1), 150 CreatedAt: &Timestamp{referenceTime}, 151 SuspendedAt: &Timestamp{referenceTime}, 152 }, 153 Milestone: &Milestone{ID: Int64(1)}, 154 Source: &Source{ 155 ID: Int64(1), 156 URL: String("url"), 157 Actor: &User{ 158 Login: String("l"), 159 ID: Int64(1), 160 NodeID: String("n"), 161 URL: String("u"), 162 ReposURL: String("r"), 163 EventsURL: String("e"), 164 AvatarURL: String("a"), 165 }, 166 Type: String("type"), 167 Issue: &Issue{ID: Int64(1)}, 168 }, 169 Rename: &Rename{ 170 From: String("from"), 171 To: String("to"), 172 }, 173 ProjectCard: &ProjectCard{ID: Int64(1)}, 174 State: String("state"), 175 } 176 177 want := `{ 178 "id": 1, 179 "url": "url", 180 "commit_url": "curl", 181 "actor": { 182 "login": "l", 183 "id": 1, 184 "avatar_url": "a", 185 "gravatar_id": "g", 186 "name": "n", 187 "company": "c", 188 "blog": "b", 189 "location": "l", 190 "email": "e", 191 "hireable": true, 192 "bio": "b", 193 "twitter_username": "t", 194 "public_repos": 1, 195 "followers": 1, 196 "following": 1, 197 "created_at": ` + referenceTimeStr + `, 198 "suspended_at": ` + referenceTimeStr + `, 199 "url": "u" 200 }, 201 "event": "event", 202 "commit_id": "cid", 203 "created_at": ` + referenceTimeStr + `, 204 "label": { 205 "id": 1 206 }, 207 "assignee": { 208 "login": "l", 209 "id": 1, 210 "avatar_url": "a", 211 "gravatar_id": "g", 212 "name": "n", 213 "company": "c", 214 "blog": "b", 215 "location": "l", 216 "email": "e", 217 "hireable": true, 218 "bio": "b", 219 "twitter_username": "t", 220 "public_repos": 1, 221 "followers": 1, 222 "following": 1, 223 "created_at": ` + referenceTimeStr + `, 224 "suspended_at": ` + referenceTimeStr + `, 225 "url": "u" 226 }, 227 "milestone": { 228 "id": 1 229 }, 230 "source": { 231 "id": 1, 232 "url": "url", 233 "actor": { 234 "login": "l", 235 "id": 1, 236 "node_id": "n", 237 "avatar_url": "a", 238 "url": "u", 239 "events_url": "e", 240 "repos_url": "r" 241 }, 242 "type": "type", 243 "issue": { 244 "id": 1 245 } 246 }, 247 "rename": { 248 "from": "from", 249 "to": "to" 250 }, 251 "project_card": { 252 "id": 1 253 }, 254 "state": "state" 255 }` 256 257 testJSONMarshal(t, u, want) 258 }