-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_test.go
More file actions
309 lines (281 loc) · 7.23 KB
/
class_test.go
File metadata and controls
309 lines (281 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"os"
"testing"
"time"
)
// TestGetNextSunday tests the getNextSunday function
func TestGetNextSunday(t *testing.T) {
// This test is tricky because it depends on current time
// We'll test the logic by checking the result is a Sunday at 18:00 Berlin time
nextSunday := getNextSunday()
// Verify it's a Sunday
if nextSunday.Weekday() != time.Sunday {
t.Errorf("expected Sunday, got %s", nextSunday.Weekday())
}
// Verify it's at 18:00
if nextSunday.Hour() != 18 || nextSunday.Minute() != 0 {
t.Errorf("expected 18:00, got %02d:%02d", nextSunday.Hour(), nextSunday.Minute())
}
// Verify it's in the future (or today if Sunday before 18:00)
now := time.Now()
if nextSunday.Before(now) {
t.Error("next Sunday should be in the future")
}
}
// TestGetNextSundayEdgeCases tests edge cases for getNextSunday
func TestGetNextSundayEdgeCases(t *testing.T) {
tests := []struct {
name string
nowFunc func() time.Time
checkDay time.Weekday
}{
{
name: "Monday should return next Sunday",
nowFunc: func() time.Time { return time.Date(2024, 1, 8, 12, 0, 0, 0, time.UTC) }, // Monday
checkDay: time.Sunday,
},
{
name: "Saturday should return next Sunday",
nowFunc: func() time.Time { return time.Date(2024, 1, 13, 12, 0, 0, 0, time.UTC) }, // Saturday
checkDay: time.Sunday,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// This test verifies the function returns a valid result
result := getNextSunday()
if result.Weekday() != tt.checkDay {
t.Errorf("expected %s, got %s", tt.checkDay, result.Weekday())
}
})
}
}
// TestIsColumnExistsError tests the isColumnExistsError function
func TestIsColumnExistsError(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{
name: "nil error",
err: nil,
expected: false,
},
{
name: "duplicate column error",
err: &testError{message: "duplicate column name: conducted"},
expected: true,
},
{
name: "UNIQUE constraint error",
err: &testError{message: "UNIQUE constraint failed"},
expected: true,
},
{
name: "no column error",
err: &testError{message: "table classes has no column named conducted"},
expected: true,
},
{
name: "other error",
err: &testError{message: "some other error"},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isColumnExistsError(tt.err)
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
// testError is a simple error for testing
type testError struct {
message string
}
func (e *testError) Error() string {
return e.message
}
// TestFormatClassTimeWithTimezones tests the formatClassTimeWithTimezones function
func TestFormatClassTimeWithTimezones(t *testing.T) {
// Create a specific time for testing
classTime := time.Date(2024, 1, 14, 18, 0, 0, 0, time.UTC) // Sunday 18:00 UTC
result := formatClassTimeWithTimezones(classTime)
// Check that all expected timezones are present
timezones := []string{"Berlin", "Egypt", "India"}
for _, tz := range timezones {
if !containsString(result, tz) {
t.Errorf("expected timezone %s to be in result", tz)
}
}
// Check that it contains time format (should show 19:00 for Berlin which is UTC+1 in January)
if !containsString(result, "19:00") && !containsString(result, "18:00") {
t.Error("expected time to be in result (19:00 or 18:00 depending on timezone)")
}
// Check that it contains day of week
if !containsString(result, "Sunday") {
t.Error("expected Sunday to be in result")
}
}
// containsString checks if a string contains a substring
func containsString(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsSubstring(s, substr))
}
func containsSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
// TestSanitizeClassDescription tests the sanitizeClassDescription function
func TestSanitizeClassDescription(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "normal description",
input: "Introduction to Python",
expected: "Introduction to Python",
},
{
name: "trim whitespace",
input: " Introduction to Python ",
expected: "Introduction to Python",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "long description truncated",
input: string(make([]byte, 600)), // 600 characters
expected: string(make([]byte, 500)), // Should be truncated to 500
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := sanitizeClassDescription(tt.input)
if result != tt.expected {
t.Errorf("expected '%s', got '%s'", tt.expected, result)
}
})
}
}
// TestShouldSendLowRSVPWarning tests the shouldSendLowRSVPWarning function
func TestShouldSendLowRSVPWarning(t *testing.T) {
tests := []struct {
name string
rsvpCount int
expected bool
}{
{
name: "zero RSVPs",
rsvpCount: 0,
expected: true,
},
{
name: "one RSVP",
rsvpCount: 1,
expected: true,
},
{
name: "two RSVPs",
rsvpCount: 2,
expected: true,
},
{
name: "three RSVPs",
rsvpCount: 3,
expected: true,
},
{
name: "four RSVPs - minimum met",
rsvpCount: 4,
expected: false,
},
{
name: "five RSVPs",
rsvpCount: 5,
expected: false,
},
{
name: "ten RSVPs",
rsvpCount: 10,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := shouldSendLowRSVPWarning(tt.rsvpCount)
if result != tt.expected {
t.Errorf("shouldSendLowRSVPWarning(%d) = %v, want %v", tt.rsvpCount, result, tt.expected)
}
})
}
}
// TestGetRandomAnnouncementDelay tests the getRandomAnnouncementDelay function
func TestGetRandomAnnouncementDelay(t *testing.T) {
// Run multiple times to check it's within expected range
for i := 0; i < 100; i++ {
delay := getRandomAnnouncementDelay()
// Should be between 1 and 6 hours
minDelay := 1 * time.Hour
maxDelay := 6 * time.Hour
if delay < minDelay || delay > maxDelay {
t.Errorf("delay %v out of range [%v, %v]", delay, minDelay, maxDelay)
}
}
}
// TestIsOwner tests the isOwner function
func TestIsOwner(t *testing.T) {
// Save original environment
originalOwnerID := os.Getenv("OWNER_ID")
defer os.Setenv("OWNER_ID", originalOwnerID)
tests := []struct {
name string
ownerID string
userID int64
expected bool
}{
{
name: "owner matches",
ownerID: "12345",
userID: 12345,
expected: true,
},
{
name: "owner does not match",
ownerID: "12345",
userID: 67890,
expected: false,
},
{
name: "owner ID not set",
ownerID: "",
userID: 12345,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.ownerID == "" {
os.Unsetenv("OWNER_ID")
} else {
os.Setenv("OWNER_ID", tt.ownerID)
}
result := isOwner(tt.userID)
if result != tt.expected {
t.Errorf("isOwner(%d) = %v, want %v", tt.userID, result, tt.expected)
}
})
}
}