|
| 1 | +package graphql |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestMonthlyContributionYearMonth(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + mc MonthlyContribution |
| 12 | + want string |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "January 2024", |
| 16 | + mc: MonthlyContribution{Year: 2024, Month: time.January, Count: 10}, |
| 17 | + want: "2024-01", |
| 18 | + }, |
| 19 | + { |
| 20 | + name: "December 2023", |
| 21 | + mc: MonthlyContribution{Year: 2023, Month: time.December, Count: 5}, |
| 22 | + want: "2023-12", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "June 2025", |
| 26 | + mc: MonthlyContribution{Year: 2025, Month: time.June, Count: 0}, |
| 27 | + want: "2025-06", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "February leap year", |
| 31 | + mc: MonthlyContribution{Year: 2024, Month: time.February, Count: 29}, |
| 32 | + want: "2024-02", |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + got := tt.mc.YearMonth() |
| 39 | + if got != tt.want { |
| 40 | + t.Errorf("YearMonth() = %q, want %q", got, tt.want) |
| 41 | + } |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestContributionStatsStruct(t *testing.T) { |
| 47 | + from := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) |
| 48 | + to := time.Date(2024, 12, 31, 23, 59, 59, 0, time.UTC) |
| 49 | + |
| 50 | + stats := ContributionStats{ |
| 51 | + Username: "testuser", |
| 52 | + From: from, |
| 53 | + To: to, |
| 54 | + TotalCommitContributions: 100, |
| 55 | + TotalIssueContributions: 20, |
| 56 | + TotalPRContributions: 15, |
| 57 | + TotalPRReviewContributions: 30, |
| 58 | + TotalRepositoryContributions: 5, |
| 59 | + RestrictedContributions: 10, |
| 60 | + ContributionsByMonth: []MonthlyContribution{ |
| 61 | + {Year: 2024, Month: time.January, Count: 50}, |
| 62 | + {Year: 2024, Month: time.February, Count: 50}, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + if stats.Username != "testuser" { |
| 67 | + t.Errorf("Username = %q, want %q", stats.Username, "testuser") |
| 68 | + } |
| 69 | + if stats.TotalCommitContributions != 100 { |
| 70 | + t.Errorf("TotalCommitContributions = %d, want %d", stats.TotalCommitContributions, 100) |
| 71 | + } |
| 72 | + if stats.TotalIssueContributions != 20 { |
| 73 | + t.Errorf("TotalIssueContributions = %d, want %d", stats.TotalIssueContributions, 20) |
| 74 | + } |
| 75 | + if stats.TotalPRContributions != 15 { |
| 76 | + t.Errorf("TotalPRContributions = %d, want %d", stats.TotalPRContributions, 15) |
| 77 | + } |
| 78 | + if stats.TotalPRReviewContributions != 30 { |
| 79 | + t.Errorf("TotalPRReviewContributions = %d, want %d", stats.TotalPRReviewContributions, 30) |
| 80 | + } |
| 81 | + if stats.TotalRepositoryContributions != 5 { |
| 82 | + t.Errorf("TotalRepositoryContributions = %d, want %d", stats.TotalRepositoryContributions, 5) |
| 83 | + } |
| 84 | + if stats.RestrictedContributions != 10 { |
| 85 | + t.Errorf("RestrictedContributions = %d, want %d", stats.RestrictedContributions, 10) |
| 86 | + } |
| 87 | + if len(stats.ContributionsByMonth) != 2 { |
| 88 | + t.Errorf("ContributionsByMonth len = %d, want %d", len(stats.ContributionsByMonth), 2) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestMapToMonthlyContributions(t *testing.T) { |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + input map[string]int |
| 96 | + wantLen int |
| 97 | + wantKeys []string |
| 98 | + }{ |
| 99 | + { |
| 100 | + name: "empty map", |
| 101 | + input: map[string]int{}, |
| 102 | + wantLen: 0, |
| 103 | + wantKeys: []string{}, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "single month", |
| 107 | + input: map[string]int{ |
| 108 | + "2024-01": 10, |
| 109 | + }, |
| 110 | + wantLen: 1, |
| 111 | + wantKeys: []string{"2024-01"}, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "multiple months sorted", |
| 115 | + input: map[string]int{ |
| 116 | + "2024-03": 30, |
| 117 | + "2024-01": 10, |
| 118 | + "2024-02": 20, |
| 119 | + }, |
| 120 | + wantLen: 3, |
| 121 | + wantKeys: []string{"2024-01", "2024-02", "2024-03"}, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "multiple years sorted", |
| 125 | + input: map[string]int{ |
| 126 | + "2025-01": 50, |
| 127 | + "2024-01": 10, |
| 128 | + "2024-12": 40, |
| 129 | + }, |
| 130 | + wantLen: 3, |
| 131 | + wantKeys: []string{"2024-01", "2024-12", "2025-01"}, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + for _, tt := range tests { |
| 136 | + t.Run(tt.name, func(t *testing.T) { |
| 137 | + result := mapToMonthlyContributions(tt.input) |
| 138 | + |
| 139 | + if len(result) != tt.wantLen { |
| 140 | + t.Errorf("mapToMonthlyContributions() len = %d, want %d", len(result), tt.wantLen) |
| 141 | + } |
| 142 | + |
| 143 | + for i, wantKey := range tt.wantKeys { |
| 144 | + if i >= len(result) { |
| 145 | + break |
| 146 | + } |
| 147 | + gotKey := result[i].YearMonth() |
| 148 | + if gotKey != wantKey { |
| 149 | + t.Errorf("mapToMonthlyContributions()[%d].YearMonth() = %q, want %q", i, gotKey, wantKey) |
| 150 | + } |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestMapToMonthlyContributionsCount(t *testing.T) { |
| 157 | + input := map[string]int{ |
| 158 | + "2024-01": 10, |
| 159 | + "2024-02": 20, |
| 160 | + } |
| 161 | + |
| 162 | + result := mapToMonthlyContributions(input) |
| 163 | + |
| 164 | + if len(result) != 2 { |
| 165 | + t.Fatalf("expected 2 results, got %d", len(result)) |
| 166 | + } |
| 167 | + |
| 168 | + // Results are sorted, so January should be first |
| 169 | + if result[0].Count != 10 { |
| 170 | + t.Errorf("result[0].Count = %d, want 10", result[0].Count) |
| 171 | + } |
| 172 | + if result[1].Count != 20 { |
| 173 | + t.Errorf("result[1].Count = %d, want 20", result[1].Count) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func TestMapToMonthlyContributionsInvalidDate(t *testing.T) { |
| 178 | + // Invalid date format should be skipped |
| 179 | + input := map[string]int{ |
| 180 | + "invalid": 10, |
| 181 | + "2024-01": 20, |
| 182 | + } |
| 183 | + |
| 184 | + result := mapToMonthlyContributions(input) |
| 185 | + |
| 186 | + if len(result) != 1 { |
| 187 | + t.Errorf("mapToMonthlyContributions() should skip invalid dates, got len = %d", len(result)) |
| 188 | + } |
| 189 | + if result[0].YearMonth() != "2024-01" { |
| 190 | + t.Errorf("result[0].YearMonth() = %q, want %q", result[0].YearMonth(), "2024-01") |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestMonthlyContributionStruct(t *testing.T) { |
| 195 | + mc := MonthlyContribution{ |
| 196 | + Year: 2024, |
| 197 | + Month: time.March, |
| 198 | + Count: 42, |
| 199 | + } |
| 200 | + |
| 201 | + if mc.Year != 2024 { |
| 202 | + t.Errorf("Year = %d, want %d", mc.Year, 2024) |
| 203 | + } |
| 204 | + if mc.Month != time.March { |
| 205 | + t.Errorf("Month = %v, want %v", mc.Month, time.March) |
| 206 | + } |
| 207 | + if mc.Count != 42 { |
| 208 | + t.Errorf("Count = %d, want %d", mc.Count, 42) |
| 209 | + } |
| 210 | +} |
0 commit comments