|
| 1 | +require File.dirname(__FILE__) + "/../spec_helper" |
| 2 | + |
| 3 | +module IceCube |
| 4 | + describe Rule, "from_hash" do |
| 5 | + describe "rule_type validations" do |
| 6 | + it "should raise an ArgumentError when the hash is empty" do |
| 7 | + expect { Rule.from_hash({}) } |
| 8 | + .to raise_error ArgumentError, "Invalid rule type" |
| 9 | + end |
| 10 | + |
| 11 | + it "should raise an ArgumentError when the hash[:rule_type] is invalid" do |
| 12 | + expect { Rule.from_hash({rule_type: "IceCube::MadeUpIntervalRule"}) } |
| 13 | + .to raise_error ArgumentError, "Invalid rule frequency type: MadeUpInterval" |
| 14 | + end |
| 15 | + |
| 16 | + it "returns a SecondlyRule when the hash[:rule_type] is secondly" do |
| 17 | + expect(Rule.from_hash({rule_type: "IceCube::SecondlyRule"})).to be_a SecondlyRule |
| 18 | + end |
| 19 | + |
| 20 | + it "returns a MinutelyRule when the hash[:rule_type] is minutely" do |
| 21 | + expect(Rule.from_hash({rule_type: "IceCube::MinutelyRule"})).to be_a MinutelyRule |
| 22 | + end |
| 23 | + |
| 24 | + it "returns a HourlyRule when the hash[:rule_type] is hourly" do |
| 25 | + expect(Rule.from_hash({rule_type: "IceCube::HourlyRule"})).to be_a HourlyRule |
| 26 | + end |
| 27 | + |
| 28 | + it "returns a DailyRule when the hash[:rule_type] is daily" do |
| 29 | + expect(Rule.from_hash({rule_type: "IceCube::DailyRule"})).to be_a DailyRule |
| 30 | + end |
| 31 | + |
| 32 | + it "returns a WeeklyRule when the hash[:rule_type] is weekly" do |
| 33 | + expect(Rule.from_hash({rule_type: "IceCube::WeeklyRule"})).to be_a WeeklyRule |
| 34 | + end |
| 35 | + |
| 36 | + it "returns a MonthlyRule when the hash[:rule_type] is monthly" do |
| 37 | + expect(Rule.from_hash({rule_type: "IceCube::MonthlyRule"})).to be_a MonthlyRule |
| 38 | + end |
| 39 | + |
| 40 | + it "returns a YearlyRule when the hash[:rule_type] is yearly" do |
| 41 | + expect(Rule.from_hash({rule_type: "IceCube::YearlyRule"})).to be_a YearlyRule |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + describe "creating monthly rule" do |
| 46 | + context "with valid day_of_week validations" do |
| 47 | + let(:input_hash) { |
| 48 | + { |
| 49 | + rule_type: "IceCube::MonthlyRule", |
| 50 | + interval: 1, |
| 51 | + validations: { |
| 52 | + day_of_week: { |
| 53 | + "0": [2], |
| 54 | + "1": [2], |
| 55 | + "2": [2], |
| 56 | + "3": [2], |
| 57 | + "4": [2], |
| 58 | + "5": [2], |
| 59 | + "6": [1, 2] |
| 60 | + }, |
| 61 | + hour_of_day: 7, |
| 62 | + minute_of_hour: 19 |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + it "can provide the first occurrence" do |
| 68 | + rule = Rule.from_hash(input_hash) |
| 69 | + schedule = Schedule.new(Time.utc(2010, 1, 1, 0, 0, 0)) |
| 70 | + schedule.add_recurrence_rule rule |
| 71 | + expect(schedule.first(10).map(&:to_time)).to eq([ |
| 72 | + Time.utc(2010, 1, 2, 7, 19, 0), |
| 73 | + Time.utc(2010, 1, 8, 7, 19, 0), |
| 74 | + Time.utc(2010, 1, 9, 7, 19, 0), |
| 75 | + Time.utc(2010, 1, 10, 7, 19, 0), |
| 76 | + Time.utc(2010, 1, 11, 7, 19, 0), |
| 77 | + Time.utc(2010, 1, 12, 7, 19, 0), |
| 78 | + Time.utc(2010, 1, 13, 7, 19, 0), |
| 79 | + Time.utc(2010, 1, 14, 7, 19, 0), |
| 80 | + Time.utc(2010, 2, 6, 7, 19, 0), |
| 81 | + Time.utc(2010, 2, 8, 7, 19, 0) |
| 82 | + ]) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context "with invalid day_of_week validations" do |
| 87 | + let(:input_hash_with_zeroeth_occurrence) { |
| 88 | + { |
| 89 | + rule_type: "IceCube::MonthlyRule", |
| 90 | + interval: 1, |
| 91 | + validations: { |
| 92 | + day_of_week: { |
| 93 | + "1": [], |
| 94 | + "2": [0], |
| 95 | + "3": [], |
| 96 | + "4": [] |
| 97 | + }, |
| 98 | + hour_of_day: 7, |
| 99 | + minute_of_hour: 19 |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + let(:input_hash_with_sixth_occurrence) { |
| 104 | + { |
| 105 | + rule_type: "IceCube::MonthlyRule", |
| 106 | + interval: 1, |
| 107 | + validations: { |
| 108 | + day_of_week: { |
| 109 | + "1": [], |
| 110 | + "2": [6], |
| 111 | + "3": [], |
| 112 | + "4": [] |
| 113 | + }, |
| 114 | + hour_of_day: 7, |
| 115 | + minute_of_hour: 19 |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + it "should raise an ArgumentError" do |
| 121 | + expect { Rule.from_hash(input_hash_with_zeroeth_occurrence) } |
| 122 | + .to raise_error ArgumentError, "Invalid day_of_week occurrence: 0" |
| 123 | + expect { Rule.from_hash(input_hash_with_sixth_occurrence) } |
| 124 | + .to raise_error ArgumentError, "Invalid day_of_week occurrence: 6" |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | +end |
0 commit comments