github.com/google/go-github/v33@v33.0.0/github/repos_stats_test.go (about) 1 // Copyright 2014 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 "reflect" 13 "testing" 14 "time" 15 ) 16 17 func TestRepositoriesService_ListContributorsStats(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/repos/o/r/stats/contributors", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 24 fmt.Fprint(w, ` 25 [ 26 { 27 "author": { 28 "id": 1, 29 "node_id": "nodeid-1" 30 }, 31 "total": 135, 32 "weeks": [ 33 { 34 "w": 1367712000, 35 "a": 6898, 36 "d": 77, 37 "c": 10 38 } 39 ] 40 } 41 ] 42 `) 43 }) 44 45 stats, _, err := client.Repositories.ListContributorsStats(context.Background(), "o", "r") 46 if err != nil { 47 t.Errorf("RepositoriesService.ListContributorsStats returned error: %v", err) 48 } 49 50 want := []*ContributorStats{ 51 { 52 Author: &Contributor{ 53 ID: Int64(1), 54 NodeID: String("nodeid-1"), 55 }, 56 Total: Int(135), 57 Weeks: []*WeeklyStats{ 58 { 59 Week: &Timestamp{time.Date(2013, time.May, 05, 00, 00, 00, 0, time.UTC).Local()}, 60 Additions: Int(6898), 61 Deletions: Int(77), 62 Commits: Int(10), 63 }, 64 }, 65 }, 66 } 67 68 if !reflect.DeepEqual(stats, want) { 69 t.Errorf("RepositoriesService.ListContributorsStats returned %+v, want %+v", stats, want) 70 } 71 } 72 73 func TestRepositoriesService_ListCommitActivity(t *testing.T) { 74 client, mux, _, teardown := setup() 75 defer teardown() 76 77 mux.HandleFunc("/repos/o/r/stats/commit_activity", func(w http.ResponseWriter, r *http.Request) { 78 testMethod(t, r, "GET") 79 80 fmt.Fprint(w, ` 81 [ 82 { 83 "days": [0, 3, 26, 20, 39, 1, 0], 84 "total": 89, 85 "week": 1336280400 86 } 87 ] 88 `) 89 }) 90 91 activity, _, err := client.Repositories.ListCommitActivity(context.Background(), "o", "r") 92 if err != nil { 93 t.Errorf("RepositoriesService.ListCommitActivity returned error: %v", err) 94 } 95 96 want := []*WeeklyCommitActivity{ 97 { 98 Days: []int{0, 3, 26, 20, 39, 1, 0}, 99 Total: Int(89), 100 Week: &Timestamp{time.Date(2012, time.May, 06, 05, 00, 00, 0, time.UTC).Local()}, 101 }, 102 } 103 104 if !reflect.DeepEqual(activity, want) { 105 t.Errorf("RepositoriesService.ListCommitActivity returned %+v, want %+v", activity, want) 106 } 107 } 108 109 func TestRepositoriesService_ListCodeFrequency(t *testing.T) { 110 client, mux, _, teardown := setup() 111 defer teardown() 112 113 mux.HandleFunc("/repos/o/r/stats/code_frequency", func(w http.ResponseWriter, r *http.Request) { 114 testMethod(t, r, "GET") 115 116 fmt.Fprint(w, `[[1302998400, 1124, -435]]`) 117 }) 118 119 code, _, err := client.Repositories.ListCodeFrequency(context.Background(), "o", "r") 120 if err != nil { 121 t.Errorf("RepositoriesService.ListCodeFrequency returned error: %v", err) 122 } 123 124 want := []*WeeklyStats{{ 125 Week: &Timestamp{time.Date(2011, time.April, 17, 00, 00, 00, 0, time.UTC).Local()}, 126 Additions: Int(1124), 127 Deletions: Int(-435), 128 }} 129 130 if !reflect.DeepEqual(code, want) { 131 t.Errorf("RepositoriesService.ListCodeFrequency returned %+v, want %+v", code, want) 132 } 133 } 134 135 func TestRepositoriesService_Participation(t *testing.T) { 136 client, mux, _, teardown := setup() 137 defer teardown() 138 139 mux.HandleFunc("/repos/o/r/stats/participation", func(w http.ResponseWriter, r *http.Request) { 140 testMethod(t, r, "GET") 141 142 fmt.Fprint(w, ` 143 { 144 "all": [ 145 11,21,15,2,8,1,8,23,17,21,11,10,33, 146 91,38,34,22,23,32,3,43,87,71,18,13,5, 147 13,16,66,27,12,45,110,117,13,8,18,9,19, 148 26,39,12,20,31,46,91,45,10,24,9,29,7 149 ], 150 "owner": [ 151 3,2,3,0,2,0,5,14,7,9,1,5,0, 152 48,19,2,0,1,10,2,23,40,35,8,8,2, 153 10,6,30,0,2,9,53,104,3,3,10,4,7, 154 11,21,4,4,22,26,63,11,2,14,1,10,3 155 ] 156 } 157 `) 158 }) 159 160 participation, _, err := client.Repositories.ListParticipation(context.Background(), "o", "r") 161 if err != nil { 162 t.Errorf("RepositoriesService.ListParticipation returned error: %v", err) 163 } 164 165 want := &RepositoryParticipation{ 166 All: []int{ 167 11, 21, 15, 2, 8, 1, 8, 23, 17, 21, 11, 10, 33, 168 91, 38, 34, 22, 23, 32, 3, 43, 87, 71, 18, 13, 5, 169 13, 16, 66, 27, 12, 45, 110, 117, 13, 8, 18, 9, 19, 170 26, 39, 12, 20, 31, 46, 91, 45, 10, 24, 9, 29, 7, 171 }, 172 Owner: []int{ 173 3, 2, 3, 0, 2, 0, 5, 14, 7, 9, 1, 5, 0, 174 48, 19, 2, 0, 1, 10, 2, 23, 40, 35, 8, 8, 2, 175 10, 6, 30, 0, 2, 9, 53, 104, 3, 3, 10, 4, 7, 176 11, 21, 4, 4, 22, 26, 63, 11, 2, 14, 1, 10, 3, 177 }, 178 } 179 180 if !reflect.DeepEqual(participation, want) { 181 t.Errorf("RepositoriesService.ListParticipation returned %+v, want %+v", participation, want) 182 } 183 } 184 185 func TestRepositoriesService_ListPunchCard(t *testing.T) { 186 client, mux, _, teardown := setup() 187 defer teardown() 188 189 mux.HandleFunc("/repos/o/r/stats/punch_card", func(w http.ResponseWriter, r *http.Request) { 190 testMethod(t, r, "GET") 191 192 fmt.Fprint(w, `[ 193 [0, 0, 5], 194 [0, 1, 43], 195 [0, 2, 21] 196 ]`) 197 }) 198 199 card, _, err := client.Repositories.ListPunchCard(context.Background(), "o", "r") 200 if err != nil { 201 t.Errorf("RepositoriesService.ListPunchCard returned error: %v", err) 202 } 203 204 want := []*PunchCard{ 205 {Day: Int(0), Hour: Int(0), Commits: Int(5)}, 206 {Day: Int(0), Hour: Int(1), Commits: Int(43)}, 207 {Day: Int(0), Hour: Int(2), Commits: Int(21)}, 208 } 209 210 if !reflect.DeepEqual(card, want) { 211 t.Errorf("RepositoriesService.ListPunchCard returned %+v, want %+v", card, want) 212 } 213 } 214 215 func TestRepositoriesService_AcceptedError(t *testing.T) { 216 client, mux, _, teardown := setup() 217 defer teardown() 218 219 mux.HandleFunc("/repos/o/r/stats/contributors", func(w http.ResponseWriter, r *http.Request) { 220 testMethod(t, r, "GET") 221 // This response indicates the fork will happen asynchronously. 222 w.WriteHeader(http.StatusAccepted) 223 fmt.Fprint(w, `{"id":1}`) 224 }) 225 226 stats, _, err := client.Repositories.ListContributorsStats(context.Background(), "o", "r") 227 if err == nil { 228 t.Errorf("RepositoriesService.AcceptedError should have returned an error") 229 } 230 231 if _, ok := err.(*AcceptedError); !ok { 232 t.Errorf("RepositoriesService.AcceptedError returned an AcceptedError: %v", err) 233 } 234 235 if stats != nil { 236 t.Errorf("RepositoriesService.AcceptedError expected stats to be nil: %v", stats) 237 } 238 }