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
6package github
7
8import (
9	"context"
10	"fmt"
11	"net/http"
12	"reflect"
13	"testing"
14	"time"
15)
16
17func TestRepositoriesService_ListTrafficReferrers(t *testing.T) {
18	client, mux, _, teardown := setup()
19	defer teardown()
20
21	mux.HandleFunc("/repos/o/r/traffic/popular/referrers", func(w http.ResponseWriter, r *http.Request) {
22		testMethod(t, r, "GET")
23		fmt.Fprintf(w, `[{
24			"referrer": "Google",
25			"count": 4,
26			"uniques": 3
27 		}]`)
28	})
29	referrers, _, err := client.Repositories.ListTrafficReferrers(context.Background(), "o", "r")
30	if err != nil {
31		t.Errorf("Repositories.ListPaths returned error: %+v", err)
32	}
33
34	want := []*TrafficReferrer{{
35		Referrer: String("Google"),
36		Count:    Int(4),
37		Uniques:  Int(3),
38	}}
39	if !reflect.DeepEqual(referrers, want) {
40		t.Errorf("Repositories.ListReferrers returned %+v, want %+v", referrers, want)
41	}
42
43}
44
45func TestRepositoriesService_ListTrafficPaths(t *testing.T) {
46	client, mux, _, teardown := setup()
47	defer teardown()
48
49	mux.HandleFunc("/repos/o/r/traffic/popular/paths", func(w http.ResponseWriter, r *http.Request) {
50		testMethod(t, r, "GET")
51		fmt.Fprintf(w, `[{
52			"path": "/github/hubot",
53			"title": "github/hubot: A customizable life embetterment robot.",
54			"count": 3542,
55			"uniques": 2225
56 		}]`)
57	})
58	paths, _, err := client.Repositories.ListTrafficPaths(context.Background(), "o", "r")
59	if err != nil {
60		t.Errorf("Repositories.ListPaths returned error: %+v", err)
61	}
62
63	want := []*TrafficPath{{
64		Path:    String("/github/hubot"),
65		Title:   String("github/hubot: A customizable life embetterment robot."),
66		Count:   Int(3542),
67		Uniques: Int(2225),
68	}}
69	if !reflect.DeepEqual(paths, want) {
70		t.Errorf("Repositories.ListPaths returned %+v, want %+v", paths, want)
71	}
72
73}
74
75func TestRepositoriesService_ListTrafficViews(t *testing.T) {
76	client, mux, _, teardown := setup()
77	defer teardown()
78
79	mux.HandleFunc("/repos/o/r/traffic/views", func(w http.ResponseWriter, r *http.Request) {
80		testMethod(t, r, "GET")
81		fmt.Fprintf(w, `{"count": 7,
82			"uniques": 6,
83			"views": [{
84				"timestamp": "2016-05-31T16:00:00.000Z",
85				"count": 7,
86				"uniques": 6
87		}]}`)
88	})
89
90	views, _, err := client.Repositories.ListTrafficViews(context.Background(), "o", "r", nil)
91	if err != nil {
92		t.Errorf("Repositories.ListPaths returned error: %+v", err)
93	}
94
95	want := &TrafficViews{
96		Views: []*TrafficData{{
97			Timestamp: &Timestamp{time.Date(2016, time.May, 31, 16, 0, 0, 0, time.UTC)},
98			Count:     Int(7),
99			Uniques:   Int(6),
100		}},
101		Count:   Int(7),
102		Uniques: Int(6),
103	}
104
105	if !reflect.DeepEqual(views, want) {
106		t.Errorf("Repositories.ListViews returned %+v, want %+v", views, want)
107	}
108
109}
110
111func TestRepositoriesService_ListTrafficClones(t *testing.T) {
112	client, mux, _, teardown := setup()
113	defer teardown()
114
115	mux.HandleFunc("/repos/o/r/traffic/clones", func(w http.ResponseWriter, r *http.Request) {
116		testMethod(t, r, "GET")
117		fmt.Fprintf(w, `{"count": 7,
118			"uniques": 6,
119			"clones": [{
120				"timestamp": "2016-05-31T16:00:00.00Z",
121				"count": 7,
122				"uniques": 6
123		}]}`)
124	})
125
126	clones, _, err := client.Repositories.ListTrafficClones(context.Background(), "o", "r", nil)
127	if err != nil {
128		t.Errorf("Repositories.ListPaths returned error: %+v", err)
129	}
130
131	want := &TrafficClones{
132		Clones: []*TrafficData{{
133			Timestamp: &Timestamp{time.Date(2016, time.May, 31, 16, 0, 0, 0, time.UTC)},
134			Count:     Int(7),
135			Uniques:   Int(6),
136		}},
137		Count:   Int(7),
138		Uniques: Int(6),
139	}
140
141	if !reflect.DeepEqual(clones, want) {
142		t.Errorf("Repositories.ListViews returned %+v, want %+v", clones, want)
143	}
144
145}
146