/
schedule_spec.rb
411 lines (346 loc) · 14.5 KB
/
schedule_spec.rb
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
require File.dirname(__FILE__) + '/../spec_helper'
describe IceCube::Schedule do
include IceCube
describe :conflicts_with? do
it 'should raise an error if both are not terminating' do
schedules = 2.times.map do
schedule = IceCube::Schedule.new(Time.now)
schedule.rrule IceCube::Rule.daily
schedule
end
lambda do
schedules.first.conflicts_with?(schedules.last)
end.should raise_error ArgumentError
end
it 'should not raise error if both are non-terminating closing time present' do
schedule1 = IceCube::Schedule.new Time.now
schedule1.rrule IceCube::Rule.weekly
schedule2 = IceCube::Schedule.new Time.now
schedule2.rrule IceCube::Rule.weekly
lambda do
schedule1.conflicts_with?(schedule2, Time.now + IceCube::ONE_DAY)
end.should_not raise_error
end
it 'should not raise an error if one is non-terminating' do
schedule1 = IceCube::Schedule.new Time.now
schedule1.rrule IceCube::Rule.weekly
schedule2 = IceCube::Schedule.new Time.now
schedule2.rrule IceCube::Rule.weekly.until(Time.now)
lambda do
schedule1.conflicts_with?(schedule2)
end.should_not raise_error
end
it 'should not raise an error if the other is non-terminating' do
schedule1 = IceCube::Schedule.new Time.now
schedule1.rrule IceCube::Rule.weekly.until(Time.now)
schedule2 = IceCube::Schedule.new Time.now
schedule2.rrule IceCube::Rule.weekly
lambda do
schedule1.conflicts_with?(schedule2)
end.should_not raise_error
end
it 'should return true if conflict is present' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time)
schedule1.rrule IceCube::Rule.daily
schedule2 = IceCube::Schedule.new(start_time)
schedule2.rrule IceCube::Rule.daily
conflict = schedule1.conflicts_with?(schedule2, start_time + IceCube::ONE_DAY)
conflict.should be_true
end
it 'should return false if conflict is not present' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time)
schedule1.rrule IceCube::Rule.weekly.day(:tuesday)
schedule2 = IceCube::Schedule.new(start_time)
schedule2.rrule IceCube::Rule.weekly.day(:monday)
conflict = schedule1.conflicts_with?(schedule2, start_time + IceCube::ONE_DAY)
conflict.should be_false
end
it 'should return true if conflict is present based on duration' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_DAY + 1)
schedule1.rrule IceCube::Rule.weekly.day(:monday)
schedule2 = IceCube::Schedule.new(start_time)
schedule2.rrule IceCube::Rule.weekly.day(:tuesday)
conflict = schedule1.conflicts_with?(schedule2, start_time + IceCube::ONE_WEEK)
conflict.should be_true
end
it 'should return true if conflict is present based on duration - other way' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time)
schedule1.rrule IceCube::Rule.weekly.day(:tuesday)
schedule2 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_DAY + 1)
schedule2.rrule IceCube::Rule.weekly.day(:monday)
conflict = schedule1.conflicts_with?(schedule2, start_time + IceCube::ONE_WEEK)
conflict.should be_true
end
it 'should return false if conflict is past closing_time' do
start_time = Time.local(2011, 1, 1, 12) # Sunday
schedule1 = IceCube::Schedule.new(start_time)
schedule1.rrule IceCube::Rule.weekly.day(:friday)
schedule2 = IceCube::Schedule.new(start_time)
schedule2.rrule IceCube::Rule.weekly.day(:friday)
schedule2.conflicts_with?(schedule1, start_time + IceCube::ONE_WEEK).
should be_true
schedule2.conflicts_with?(schedule1, start_time + IceCube::ONE_DAY).
should be_false
end
it 'should return false if conflict is not present based on duration' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule1.rrule IceCube::Rule.weekly.day(:monday)
schedule2 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule2.rrule IceCube::Rule.weekly.day(:tuesday)
conflict = schedule1.conflicts_with?(schedule2, start_time + IceCube::ONE_WEEK)
conflict.should be_false
end
it 'should return false if conflict is not present on same day based on duration' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule1.rrule IceCube::Rule.daily
schedule2 = IceCube::Schedule.new(start_time + 1.hour, :duration => IceCube::ONE_HOUR)
schedule2.rrule IceCube::Rule.daily
conflict = schedule1.conflicts_with?(schedule2, start_time + IceCube::ONE_WEEK)
conflict.should be_false
end
it 'should return true if conflict is present on same day based on duration' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule1.rrule IceCube::Rule.daily
schedule2 = IceCube::Schedule.new(start_time + 10.minutes, :duration => IceCube::ONE_HOUR)
schedule2.rrule IceCube::Rule.daily
conflict = schedule1.conflicts_with?(schedule2, start_time + IceCube::ONE_WEEK)
conflict.should be_true
end
it 'should return true if conflict is present and no recurrence' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule1.add_recurrence_time(start_time)
schedule2 = IceCube::Schedule.new(start_time + 10.minutes, :duration => IceCube::ONE_HOUR)
schedule2.add_recurrence_time(start_time + 10.minutes)
conflict = schedule1.conflicts_with?(schedule2)
conflict.should be_true
conflict = schedule2.conflicts_with?(schedule1)
conflict.should be_true
end
it 'should return false if conflict is not present and no recurrence' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule1.add_recurrence_time(start_time)
schedule2 = IceCube::Schedule.new(start_time + IceCube::ONE_HOUR, :duration => IceCube::ONE_HOUR)
schedule2.add_recurrence_time(start_time + IceCube::ONE_HOUR)
conflict = schedule1.conflicts_with?(schedule2)
conflict.should be_false
conflict = schedule2.conflicts_with?(schedule1)
conflict.should be_false
end
it 'should return false if conflict is not present and single recurrence' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule1.add_recurrence_time(start_time)
schedule2 = IceCube::Schedule.new(start_time + IceCube::ONE_HOUR, :duration => IceCube::ONE_HOUR)
schedule2.rrule IceCube::Rule.daily
conflict = schedule1.conflicts_with?(schedule2)
conflict.should be_false
conflict = schedule2.conflicts_with?(schedule1)
conflict.should be_false
end
it 'should return true if conflict is present and single recurrence' do
start_time = Time.now
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule1.add_recurrence_time(start_time)
schedule2 = IceCube::Schedule.new(start_time + 10.minutes, :duration => IceCube::ONE_HOUR)
schedule2.rrule IceCube::Rule.daily
conflict = schedule1.conflicts_with?(schedule2)
conflict.should be_true
conflict = schedule2.conflicts_with?(schedule1)
conflict.should be_true
end
it 'should return false if conflict is not present and single recurrence and time originally specified as DateTime' do
date = DateTime.new(2020,9,21,11,30,0)
start_time = date.to_time
schedule1 = IceCube::Schedule.new(start_time, :duration => IceCube::ONE_HOUR)
schedule1.add_recurrence_time(start_time)
schedule2 = IceCube::Schedule.new(start_time + IceCube::ONE_HOUR, :duration => IceCube::ONE_HOUR)
schedule2.add_recurrence_time(start_time + IceCube::ONE_HOUR)
conflict = schedule1.conflicts_with?(schedule2)
conflict.should be_false
conflict = schedule2.conflicts_with?(schedule1)
conflict.should be_false
end
end
describe :each do
it 'should be able to yield occurrences for a schedule' do
schedule = IceCube::Schedule.new
schedule.add_recurrence_rule IceCube::Rule.daily
i = 0
answers = []
schedule.each_occurrence do |time|
answers << time
i += 1
break if i > 9
end
answers.should == schedule.first(10)
end
it 'should return self' do
schedule = IceCube::Schedule.new
schedule.each_occurrence { |s| break }.should == schedule
end
it 'should stop itself when hitting the end of a schedule' do
time = Time.now + 24 * ONE_DAY
schedule = IceCube::Schedule.new
schedule.add_recurrence_time time
answers = []
schedule.each_occurrence { |t| answers << t }
answers.should == [time]
end
end
describe :all_occurrences do
it 'should stop automatically with just a date' do
time = Time.now + 24 * ONE_DAY
schedule = IceCube::Schedule.new
schedule.add_recurrence_time time
schedule.all_occurrences.should == [time]
end
it 'should return an error if there is nothing to stop it' do
schedule = IceCube::Schedule.new
schedule.rrule IceCube::Rule.daily
lambda do
schedule.all_occurrences
end.should raise_error ArgumentError
end
end
describe :next_occurrences do
it 'should be able to calculate next occurrences ignoring exclude_dates' do
start_time = Time.now
schedule = Schedule.new start_time
schedule.rrule Rule.daily(1)
schedule.exdate start_time + ONE_DAY
occurrences = schedule.next_occurrences(2, start_time) # 3 occurrences in the next year
occurrences.should == [
start_time + ONE_DAY * 2,
start_time + ONE_DAY * 3
]
end
end
describe :next_occurrence do
it 'should be able to calculate the next occurrence past an exdate' do
start_time = Time.now
schedule = Schedule.new start_time
schedule.rrule Rule.daily(1)
schedule.exdate start_time + ONE_DAY
occurrence = schedule.next_occurrence(start_time) # 3 occurrences in the next year
occurrence.should == start_time + ONE_DAY * 2
end
end
describe :start_date= do
it 'should modify start date in rrule_occurrence_heads when changed' do
schedule = Schedule.new(Time.now - 1000)
schedule.rrule Rule.daily
schedule.start_date = (start_date = Time.now)
(Time.now - schedule.first).should be < 100
end
end
describe :recurrence_rules do
it 'should not include rules for single occurrences' do
schedule = IceCube::Schedule.new Time.now
schedule.add_recurrence_time Time.now
schedule.rrules.should be_empty
end
end
describe :remove_recurrence_rule do
it 'should be able to one rule based on the comparator' do
schedule = IceCube::Schedule.new Time.now
schedule.rrule IceCube::Rule.daily
schedule.rrule IceCube::Rule.daily(2)
schedule.remove_recurrence_rule schedule.rrules.first
schedule.rrules.count.should == 1
end
it 'should be able to remove multiple rules based on the comparator' do
schedule = IceCube::Schedule.new Time.now
schedule.rrule IceCube::Rule.daily
schedule.rrule IceCube::Rule.daily
schedule.remove_recurrence_rule schedule.rrules.first
schedule.rrules.should be_empty
end
it 'should return the rule that was removed' do
schedule = IceCube::Schedule.new Time.now
rule = IceCube::Rule.daily
schedule.rrule rule
rule2 = schedule.remove_recurrence_rule rule
[rule].should == rule2
end
it 'should return [] if nothing was removed' do
schedule = IceCube::Schedule.new Time.now
rule = IceCube::Rule.daily
schedule.remove_recurrence_rule(rule).should == []
end
end
describe :remove_exception_rule do
it 'should be able to one rule based on the comparator' do
schedule = IceCube::Schedule.new Time.now
schedule.exrule IceCube::Rule.daily
schedule.exrule IceCube::Rule.daily(2)
schedule.remove_exception_rule schedule.exrules.first
schedule.exrules.count.should == 1
end
it 'should be able to remove multiple rules based on the comparator' do
schedule = IceCube::Schedule.new Time.now
schedule.exrule IceCube::Rule.daily
schedule.exrule IceCube::Rule.daily
schedule.remove_exception_rule schedule.exrules.first
schedule.exrules.should be_empty
end
it 'should return the rule that was removed' do
schedule = IceCube::Schedule.new Time.now
rule = IceCube::Rule.daily
schedule.exrule rule
rule2 = schedule.remove_exception_rule rule
[rule].should == rule2
end
it 'should return [] if nothing was removed' do
schedule = IceCube::Schedule.new Time.now
rule = IceCube::Rule.daily
schedule.remove_exception_rule(rule).should == []
end
end
describe :remove_recurrence_date do
it 'should be able to remove a recurrence date from a schedule' do
time = Time.now
schedule = IceCube::Schedule.new(time)
schedule.add_recurrence_date time
schedule.remove_recurrence_date time
schedule.recurrence_dates.should be_empty
end
it 'should return the date that was removed' do
schedule = IceCube::Schedule.new Time.now
time = Time.now
schedule.rdate time
schedule.remove_rdate(time).should == time
end
it 'should return nil if the date was not in the schedule' do
schedule = IceCube::Schedule.new Time.now
schedule.remove_recurrence_date(Time.now).should be_nil
end
end
describe :remove_exception_date do
it 'should be able to remove a exception date from a schedule' do
time = Time.now
schedule = IceCube::Schedule.new(time)
schedule.exdate time
schedule.remove_exception_date time
schedule.exception_dates.should be_empty
end
it 'should return the date that was removed' do
schedule = IceCube::Schedule.new Time.now
time = Time.now
schedule.exdate time
schedule.remove_exdate(time).should == time
end
it 'should return nil if the date was not in the schedule' do
schedule = IceCube::Schedule.new Time.now
schedule.remove_exception_date(Time.now).should be_nil
end
end
end