| 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 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 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 |
client, mux, _, teardown := setup() |
| 62 |
defer teardown() |
| 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 |
client, mux, _, teardown := setup() |
| 106 |
defer teardown() |
| 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 |
client, mux, _, teardown := setup() |
| 156 |
defer teardown() |
| 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 |
testJSONMarshal(t, &TrafficReferrer{}, "{}") |
| 206 |
|
| 207 |
u := &TrafficReferrer{ |
| 208 |
Referrer: String("referrer"), |
| 209 |
Count: Int(0), |
| 210 |
Uniques: Int(0), |
| 211 |
} |
| 212 |
|
| 213 |
want := `{ |
| 214 |
"referrer" : "referrer", |
| 215 |
"count" : 0, |
| 216 |
"uniques" : 0 |
| 217 |
}` |
| 218 |
|
| 219 |
testJSONMarshal(t, u, want) |
| 220 |
} |
| 221 |
|
| 222 |
func TestTrafficViews_Marshal(t *testing.T) { |
| 223 |
testJSONMarshal(t, &TrafficViews{}, "{}") |
| 224 |
|
| 225 |
u := &TrafficViews{ |
| 226 |
Views: []*TrafficData{{ |
| 227 |
Timestamp: &Timestamp{time.Date(2016, time.May, 31, 16, 0, 0, 0, time.UTC)}, |
| 228 |
Count: Int(7), |
| 229 |
Uniques: Int(6), |
| 230 |
}}, |
| 231 |
Count: Int(0), |
| 232 |
Uniques: Int(0), |
| 233 |
} |
| 234 |
|
| 235 |
want := `{ |
| 236 |
"views": [{ |
| 237 |
"timestamp": "2016-05-31T16:00:00.000Z", |
| 238 |
"count": 7, |
| 239 |
"uniques": 6 |
| 240 |
}], |
| 241 |
"count" : 0, |
| 242 |
"uniques" : 0 |
| 243 |
}` |
| 244 |
|
| 245 |
testJSONMarshal(t, u, want) |
| 246 |
} |
| 247 |
|
| 248 |
func TestTrafficClones_Marshal(t *testing.T) { |
| 249 |
testJSONMarshal(t, &TrafficClones{}, "{}") |
| 250 |
|
| 251 |
u := &TrafficClones{ |
| 252 |
Clones: []*TrafficData{{ |
| 253 |
Timestamp: &Timestamp{time.Date(2021, time.October, 29, 16, 0, 0, 0, time.UTC)}, |
| 254 |
Count: Int(1), |
| 255 |
Uniques: Int(1), |
| 256 |
}}, |
| 257 |
Count: Int(0), |
| 258 |
Uniques: Int(0), |
| 259 |
} |
| 260 |
|
| 261 |
want := `{ |
| 262 |
"clones": [{ |
| 263 |
"timestamp": "2021-10-29T16:00:00.000Z", |
| 264 |
"count": 1, |
| 265 |
"uniques": 1 |
| 266 |
}], |
| 267 |
"count" : 0, |
| 268 |
"uniques" : 0 |
| 269 |
}` |
| 270 |
|
| 271 |
testJSONMarshal(t, u, want) |
| 272 |
} |
| 273 |
|
| 274 |
func TestTrafficPath_Marshal(t *testing.T) { |
| 275 |
testJSONMarshal(t, &TrafficPath{}, "{}") |
| 276 |
|
| 277 |
u := &TrafficPath{ |
| 278 |
Path: String("test/path"), |
| 279 |
Title: String("test"), |
| 280 |
Count: Int(2), |
| 281 |
Uniques: Int(3), |
| 282 |
} |
| 283 |
|
| 284 |
want := `{ |
| 285 |
"path" : "test/path", |
| 286 |
"title": "test", |
| 287 |
"count": 2, |
| 288 |
"uniques": 3 |
| 289 |
}` |
| 290 |
|
| 291 |
testJSONMarshal(t, u, want) |
| 292 |
} |
| 293 |
|
| 294 |
func TestTrafficData_Marshal(t *testing.T) { |
| 295 |
testJSONMarshal(t, &TrafficData{}, "{}") |
| 296 |
|
| 297 |
u := &TrafficData{ |
| 298 |
Timestamp: &Timestamp{time.Date(2016, time.May, 31, 16, 0, 0, 0, time.UTC)}, |
| 299 |
Count: Int(7), |
| 300 |
Uniques: Int(6), |
| 301 |
} |
| 302 |
|
| 303 |
want := `{ |
| 304 |
"timestamp": "2016-05-31T16:00:00.000Z", |
| 305 |
"count": 7, |
| 306 |
"uniques": 6 |
| 307 |
}` |
| 308 |
|
| 309 |
testJSONMarshal(t, u, want) |
| 310 |
} |
| 311 |
|
| 312 |
func TestTrafficBreakdownOptions_Marshal(t *testing.T) { |
| 313 |
testJSONMarshal(t, &TrafficBreakdownOptions{}, "{}") |
| 314 |
|
| 315 |
u := &TrafficBreakdownOptions{ |
| 316 |
Per: "day", |
| 317 |
} |
| 318 |
|
| 319 |
want := `{ |
| 320 |
"per": "day" |
| 321 |
}` |
| 322 |
|
| 323 |
testJSONMarshal(t, u, want) |
| 324 |
} |