File: active_support_spec.rb

package info (click to toggle)
ruby-ice-cube 0.16.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 800 kB
  • sloc: ruby: 7,823; makefile: 6
file content (175 lines) | stat: -rw-r--r-- 7,477 bytes parent folder | download
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
require File.dirname(__FILE__) + '/../spec_helper'
require 'active_support'
require 'active_support/time'
require 'active_support/version'
require 'tzinfo' if ActiveSupport::VERSION::MAJOR == 3

module IceCube
  describe Schedule, 'using ActiveSupport' do

    before(:all) { Time.zone = 'Eastern Time (US & Canada)' }

    around(:each) do |example|
      Time.zone = 'America/Anchorage'
      orig_tz, ENV['TZ'] = ENV['TZ'], 'Pacific/Auckland'
      example.run
      ENV['TZ'] = orig_tz
    end

    it 'works with a single recurrence time starting from a TimeWithZone' do
      schedule = Schedule.new(t0 = Time.zone.parse("2010-02-05 05:00:00"))
      schedule.add_recurrence_time t0
      expect(schedule.all_occurrences).to eq([t0])
    end

    it 'works with a monthly recurrence rule starting from a TimeWithZone' do
      schedule = Schedule.new(Time.zone.parse("2010-02-05 05:00:00"))
      schedule.add_recurrence_rule Rule.monthly
      expect(schedule.first(10)).to eq([
        Time.zone.parse("2010-02-05 05:00"), Time.zone.parse("2010-03-05 05:00"),
        Time.zone.parse("2010-04-05 05:00"), Time.zone.parse("2010-05-05 05:00"),
        Time.zone.parse("2010-06-05 05:00"), Time.zone.parse("2010-07-05 05:00"),
        Time.zone.parse("2010-08-05 05:00"), Time.zone.parse("2010-09-05 05:00"),
        Time.zone.parse("2010-10-05 05:00"), Time.zone.parse("2010-11-05 05:00")
      ])
    end

    it 'works with a monthly schedule converting to UTC across DST' do
      Time.zone = 'Eastern Time (US & Canada)'
      schedule = Schedule.new(Time.zone.parse("2009-10-28 19:30:00"))
      schedule.add_recurrence_rule Rule.monthly
      expect(schedule.first(7).map { |d| d.getutc }).to eq([
        Time.utc(2009, 10, 28, 23, 30, 0), Time.utc(2009, 11, 29,  0, 30, 0),
        Time.utc(2009, 12, 29,  0, 30, 0), Time.utc(2010,  1, 29,  0, 30, 0),
        Time.utc(2010,  3,  1,  0, 30, 0), Time.utc(2010,  3, 28, 23, 30, 0),
        Time.utc(2010,  4, 28, 23, 30, 0)
      ])
    end

    it 'can round trip TimeWithZone to YAML' do
      schedule = Schedule.new(t0 = Time.zone.parse("2010-02-05 05:00:00"))
      schedule.add_recurrence_time t0
      schedule2 = Schedule.from_yaml(schedule.to_yaml)
      expect(schedule.all_occurrences).to eq(schedule2.all_occurrences)
    end

    it 'uses local zone from start time to determine occurs_on? from the beginning of day' do
      schedule = Schedule.new(Time.local(2009, 2, 7, 23, 59, 59))
      schedule.add_recurrence_rule Rule.daily
      expect(schedule.occurs_on?(Date.new(2009, 2, 7))).to be_truthy
    end

    it 'uses local zone from start time to determine occurs_on? to the end of day' do
      schedule = Schedule.new(Time.local(2009, 2, 7, 0, 0, 0))
      schedule.add_recurrence_rule Rule.daily
      expect(schedule.occurs_on?(Date.new(2009, 2, 7))).to be_truthy
    end

    it 'should use the correct zone for next_occurrences before start_time' do
      future_time = Time.zone.now.beginning_of_day + IceCube::ONE_DAY
      schedule = Schedule.new(future_time)
      schedule.add_recurrence_rule Rule.daily
      expect(schedule.next_occurrence.time_zone).to eq(schedule.start_time.time_zone)
    end

    it 'should use the correct zone for next_occurrences after start_time' do
      past_time = Time.zone.now.beginning_of_day
      schedule = Schedule.new(past_time)
      schedule.add_recurrence_rule Rule.daily
      expect(schedule.next_occurrence.time_zone).to eq(schedule.start_time.time_zone)
    end

    describe 'querying with time arguments for a different zone' do

      let(:schedule) do
        utc = Time.utc(2013, 1, 1).in_time_zone('UTC')
        Schedule.new(utc) { |s| s.add_recurrence_rule Rule.daily.count(3) }
      end

      let(:reference_time) do
        Time.utc(2013, 1, 1).in_time_zone('Bern') # +01:00
      end

      it 'uses schedule zone for next_occurrence' do
        next_occurrence = schedule.next_occurrence(reference_time)
        expect(next_occurrence).to eq(Time.utc(2013, 1, 2))
        expect(next_occurrence.time_zone).to eq(schedule.start_time.time_zone)
      end

      it 'uses schedule zone for next_occurrences' do
        next_occurrences = schedule.next_occurrences(2, reference_time)
        expect(next_occurrences).to eq([Time.utc(2013, 1, 2), Time.utc(2013, 1, 3)])
        next_occurrences.each do |t|
          expect(t.time_zone).to eq(schedule.start_time.time_zone)
        end
      end

      it 'uses schedule zone for remaining_occurrences' do
        remaining_occurrences = schedule.remaining_occurrences(reference_time + IceCube::ONE_DAY)
        expect(remaining_occurrences).to eq([Time.utc(2013, 1, 2), Time.utc(2013, 1, 3)])
        remaining_occurrences.each do |t|
          expect(t.time_zone).to eq(schedule.start_time.time_zone)
        end
      end

      it 'uses schedule zone for occurrences' do
        occurrences = schedule.occurrences(reference_time + IceCube::ONE_DAY)
        expect(occurrences).to eq([Time.utc(2013, 1, 1), Time.utc(2013, 1, 2)])
        occurrences.each do |t|
          expect(t.time_zone).to eq(schedule.start_time.time_zone)
        end
      end

      it 'uses schedule zone for occurrences_between' do
        occurrences_between = schedule.occurrences_between(reference_time, reference_time + IceCube::ONE_DAY)
        expect(occurrences_between).to eq([Time.utc(2013, 1, 1), Time.utc(2013, 1, 2)])
        occurrences_between.each do |t|
          expect(t.time_zone).to eq(schedule.start_time.time_zone)
        end
      end

      it "uses schedule zone for occurrences_between with a rule terminated by #count" do
        utc = Time.utc(2013, 1, 1).in_time_zone('UTC')
        schedule = Schedule.new(utc) { |s| s.add_recurrence_rule Rule.daily.count(3) }
        occurrences_between = schedule.occurrences_between(reference_time, reference_time + IceCube::ONE_DAY)
        expect(occurrences_between).to eq([Time.utc(2013, 1, 1), Time.utc(2013, 1, 2)])
        occurrences_between.each do |t|
          expect(t.time_zone).to eq(schedule.start_time.time_zone)
        end
      end

      it "uses schedule zone for occurrences_between with a rule terminated by #until" do
        utc = Time.utc(2013, 1, 1).in_time_zone('UTC')
        schedule = Schedule.new(utc) { |s| s.add_recurrence_rule Rule.daily.until(utc.advance(:days => 3)) }
        occurrences_between = schedule.occurrences_between(reference_time, reference_time + IceCube::ONE_DAY)
        expect(occurrences_between).to eq([Time.utc(2013, 1, 1), Time.utc(2013, 1, 2)])
        occurrences_between.each do |t|
          expect(t.time_zone).to eq(schedule.start_time.time_zone)
        end
      end

      it "uses schedule zone for occurrences_between with an unterminated rule" do
        utc = Time.utc(2013, 1, 1).in_time_zone('UTC')
        schedule = Schedule.new(utc) { |s| s.add_recurrence_rule Rule.daily }
        occurrences_between = schedule.occurrences_between(reference_time, reference_time + IceCube::ONE_DAY)
        expect(occurrences_between).to eq([Time.utc(2013, 1, 1), Time.utc(2013, 1, 2)])
        occurrences_between.each do |t|
          expect(t.time_zone).to eq(schedule.start_time.time_zone)
        end
      end

    end
  end
end

describe IceCube::Occurrence do

  it 'can be subtracted from a time' do
    start_time = Time.now
    occurrence = Occurrence.new(start_time)

    difference = (start_time + 60) - occurrence
    expect(difference).to eq(60)
  end

end