github.com/google/go-github/v66@v66.0.0/github/repos_traffic_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 "testing" 13 "time" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestRepositoriesService_ListTrafficReferrers(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 mux.HandleFunc("/repos/o/r/traffic/popular/referrers", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 fmt.Fprintf(w, `[{ 25 "referrer": "Google", 26 "count": 4, 27 "uniques": 3 28 }]`) 29 }) 30 ctx := context.Background() 31 got, _, err := client.Repositories.ListTrafficReferrers(ctx, "o", "r") 32 if err != nil { 33 t.Errorf("Repositories.ListTrafficReferrers returned error: %+v", err) 34 } 35 36 want := []*TrafficReferrer{{ 37 Referrer: String("Google"), 38 Count: Int(4), 39 Uniques: Int(3), 40 }} 41 if !cmp.Equal(got, want) { 42 t.Errorf("Repositories.ListTrafficReferrers returned %+v, want %+v", got, want) 43 } 44 45 const methodName = "ListTrafficReferrers" 46 testBadOptions(t, methodName, func() (err error) { 47 _, _, err = client.Repositories.ListTrafficReferrers(ctx, "\n", "\n") 48 return err 49 }) 50 51 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 52 got, resp, err := client.Repositories.ListTrafficReferrers(ctx, "o", "r") 53 if got != nil { 54 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 55 } 56 return resp, err 57 }) 58 } 59 60 func TestRepositoriesService_ListTrafficPaths(t *testing.T) { 61 t.Parallel() 62 client, mux, _ := setup(t) 63 64 mux.HandleFunc("/repos/o/r/traffic/popular/paths", func(w http.ResponseWriter, r *http.Request) { 65 testMethod(t, r, "GET") 66 fmt.Fprintf(w, `[{ 67 "path": "/github/hubot", 68 "title": "github/hubot: A customizable life embetterment robot.", 69 "count": 3542, 70 "uniques": 2225 71 }]`) 72 }) 73 ctx := context.Background() 74 got, _, err := client.Repositories.ListTrafficPaths(ctx, "o", "r") 75 if err != nil { 76 t.Errorf("Repositories.ListTrafficPaths returned error: %+v", err) 77 } 78 79 want := []*TrafficPath{{ 80 Path: String("/github/hubot"), 81 Title: String("github/hubot: A customizable life embetterment robot."), 82 Count: Int(3542), 83 Uniques: Int(2225), 84 }} 85 if !cmp.Equal(got, want) { 86 t.Errorf("Repositories.ListTrafficPaths returned %+v, want %+v", got, want) 87 } 88 89 const methodName = "ListTrafficPaths" 90 testBadOptions(t, methodName, func() (err error) { 91 _, _, err = client.Repositories.ListTrafficPaths(ctx, "\n", "\n") 92 return err 93 }) 94 95 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 96 got, resp, err := client.Repositories.ListTrafficPaths(ctx, "o", "r") 97 if got != nil { 98 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 99 } 100 return resp, err 101 }) 102 } 103 104 func TestRepositoriesService_ListTrafficViews(t *testing.T) { 105 t.Parallel() 106 client, mux, _ := setup(t) 107 108 mux.HandleFunc("/repos/o/r/traffic/views", func(w http.ResponseWriter, r *http.Request) { 109 testMethod(t, r, "GET") 110 fmt.Fprintf(w, `{"count": 7, 111 "uniques": 6, 112 "views": [{ 113 "timestamp": "2016-05-31T16:00:00.000Z", 114 "count": 7, 115 "uniques": 6 116 }]}`) 117 }) 118 119 ctx := context.Background() 120 got, _, err := client.Repositories.ListTrafficViews(ctx, "o", "r", nil) 121 if err != nil { 122 t.Errorf("Repositories.ListTrafficViews returned error: %+v", err) 123 } 124 125 want := &TrafficViews{ 126 Views: []*TrafficData{{ 127 Timestamp: &Timestamp{time.Date(2016, time.May, 31, 16, 0, 0, 0, time.UTC)}, 128 Count: Int(7), 129 Uniques: Int(6), 130 }}, 131 Count: Int(7), 132 Uniques: Int(6), 133 } 134 135 if !cmp.Equal(got, want) { 136 t.Errorf("Repositories.ListTrafficViews returned %+v, want %+v", got, want) 137 } 138 139 const methodName = "ListTrafficViews" 140 testBadOptions(t, methodName, func() (err error) { 141 _, _, err = client.Repositories.ListTrafficViews(ctx, "\n", "\n", &TrafficBreakdownOptions{}) 142 return err 143 }) 144 145 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 146 got, resp, err := client.Repositories.ListTrafficViews(ctx, "o", "r", nil) 147 if got != nil { 148 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 149 } 150 return resp, err 151 }) 152 } 153 154 func TestRepositoriesService_ListTrafficClones(t *testing.T) { 155 t.Parallel() 156 client, mux, _ := setup(t) 157 158 mux.HandleFunc("/repos/o/r/traffic/clones", func(w http.ResponseWriter, r *http.Request) { 159 testMethod(t, r, "GET") 160 fmt.Fprintf(w, `{"count": 7, 161 "uniques": 6, 162 "clones": [{ 163 "timestamp": "2016-05-31T16:00:00.00Z", 164 "count": 7, 165 "uniques": 6 166 }]}`) 167 }) 168 169 ctx := context.Background() 170 got, _, err := client.Repositories.ListTrafficClones(ctx, "o", "r", nil) 171 if err != nil { 172 t.Errorf("Repositories.ListTrafficClones returned error: %+v", err) 173 } 174 175 want := &TrafficClones{ 176 Clones: []*TrafficData{{ 177 Timestamp: &Timestamp{time.Date(2016, time.May, 31, 16, 0, 0, 0, time.UTC)}, 178 Count: Int(7), 179 Uniques: Int(6), 180 }}, 181 Count: Int(7), 182 Uniques: Int(6), 183 } 184 185 if !cmp.Equal(got, want) { 186 t.Errorf("Repositories.ListTrafficClones returned %+v, want %+v", got, want) 187 } 188 189 const methodName = "ListTrafficClones" 190 testBadOptions(t, methodName, func() (err error) { 191 _, _, err = client.Repositories.ListTrafficClones(ctx, "\n", "\n", &TrafficBreakdownOptions{}) 192 return err 193 }) 194 195 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 196 got, resp, err := client.Repositories.ListTrafficClones(ctx, "o", "r", nil) 197 if got != nil { 198 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 199 } 200 return resp, err 201 }) 202 } 203 204 func TestTrafficReferrer_Marshal(t *testing.T) { 205 t.Parallel() 206 testJSONMarshal(t, &TrafficReferrer{}, "{}") 207 208 u := &TrafficReferrer{ 209 Referrer: String("referrer"), 210 Count: Int(0), 211 Uniques: Int(0), 212 } 213 214 want := `{ 215 "referrer" : "referrer", 216 "count" : 0, 217 "uniques" : 0 218 }` 219 220 testJSONMarshal(t, u, want) 221 } 222 223 func TestTrafficViews_Marshal(t *testing.T) { 224 t.Parallel() 225 testJSONMarshal(t, &TrafficViews{}, "{}") 226 227 u := &TrafficViews{ 228 Views: []*TrafficData{{ 229 Timestamp: &Timestamp{time.Date(2016, time.May, 31, 16, 0, 0, 0, time.UTC)}, 230 Count: Int(7), 231 Uniques: Int(6), 232 }}, 233 Count: Int(0), 234 Uniques: Int(0), 235 } 236 237 want := `{ 238 "views": [{ 239 "timestamp": "2016-05-31T16:00:00.000Z", 240 "count": 7, 241 "uniques": 6 242 }], 243 "count" : 0, 244 "uniques" : 0 245 }` 246 247 testJSONMarshal(t, u, want) 248 } 249 250 func TestTrafficClones_Marshal(t *testing.T) { 251 t.Parallel() 252 testJSONMarshal(t, &TrafficClones{}, "{}") 253 254 u := &TrafficClones{ 255 Clones: []*TrafficData{{ 256 Timestamp: &Timestamp{time.Date(2021, time.October, 29, 16, 0, 0, 0, time.UTC)}, 257 Count: Int(1), 258 Uniques: Int(1), 259 }}, 260 Count: Int(0), 261 Uniques: Int(0), 262 } 263 264 want := `{ 265 "clones": [{ 266 "timestamp": "2021-10-29T16:00:00.000Z", 267 "count": 1, 268 "uniques": 1 269 }], 270 "count" : 0, 271 "uniques" : 0 272 }` 273 274 testJSONMarshal(t, u, want) 275 } 276 277 func TestTrafficPath_Marshal(t *testing.T) { 278 t.Parallel() 279 testJSONMarshal(t, &TrafficPath{}, "{}") 280 281 u := &TrafficPath{ 282 Path: String("test/path"), 283 Title: String("test"), 284 Count: Int(2), 285 Uniques: Int(3), 286 } 287 288 want := `{ 289 "path" : "test/path", 290 "title": "test", 291 "count": 2, 292 "uniques": 3 293 }` 294 295 testJSONMarshal(t, u, want) 296 } 297 298 func TestTrafficData_Marshal(t *testing.T) { 299 t.Parallel() 300 testJSONMarshal(t, &TrafficData{}, "{}") 301 302 u := &TrafficData{ 303 Timestamp: &Timestamp{time.Date(2016, time.May, 31, 16, 0, 0, 0, time.UTC)}, 304 Count: Int(7), 305 Uniques: Int(6), 306 } 307 308 want := `{ 309 "timestamp": "2016-05-31T16:00:00.000Z", 310 "count": 7, 311 "uniques": 6 312 }` 313 314 testJSONMarshal(t, u, want) 315 } 316 317 func TestTrafficBreakdownOptions_Marshal(t *testing.T) { 318 t.Parallel() 319 testJSONMarshal(t, &TrafficBreakdownOptions{}, "{}") 320 321 u := &TrafficBreakdownOptions{ 322 Per: "day", 323 } 324 325 want := `{ 326 "per": "day" 327 }` 328 329 testJSONMarshal(t, u, want) 330 }