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
|
package config
import (
"fmt"
"testing"
"time"
)
func TestIsPaused(t *testing.T) {
tests := []struct {
name string
pauseStart string
pauseEnd string
testTime time.Time
expected bool
expectError bool
}{
{
name: "No pause dates configured",
pauseStart: "",
pauseEnd: "",
testTime: time.Date(2024, 8, 15, 12, 0, 0, 0, time.UTC),
expected: false,
expectError: false,
},
{
name: "Currently paused - middle of pause period",
pauseStart: "2024-07-01",
pauseEnd: "2024-09-18",
testTime: time.Date(2024, 8, 15, 12, 0, 0, 0, time.UTC),
expected: true,
expectError: false,
},
{
name: "Not paused - before pause period",
pauseStart: "2024-07-01",
pauseEnd: "2024-09-18",
testTime: time.Date(2024, 6, 30, 23, 59, 59, 0, time.UTC),
expected: false,
expectError: false,
},
{
name: "Not paused - after pause period",
pauseStart: "2024-07-01",
pauseEnd: "2024-09-18",
testTime: time.Date(2024, 9, 19, 0, 0, 1, 0, time.UTC),
expected: false,
expectError: false,
},
{
name: "Paused - exactly on start date",
pauseStart: "2024-07-01",
pauseEnd: "2024-09-18",
testTime: time.Date(2024, 7, 1, 0, 0, 0, 0, time.UTC),
expected: true,
expectError: false,
},
{
name: "Paused - exactly on end date",
pauseStart: "2024-07-01",
pauseEnd: "2024-09-18",
testTime: time.Date(2024, 9, 18, 23, 59, 59, 0, time.UTC),
expected: true,
expectError: false,
},
{
name: "Single day pause",
pauseStart: "2024-08-15",
pauseEnd: "2024-08-15",
testTime: time.Date(2024, 8, 15, 12, 0, 0, 0, time.UTC),
expected: true,
expectError: false,
},
{
name: "Invalid start date format",
pauseStart: "2024/07/01",
pauseEnd: "2024-09-18",
testTime: time.Date(2024, 8, 15, 12, 0, 0, 0, time.UTC),
expected: false,
expectError: true,
},
{
name: "Invalid end date format",
pauseStart: "2024-07-01",
pauseEnd: "2024/09/18",
testTime: time.Date(2024, 8, 15, 12, 0, 0, 0, time.UTC),
expected: false,
expectError: true,
},
{
name: "Empty start date only",
pauseStart: "",
pauseEnd: "2024-09-18",
testTime: time.Date(2024, 8, 15, 12, 0, 0, 0, time.UTC),
expected: false,
expectError: false,
},
{
name: "Empty end date only",
pauseStart: "2024-07-01",
pauseEnd: "",
testTime: time.Date(2024, 8, 15, 12, 0, 0, 0, time.UTC),
expected: false,
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := Config{
PauseStart: tt.pauseStart,
PauseEnd: tt.pauseEnd,
}
// Mock current time by temporarily replacing time.Now in the method
// Since we can't easily mock time.Now, we'll test the logic manually
paused, err := isPausedAtTime(config, tt.testTime)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if paused != tt.expected {
t.Errorf("Expected paused=%v, got paused=%v", tt.expected, paused)
}
})
}
}
// Helper function to test pause logic with a specific time
func isPausedAtTime(c Config, testTime time.Time) (bool, error) {
if c.PauseStart == "" || c.PauseEnd == "" {
return false, nil
}
startDate, err := time.Parse("2006-01-02", c.PauseStart)
if err != nil {
return false, err
}
endDate, err := time.Parse("2006-01-02", c.PauseEnd)
if err != nil {
return false, err
}
// Set time to start of day for start date and end of day for end date
startDate = time.Date(startDate.Year(), startDate.Month(), startDate.Day(), 0, 0, 0, 0, testTime.Location())
endDate = time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 23, 59, 59, 999999999, testTime.Location())
return (testTime.After(startDate) || testTime.Equal(startDate)) && (testTime.Before(endDate) || testTime.Equal(endDate)), nil
}
func TestIsPausedCurrentTime(t *testing.T) {
// Test with actual current time using the real IsPaused method
currentYear := time.Now().Year()
config := Config{
PauseStart: fmt.Sprintf("%d-01-01", currentYear),
PauseEnd: fmt.Sprintf("%d-12-31", currentYear),
}
paused, err := config.IsPaused()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
// Since we're in the current year, this should be paused
if !paused {
t.Errorf("Expected to be paused in %d, but got false", currentYear)
}
// Test with dates in the past
config.PauseStart = "2020-01-01"
config.PauseEnd = "2020-12-31"
paused, err = config.IsPaused()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
// Since we're past 2020, this should not be paused
if paused {
t.Errorf("Expected not to be paused for past dates, but got true")
}
}
|