|
| 1 | +package graphql |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestVisibilityConstants(t *testing.T) { |
| 9 | + // Verify constant values are distinct |
| 10 | + if VisibilityAll == VisibilityPublic { |
| 11 | + t.Error("VisibilityAll should not equal VisibilityPublic") |
| 12 | + } |
| 13 | + if VisibilityAll == VisibilityPrivate { |
| 14 | + t.Error("VisibilityAll should not equal VisibilityPrivate") |
| 15 | + } |
| 16 | + if VisibilityPublic == VisibilityPrivate { |
| 17 | + t.Error("VisibilityPublic should not equal VisibilityPrivate") |
| 18 | + } |
| 19 | + |
| 20 | + // Verify order (iota starts at 0) |
| 21 | + if VisibilityAll != 0 { |
| 22 | + t.Errorf("VisibilityAll = %d, want 0", VisibilityAll) |
| 23 | + } |
| 24 | + if VisibilityPublic != 1 { |
| 25 | + t.Errorf("VisibilityPublic = %d, want 1", VisibilityPublic) |
| 26 | + } |
| 27 | + if VisibilityPrivate != 2 { |
| 28 | + t.Errorf("VisibilityPrivate = %d, want 2", VisibilityPrivate) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestMonthlyCommitStatsYearMonth(t *testing.T) { |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + mcs MonthlyCommitStats |
| 36 | + want string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "January 2024", |
| 40 | + mcs: MonthlyCommitStats{Year: 2024, Month: time.January, Commits: 10, Additions: 100, Deletions: 50}, |
| 41 | + want: "2024-01", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "December 2023", |
| 45 | + mcs: MonthlyCommitStats{Year: 2023, Month: time.December, Commits: 5, Additions: 50, Deletions: 25}, |
| 46 | + want: "2023-12", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "June 2025", |
| 50 | + mcs: MonthlyCommitStats{Year: 2025, Month: time.June}, |
| 51 | + want: "2025-06", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "October 2024", |
| 55 | + mcs: MonthlyCommitStats{Year: 2024, Month: time.October, Commits: 30, Additions: 300, Deletions: 150}, |
| 56 | + want: "2024-10", |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + got := tt.mcs.YearMonth() |
| 63 | + if got != tt.want { |
| 64 | + t.Errorf("YearMonth() = %q, want %q", got, tt.want) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestCommitStatsStruct(t *testing.T) { |
| 71 | + from := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) |
| 72 | + to := time.Date(2024, 12, 31, 23, 59, 59, 0, time.UTC) |
| 73 | + |
| 74 | + stats := CommitStats{ |
| 75 | + Username: "testuser", |
| 76 | + From: from, |
| 77 | + To: to, |
| 78 | + Visibility: VisibilityPublic, |
| 79 | + TotalCommits: 100, |
| 80 | + Additions: 5000, |
| 81 | + Deletions: 2000, |
| 82 | + ByMonth: []MonthlyCommitStats{ |
| 83 | + {Year: 2024, Month: time.January, Commits: 50, Additions: 2500, Deletions: 1000}, |
| 84 | + {Year: 2024, Month: time.February, Commits: 50, Additions: 2500, Deletions: 1000}, |
| 85 | + }, |
| 86 | + ByRepo: []RepoCommitStats{ |
| 87 | + {Owner: "owner1", Name: "repo1", IsPrivate: false, Commits: 60, Additions: 3000, Deletions: 1200}, |
| 88 | + {Owner: "owner2", Name: "repo2", IsPrivate: false, Commits: 40, Additions: 2000, Deletions: 800}, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + if stats.Username != "testuser" { |
| 93 | + t.Errorf("Username = %q, want %q", stats.Username, "testuser") |
| 94 | + } |
| 95 | + if stats.Visibility != VisibilityPublic { |
| 96 | + t.Errorf("Visibility = %d, want %d", stats.Visibility, VisibilityPublic) |
| 97 | + } |
| 98 | + if stats.TotalCommits != 100 { |
| 99 | + t.Errorf("TotalCommits = %d, want %d", stats.TotalCommits, 100) |
| 100 | + } |
| 101 | + if stats.Additions != 5000 { |
| 102 | + t.Errorf("Additions = %d, want %d", stats.Additions, 5000) |
| 103 | + } |
| 104 | + if stats.Deletions != 2000 { |
| 105 | + t.Errorf("Deletions = %d, want %d", stats.Deletions, 2000) |
| 106 | + } |
| 107 | + if len(stats.ByMonth) != 2 { |
| 108 | + t.Errorf("ByMonth len = %d, want %d", len(stats.ByMonth), 2) |
| 109 | + } |
| 110 | + if len(stats.ByRepo) != 2 { |
| 111 | + t.Errorf("ByRepo len = %d, want %d", len(stats.ByRepo), 2) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestMonthlyCommitStatsStruct(t *testing.T) { |
| 116 | + mcs := MonthlyCommitStats{ |
| 117 | + Year: 2024, |
| 118 | + Month: time.March, |
| 119 | + Commits: 42, |
| 120 | + Additions: 1000, |
| 121 | + Deletions: 500, |
| 122 | + } |
| 123 | + |
| 124 | + if mcs.Year != 2024 { |
| 125 | + t.Errorf("Year = %d, want %d", mcs.Year, 2024) |
| 126 | + } |
| 127 | + if mcs.Month != time.March { |
| 128 | + t.Errorf("Month = %v, want %v", mcs.Month, time.March) |
| 129 | + } |
| 130 | + if mcs.Commits != 42 { |
| 131 | + t.Errorf("Commits = %d, want %d", mcs.Commits, 42) |
| 132 | + } |
| 133 | + if mcs.Additions != 1000 { |
| 134 | + t.Errorf("Additions = %d, want %d", mcs.Additions, 1000) |
| 135 | + } |
| 136 | + if mcs.Deletions != 500 { |
| 137 | + t.Errorf("Deletions = %d, want %d", mcs.Deletions, 500) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestRepoCommitStatsStruct(t *testing.T) { |
| 142 | + rcs := RepoCommitStats{ |
| 143 | + Owner: "myorg", |
| 144 | + Name: "myrepo", |
| 145 | + IsPrivate: true, |
| 146 | + Commits: 50, |
| 147 | + Additions: 2500, |
| 148 | + Deletions: 1000, |
| 149 | + } |
| 150 | + |
| 151 | + if rcs.Owner != "myorg" { |
| 152 | + t.Errorf("Owner = %q, want %q", rcs.Owner, "myorg") |
| 153 | + } |
| 154 | + if rcs.Name != "myrepo" { |
| 155 | + t.Errorf("Name = %q, want %q", rcs.Name, "myrepo") |
| 156 | + } |
| 157 | + if !rcs.IsPrivate { |
| 158 | + t.Error("IsPrivate = false, want true") |
| 159 | + } |
| 160 | + if rcs.Commits != 50 { |
| 161 | + t.Errorf("Commits = %d, want %d", rcs.Commits, 50) |
| 162 | + } |
| 163 | + if rcs.Additions != 2500 { |
| 164 | + t.Errorf("Additions = %d, want %d", rcs.Additions, 2500) |
| 165 | + } |
| 166 | + if rcs.Deletions != 1000 { |
| 167 | + t.Errorf("Deletions = %d, want %d", rcs.Deletions, 1000) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestRepoCommitStatsPublic(t *testing.T) { |
| 172 | + rcs := RepoCommitStats{ |
| 173 | + Owner: "publicorg", |
| 174 | + Name: "publicrepo", |
| 175 | + IsPrivate: false, |
| 176 | + Commits: 25, |
| 177 | + Additions: 1250, |
| 178 | + Deletions: 500, |
| 179 | + } |
| 180 | + |
| 181 | + if rcs.IsPrivate { |
| 182 | + t.Error("IsPrivate = true, want false") |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestMonthlyMapToSlice(t *testing.T) { |
| 187 | + tests := []struct { |
| 188 | + name string |
| 189 | + input map[string]*MonthlyCommitStats |
| 190 | + wantLen int |
| 191 | + wantKeys []string |
| 192 | + }{ |
| 193 | + { |
| 194 | + name: "empty map", |
| 195 | + input: map[string]*MonthlyCommitStats{}, |
| 196 | + wantLen: 0, |
| 197 | + wantKeys: []string{}, |
| 198 | + }, |
| 199 | + { |
| 200 | + name: "single month", |
| 201 | + input: map[string]*MonthlyCommitStats{ |
| 202 | + "2024-01": {Year: 2024, Month: time.January, Commits: 10, Additions: 100, Deletions: 50}, |
| 203 | + }, |
| 204 | + wantLen: 1, |
| 205 | + wantKeys: []string{"2024-01"}, |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "multiple months sorted", |
| 209 | + input: map[string]*MonthlyCommitStats{ |
| 210 | + "2024-03": {Year: 2024, Month: time.March, Commits: 30, Additions: 300, Deletions: 150}, |
| 211 | + "2024-01": {Year: 2024, Month: time.January, Commits: 10, Additions: 100, Deletions: 50}, |
| 212 | + "2024-02": {Year: 2024, Month: time.February, Commits: 20, Additions: 200, Deletions: 100}, |
| 213 | + }, |
| 214 | + wantLen: 3, |
| 215 | + wantKeys: []string{"2024-01", "2024-02", "2024-03"}, |
| 216 | + }, |
| 217 | + { |
| 218 | + name: "multiple years sorted", |
| 219 | + input: map[string]*MonthlyCommitStats{ |
| 220 | + "2025-01": {Year: 2025, Month: time.January, Commits: 50, Additions: 500, Deletions: 250}, |
| 221 | + "2024-01": {Year: 2024, Month: time.January, Commits: 10, Additions: 100, Deletions: 50}, |
| 222 | + "2024-12": {Year: 2024, Month: time.December, Commits: 40, Additions: 400, Deletions: 200}, |
| 223 | + }, |
| 224 | + wantLen: 3, |
| 225 | + wantKeys: []string{"2024-01", "2024-12", "2025-01"}, |
| 226 | + }, |
| 227 | + } |
| 228 | + |
| 229 | + for _, tt := range tests { |
| 230 | + t.Run(tt.name, func(t *testing.T) { |
| 231 | + result := monthlyMapToSlice(tt.input) |
| 232 | + |
| 233 | + if len(result) != tt.wantLen { |
| 234 | + t.Errorf("monthlyMapToSlice() len = %d, want %d", len(result), tt.wantLen) |
| 235 | + } |
| 236 | + |
| 237 | + for i, wantKey := range tt.wantKeys { |
| 238 | + if i >= len(result) { |
| 239 | + break |
| 240 | + } |
| 241 | + gotKey := result[i].YearMonth() |
| 242 | + if gotKey != wantKey { |
| 243 | + t.Errorf("monthlyMapToSlice()[%d].YearMonth() = %q, want %q", i, gotKey, wantKey) |
| 244 | + } |
| 245 | + } |
| 246 | + }) |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +func TestMonthlyMapToSlicePreservesData(t *testing.T) { |
| 251 | + input := map[string]*MonthlyCommitStats{ |
| 252 | + "2024-01": {Year: 2024, Month: time.January, Commits: 10, Additions: 100, Deletions: 50}, |
| 253 | + "2024-02": {Year: 2024, Month: time.February, Commits: 20, Additions: 200, Deletions: 100}, |
| 254 | + } |
| 255 | + |
| 256 | + result := monthlyMapToSlice(input) |
| 257 | + |
| 258 | + if len(result) != 2 { |
| 259 | + t.Fatalf("expected 2 results, got %d", len(result)) |
| 260 | + } |
| 261 | + |
| 262 | + // Results are sorted, so January should be first |
| 263 | + if result[0].Commits != 10 { |
| 264 | + t.Errorf("result[0].Commits = %d, want 10", result[0].Commits) |
| 265 | + } |
| 266 | + if result[0].Additions != 100 { |
| 267 | + t.Errorf("result[0].Additions = %d, want 100", result[0].Additions) |
| 268 | + } |
| 269 | + if result[0].Deletions != 50 { |
| 270 | + t.Errorf("result[0].Deletions = %d, want 50", result[0].Deletions) |
| 271 | + } |
| 272 | + |
| 273 | + if result[1].Commits != 20 { |
| 274 | + t.Errorf("result[1].Commits = %d, want 20", result[1].Commits) |
| 275 | + } |
| 276 | + if result[1].Additions != 200 { |
| 277 | + t.Errorf("result[1].Additions = %d, want 200", result[1].Additions) |
| 278 | + } |
| 279 | + if result[1].Deletions != 100 { |
| 280 | + t.Errorf("result[1].Deletions = %d, want 100", result[1].Deletions) |
| 281 | + } |
| 282 | +} |
| 283 | + |
| 284 | +func TestRepoInfoStruct(t *testing.T) { |
| 285 | + ri := repoInfo{ |
| 286 | + Owner: "testowner", |
| 287 | + Name: "testrepo", |
| 288 | + IsPrivate: true, |
| 289 | + } |
| 290 | + |
| 291 | + if ri.Owner != "testowner" { |
| 292 | + t.Errorf("Owner = %q, want %q", ri.Owner, "testowner") |
| 293 | + } |
| 294 | + if ri.Name != "testrepo" { |
| 295 | + t.Errorf("Name = %q, want %q", ri.Name, "testrepo") |
| 296 | + } |
| 297 | + if !ri.IsPrivate { |
| 298 | + t.Error("IsPrivate = false, want true") |
| 299 | + } |
| 300 | +} |
0 commit comments