github.com/google/go-github/v33@v33.0.0/github/gists_comments_test.go (about) 1 // Copyright 2013 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 "time" 16 ) 17 18 func TestGistComments_marshall(t *testing.T) { 19 testJSONMarshal(t, &GistComment{}, "{}") 20 21 createdAt := time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC) 22 23 u := &GistComment{ 24 ID: Int64(1), 25 URL: String("u"), 26 Body: String("test gist comment"), 27 User: &User{ 28 Login: String("ll"), 29 ID: Int64(123), 30 AvatarURL: String("a"), 31 GravatarID: String("g"), 32 Name: String("n"), 33 Company: String("c"), 34 Blog: String("b"), 35 Location: String("l"), 36 Email: String("e"), 37 Hireable: Bool(true), 38 PublicRepos: Int(1), 39 Followers: Int(1), 40 Following: Int(1), 41 CreatedAt: &Timestamp{referenceTime}, 42 URL: String("u"), 43 }, 44 CreatedAt: &createdAt, 45 } 46 47 want := `{ 48 "id": 1, 49 "url": "u", 50 "body": "test gist comment", 51 "user": { 52 "login": "ll", 53 "id": 123, 54 "avatar_url": "a", 55 "gravatar_id": "g", 56 "name": "n", 57 "company": "c", 58 "blog": "b", 59 "location": "l", 60 "email": "e", 61 "hireable": true, 62 "public_repos": 1, 63 "followers": 1, 64 "following": 1, 65 "created_at": ` + referenceTimeStr + `, 66 "url": "u" 67 }, 68 "created_at": "2002-02-10T15:30:00Z" 69 }` 70 71 testJSONMarshal(t, u, want) 72 } 73 func TestGistsService_ListComments(t *testing.T) { 74 client, mux, _, teardown := setup() 75 defer teardown() 76 77 mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { 78 testMethod(t, r, "GET") 79 testFormValues(t, r, values{"page": "2"}) 80 fmt.Fprint(w, `[{"id": 1}]`) 81 }) 82 83 opt := &ListOptions{Page: 2} 84 comments, _, err := client.Gists.ListComments(context.Background(), "1", opt) 85 if err != nil { 86 t.Errorf("Gists.Comments returned error: %v", err) 87 } 88 89 want := []*GistComment{{ID: Int64(1)}} 90 if !reflect.DeepEqual(comments, want) { 91 t.Errorf("Gists.ListComments returned %+v, want %+v", comments, want) 92 } 93 } 94 95 func TestGistsService_ListComments_invalidID(t *testing.T) { 96 client, _, _, teardown := setup() 97 defer teardown() 98 99 _, _, err := client.Gists.ListComments(context.Background(), "%", nil) 100 testURLParseError(t, err) 101 } 102 103 func TestGistsService_GetComment(t *testing.T) { 104 client, mux, _, teardown := setup() 105 defer teardown() 106 107 mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { 108 testMethod(t, r, "GET") 109 fmt.Fprint(w, `{"id": 1}`) 110 }) 111 112 comment, _, err := client.Gists.GetComment(context.Background(), "1", 2) 113 if err != nil { 114 t.Errorf("Gists.GetComment returned error: %v", err) 115 } 116 117 want := &GistComment{ID: Int64(1)} 118 if !reflect.DeepEqual(comment, want) { 119 t.Errorf("Gists.GetComment returned %+v, want %+v", comment, want) 120 } 121 } 122 123 func TestGistsService_GetComment_invalidID(t *testing.T) { 124 client, _, _, teardown := setup() 125 defer teardown() 126 127 _, _, err := client.Gists.GetComment(context.Background(), "%", 1) 128 testURLParseError(t, err) 129 } 130 131 func TestGistsService_CreateComment(t *testing.T) { 132 client, mux, _, teardown := setup() 133 defer teardown() 134 135 input := &GistComment{ID: Int64(1), Body: String("b")} 136 137 mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { 138 v := new(GistComment) 139 json.NewDecoder(r.Body).Decode(v) 140 141 testMethod(t, r, "POST") 142 if !reflect.DeepEqual(v, input) { 143 t.Errorf("Request body = %+v, want %+v", v, input) 144 } 145 146 fmt.Fprint(w, `{"id":1}`) 147 }) 148 149 comment, _, err := client.Gists.CreateComment(context.Background(), "1", input) 150 if err != nil { 151 t.Errorf("Gists.CreateComment returned error: %v", err) 152 } 153 154 want := &GistComment{ID: Int64(1)} 155 if !reflect.DeepEqual(comment, want) { 156 t.Errorf("Gists.CreateComment returned %+v, want %+v", comment, want) 157 } 158 } 159 160 func TestGistsService_CreateComment_invalidID(t *testing.T) { 161 client, _, _, teardown := setup() 162 defer teardown() 163 164 _, _, err := client.Gists.CreateComment(context.Background(), "%", nil) 165 testURLParseError(t, err) 166 } 167 168 func TestGistsService_EditComment(t *testing.T) { 169 client, mux, _, teardown := setup() 170 defer teardown() 171 172 input := &GistComment{ID: Int64(1), Body: String("b")} 173 174 mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { 175 v := new(GistComment) 176 json.NewDecoder(r.Body).Decode(v) 177 178 testMethod(t, r, "PATCH") 179 if !reflect.DeepEqual(v, input) { 180 t.Errorf("Request body = %+v, want %+v", v, input) 181 } 182 183 fmt.Fprint(w, `{"id":1}`) 184 }) 185 186 comment, _, err := client.Gists.EditComment(context.Background(), "1", 2, input) 187 if err != nil { 188 t.Errorf("Gists.EditComment returned error: %v", err) 189 } 190 191 want := &GistComment{ID: Int64(1)} 192 if !reflect.DeepEqual(comment, want) { 193 t.Errorf("Gists.EditComment returned %+v, want %+v", comment, want) 194 } 195 } 196 197 func TestGistsService_EditComment_invalidID(t *testing.T) { 198 client, _, _, teardown := setup() 199 defer teardown() 200 201 _, _, err := client.Gists.EditComment(context.Background(), "%", 1, nil) 202 testURLParseError(t, err) 203 } 204 205 func TestGistsService_DeleteComment(t *testing.T) { 206 client, mux, _, teardown := setup() 207 defer teardown() 208 209 mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { 210 testMethod(t, r, "DELETE") 211 }) 212 213 _, err := client.Gists.DeleteComment(context.Background(), "1", 2) 214 if err != nil { 215 t.Errorf("Gists.Delete returned error: %v", err) 216 } 217 } 218 219 func TestGistsService_DeleteComment_invalidID(t *testing.T) { 220 client, _, _, teardown := setup() 221 defer teardown() 222 223 _, err := client.Gists.DeleteComment(context.Background(), "%", 1) 224 testURLParseError(t, err) 225 }