cron_parser_spec.rb 3.24 KB
Newer Older
1 2 3 4
require 'spec_helper'

module Ci
  describe CronParser, lib: true do
Shinya Maeda's avatar
Shinya Maeda committed
5 6
    describe '#next_time_from' do
      subject { described_class.new(cron, cron_time_zone).next_time_from(Time.now) }
7 8

      context 'when cron and cron_time_zone are valid' do
Shinya Maeda's avatar
Shinya Maeda committed
9 10 11
        context 'when specific time' do
          let(:cron) { '3 4 5 6 *' }
          let(:cron_time_zone) { 'Europe/London' }
12

Shinya Maeda's avatar
Shinya Maeda committed
13 14 15 16 17 18
          it 'returns exact time in the future' do
            expect(subject).to be > Time.now.in_time_zone(cron_time_zone)
            expect(subject.min).to eq(3)
            expect(subject.hour).to eq(4)
            expect(subject.day).to eq(5)
            expect(subject.month).to eq(6)
19 20 21
          end
        end

Shinya Maeda's avatar
Shinya Maeda committed
22 23 24
        context 'when specific day of week' do
          let(:cron) { '* * * * 0' }
          let(:cron_time_zone) { 'Europe/London' }
25

Shinya Maeda's avatar
Shinya Maeda committed
26 27 28
          it 'returns exact day of week in the future' do
            expect(subject).to be > Time.now.in_time_zone(cron_time_zone)
            expect(subject.wday).to eq(0)
29 30 31
          end
        end

Shinya Maeda's avatar
Shinya Maeda committed
32 33
        context 'when slash used' do
          let(:cron) { '*/10 */6 */10 */10 *' }
34 35
          let(:cron_time_zone) { 'US/Pacific' }

Shinya Maeda's avatar
Shinya Maeda committed
36 37 38 39 40 41
          it 'returns exact minute' do
            expect(subject).to be > Time.now.in_time_zone(cron_time_zone)
            expect(subject.min).to be_in([0, 10, 20, 30, 40, 50])
            expect(subject.hour).to be_in([0, 6, 12, 18])
            expect(subject.day).to be_in([1, 11, 21, 31])
            expect(subject.month).to be_in([1, 11])
42 43 44
          end
        end

Shinya Maeda's avatar
Shinya Maeda committed
45 46
        context 'when range used' do
          let(:cron) { '0,20,40 * 1-5 * *' }
47 48 49
          let(:cron_time_zone) { 'US/Pacific' }

          it 'returns next time from now' do
Shinya Maeda's avatar
Shinya Maeda committed
50 51 52
            expect(subject).to be > Time.now.in_time_zone(cron_time_zone)
            expect(subject.min).to be_in([0, 20, 40])
            expect(subject.day).to be_in((1..5).to_a)
53 54 55 56
          end
        end

        context 'when cron_time_zone is US/Pacific' do
Shinya Maeda's avatar
Shinya Maeda committed
57
          let(:cron) { '* * * * *' }
58 59 60
          let(:cron_time_zone) { 'US/Pacific' }

          it 'returns next time from now' do
Shinya Maeda's avatar
Shinya Maeda committed
61 62
            expect(subject).to be > Time.now.in_time_zone(cron_time_zone)
            expect(subject.utc_offset/60/60).to eq(-7)
63 64 65 66 67
          end
        end
      end

      context 'when cron and cron_time_zone are invalid' do
Shinya Maeda's avatar
Shinya Maeda committed
68 69
        let(:cron) { 'invalid_cron' }
        let(:cron_time_zone) { 'invalid_cron_time_zone' }
70 71 72 73 74 75 76

        it 'returns nil' do
          is_expected.to be_nil
        end 
      end
    end

Shinya Maeda's avatar
Shinya Maeda committed
77 78 79 80 81
    describe '#validation' do
      it 'returns results' do
        is_valid_cron, is_valid_cron_time_zone = described_class.new('* * * * *', 'Europe/Istanbul').validation
        expect(is_valid_cron).to eq(true)
        expect(is_valid_cron_time_zone).to eq(true)
82 83
      end

Shinya Maeda's avatar
Shinya Maeda committed
84 85 86 87 88
      it 'returns results' do
        is_valid_cron, is_valid_cron_time_zone = described_class.new('*********', 'Europe/Istanbul').validation
        expect(is_valid_cron).to eq(false)
        expect(is_valid_cron_time_zone).to eq(true)
      end
89

Shinya Maeda's avatar
Shinya Maeda committed
90 91 92 93
      it 'returns results' do
        is_valid_cron, is_valid_cron_time_zone = described_class.new('* * * * *', 'Invalid-zone').validation
        expect(is_valid_cron).to eq(true)
        expect(is_valid_cron_time_zone).to eq(false)
94 95 96 97
      end
    end
  end
end