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
|
require File.dirname(__FILE__) + '/../spec_helper'
module IceCube
describe Schedule, 'to_yaml' do
before do
require 'active_support/time'
Time.zone = 'Eastern Time (US & Canada)'
end
[:yearly, :monthly, :weekly, :daily, :hourly, :minutely, :secondly].each do |type|
it "should make a #{type} round trip with to_yaml [#47]" do
schedule = Schedule.new(Time.zone.now)
schedule.add_recurrence_rule Rule.send(type, 3)
expect(Schedule.from_yaml(schedule.to_yaml).first(3).inspect).to eq(schedule.first(3).inspect)
end
end
it 'should be able to let rules take round trips to yaml' do
schedule = Schedule.new
schedule.add_recurrence_rule Rule.monthly
schedule = Schedule.from_yaml schedule.to_yaml
rule = schedule.rrules.first
rule.is_a?(MonthlyRule)
end
it 'should respond to .to_yaml' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.daily.until(Time.now)
#check assumption
expect(schedule).to respond_to('to_yaml')
end
it 'should be able to make a round-trip to YAML' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.daily.until(Time.now + 10)
result1 = schedule.all_occurrences
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
result2 = schedule2.all_occurrences
# compare without usecs
expect(result1.map { |r| r.to_s }).to eq(result2.map { |r| r.to_s })
end
it 'should be able to make a round-trip to YAML with .day' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.daily.day(:monday, :wednesday)
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
# compare without usecs
expect(schedule.first(10).map { |r| r.to_s }).to eq(schedule2.first(10).map { |r| r.to_s })
end
it 'should be able to make a round-trip to YAML with .day_of_month' do
schedule = Schedule.new(Time.zone.now)
schedule.add_recurrence_rule Rule.monthly.day_of_month(10, 20)
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
# compare without usecs
expect(schedule.first(10).map { |r| r.to_s }).to eq(schedule2.first(10).map { |r| r.to_s })
end
it 'should be able to make a round-trip to YAML with .day_of_week' do
schedule = Schedule.new(Time.zone.now)
schedule.add_recurrence_rule Rule.weekly.day_of_week(:monday => [1, -2])
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
# compare without usecs
expect(schedule.first(10).map { |r| r.to_s }).to eq(schedule2.first(10).map { |r| r.to_s })
end
it 'should be able to make a round-trip to YAML with .day_of_year' do
schedule = Schedule.new(Time.zone.now)
schedule.add_recurrence_rule Rule.yearly.day_of_year(100, 200)
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
# compare without usecs
expect(schedule.first(10).map { |r| r.to_s }).to eq(schedule2.first(10).map { |r| r.to_s })
end
it 'should be able to make a round-trip to YAML with .hour_of_day' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.daily.hour_of_day(1, 2)
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
# compare without usecs
expect(schedule.first(10).map { |r| r.to_s }).to eq(schedule2.first(10).map { |r| r.to_s })
end
it 'should be able to make a round-trip to YAML with .minute_of_hour' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.daily.minute_of_hour(0, 30)
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
# compare without usecs
expect(schedule.first(10).map { |r| r.to_s }).to eq(schedule2.first(10).map { |r| r.to_s })
end
it 'should be able to make a round-trip to YAML with .month_of_year' do
schedule = Schedule.new(Time.zone.now)
schedule.add_recurrence_rule Rule.yearly.month_of_year(:april, :may)
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
# compare without usecs
expect(schedule.first(10).map { |r| r.to_s }).to eq(schedule2.first(10).map { |r| r.to_s })
end
it 'should be able to make a round-trip to YAML with .second_of_minute' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.daily.second_of_minute(1, 2)
yaml_string = schedule.to_yaml
schedule2 = Schedule.from_yaml(yaml_string)
# compare without usecs
expect(schedule.first(10).map { |r| r.to_s }).to eq(schedule2.first(10).map { |r| r.to_s })
end
it 'should have a to_yaml representation of a rule that does not contain ruby objects' do
rule = Rule.daily.day_of_week(:monday => [1, -1]).month_of_year(:april)
expect(rule.to_yaml.include?('object')).to be_falsey
end
it 'should have a to_yaml representation of a schedule that does not contain ruby objects' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.daily.day_of_week(:monday => [1, -1]).month_of_year(:april)
expect(schedule.to_yaml.include?('object')).to be_falsey
end
# This test will fail when not run in Eastern Time
# This is a bug because to_datetime will always convert to system local time
it 'should be able to roll forward times and get back times in an array - TimeWithZone', :requires_active_support => true do
Time.zone = "Eastern Time (US & Canada)"
start_time = Time.zone.local(2011, 11, 5, 12, 0, 0)
schedule = Schedule.new(start_time)
schedule = Schedule.from_yaml(schedule.to_yaml) # round trip
ice_cube_start_time = schedule.start_time
expect(ice_cube_start_time).to eq(start_time)
expect(ice_cube_start_time.utc_offset).to eq(start_time.utc_offset)
end
it 'should be able to roll forward times and get back times in an array - Time' do
start_time = Time.now
schedule = Schedule.new(start_time)
schedule = Schedule.from_yaml(schedule.to_yaml) # round trip
ice_cube_start_time = schedule.start_time
expect(ice_cube_start_time.to_s).to eq(start_time.to_s)
expect(ice_cube_start_time.class).to eq(Time)
expect(ice_cube_start_time.utc_offset).to eq(start_time.utc_offset)
end
it 'should be able to go back and forth to yaml and then call occurrences' do
start_time = Time.local(2011, 5, 10, 12, 0, 0)
schedule1 = Schedule.new(start_time)
schedule1.add_recurrence_time start_time
schedule2 = Schedule.from_yaml(schedule1.to_yaml) # round trip
end_time = Time.now + ONE_DAY
expect(schedule1.occurrences(end_time)).to eq(schedule2.occurrences(end_time))
end
it 'should be able to make a round trip with an exception time' do
schedule = Schedule.new
schedule.add_exception_time(time = Time.now)
schedule = Schedule.from_yaml schedule.to_yaml
expect(schedule.extimes.map(&:to_s)).to eq([time.to_s])
end
it 'crazy shit' do
start_time = Time.zone.now
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule Rule.weekly.day(:wednesday)
schedule.add_recurrence_time start_time
schedule = Schedule.from_hash(schedule.to_hash)
schedule = Schedule.from_yaml(schedule.to_yaml)
schedule.occurrences(start_time + ONE_DAY * 14)
end
it 'should be able to make a round trip to hash with a duration' do
schedule = Schedule.new Time.now, :duration => 3600
expect(Schedule.from_hash(schedule.to_hash).duration).to eq(3600)
end
it 'should be able to be serialized to yaml as part of a hash' do
schedule = Schedule.new Time.now
hash = { :schedule => schedule }
expect do
hash.to_yaml
end.not_to raise_error
end
it 'should be able to roll forward and back in time' do
schedule = Schedule.new(Time.now)
rt_schedule = Schedule.from_yaml(schedule.to_yaml)
expect(rt_schedule.start_time.utc_offset).to eq(schedule.start_time.utc_offset)
end
it 'should be backward compatible with old yaml Time format', expect_warnings: true do
yaml = "---\n:end_time:\n:rdates: []\n:rrules: []\n:duration:\n:exdates: []\n:start_time: 2010-10-18T14:35:47-07:00"
schedule = Schedule.from_yaml(yaml)
expect(schedule.start_time).to be_a(Time)
end
it 'should work to_yaml with non-TimeWithZone' do
schedule = Schedule.new(Time.now)
expect(schedule.to_yaml.length).to be < 200
end
it 'should work with occurs_on and TimeWithZone' do
pacific_time = 'Pacific Time (US & Canada)'
Time.zone = pacific_time
schedule = Schedule.new(Time.zone.now)
schedule.add_recurrence_rule Rule.weekly
expect(schedule.occurs_on?(schedule.start_time.to_date + 6)).to be_falsey
expect(schedule.occurs_on?(schedule.start_time.to_date + 7)).to be_truthy
expect(schedule.occurs_on?(schedule.start_time.to_date + 8)).to be_falsey
end
it 'should work with occurs_on and TimeWithZone' do
start_time = Time.zone.local(2012, 7, 15, 12, 0, 0)
pacific_time = 'Pacific Time (US & Canada)'
Time.zone = pacific_time
schedule = Schedule.new(start_time)
schedule.add_recurrence_time start_time + 7 * ONE_DAY
expect(schedule.occurs_on?(schedule.start_time.to_date + 6)).to be_falsey
expect(schedule.occurs_on?(schedule.start_time.to_date + 7)).to be_truthy
expect(schedule.occurs_on?(schedule.start_time.to_date + 8)).to be_falsey
end
it 'should crazy patch' do
Time.zone = 'Pacific Time (US & Canada)'
day = Time.zone.parse('21 Oct 2010 02:00:00')
schedule = Schedule.new(day)
schedule.add_recurrence_time(day)
expect(schedule.occurs_on?(Date.new(2010, 10, 20))).to be_falsey
expect(schedule.occurs_on?(Date.new(2010, 10, 21))).to be_truthy
expect(schedule.occurs_on?(Date.new(2010, 10, 22))).to be_falsey
end
it 'should be able to bring a Rule to_yaml and back with a timezone' do
Time.zone = 'Pacific Time (US & Canada)'
time = Time.now
offset = time.utc_offset
rule = Rule.daily.until(time)
rule = Rule.from_yaml(rule.to_yaml)
expect(rule.until_time.utc_offset).to eq(offset)
end
it 'should be able to bring a Rule to_yaml and back with a count' do
rule = Rule.daily.count(5)
rule = Rule.from_yaml rule.to_yaml
expect(rule.occurrence_count).to eq(5)
end
it 'should be able to bring a Rule to_yaml and back with an until Date' do
rule = Rule.daily.until(Date.today >> 1)
rule = Rule.from_yaml rule.to_yaml
expect(rule.until_time).to eq(Date.today >> 1)
end
it 'should be able to bring a Rule to_yaml and back with an until Time' do
t1 = Time.now + ONE_HOUR
rule = Rule.daily.until(t1)
rule = Rule.from_yaml rule.to_yaml
expect(rule.until_time).to eq(t1)
end
it 'should be able to bring a Rule to_yaml and back with an until TimeWithZone' do
Time.zone = "America/Vancouver"
t1 = Time.zone.now + ONE_HOUR
rule = Rule.daily.until(t1)
rule = Rule.from_yaml rule.to_yaml
expect(rule.until_time).to eq(t1)
end
it 'should be able to bring a Rule to_yaml and back with an undefined week start' do
rule = Rule.weekly(2)
rule = Rule.from_yaml rule.to_yaml
expect(rule.week_start).to eq(:sunday)
end
it 'should be able to bring a Rule to_yaml and back with a week start defined' do
rule = Rule.weekly.interval(2, :monday)
rule = Rule.from_yaml rule.to_yaml
expect(rule.week_start).to eq(:monday)
end
it 'should be able to bring in a schedule with a rule from hash with symbols or strings' do
time = Time.zone.now
symbol_data = { :start_time => time, :rrules => [ { :validations => { :day => [1] }, :rule_type => "IceCube::DailyRule", :interval => 1 } ], :rtimes => [], :extimes => [] }
string_data = { 'start_time' => time, 'rrules' => [ { 'validations' => { 'day' => [1] }, 'rule_type' => "IceCube::DailyRule", 'interval' => 1 } ], 'rtimes' => [], 'extimes' => [] }
symbol_yaml = Schedule.from_hash(symbol_data).to_yaml
string_yaml = Schedule.from_hash(string_data).to_yaml
expect(YAML.safe_load(symbol_yaml, permitted_classes: [Symbol, Time]))
.to eq(YAML.safe_load(string_yaml, permitted_classes: [Symbol, Time]))
end
it 'should raise an ArgumentError when trying to deserialize an invalid rule type' do
data = {:rule_type => 'IceCube::FakeRule', :interval => 1}
expect { Rule.from_hash(data) }.to raise_error(ArgumentError, 'Invalid rule frequency type: Fake')
end
it 'should raise an ArgumentError when trying to deserialize an invalid validation' do
data = {:validations => {:fake => []}, :rule_type => 'IceCube::DailyRule', :interval => 1}
expect { Rule.from_hash(data) }.to raise_error(ArgumentError, 'Invalid rule validation type: fake')
end
end
end
|