aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/calendar_spec.rb
blob: e71c2b0811ea8006f525568fa8096ef24e423630 (plain)
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
RSpec.describe Calendar, :type => :model do

  it { should belong_to(:organisation) }

  it { is_expected.to validate_presence_of(:organisation) }
  it { is_expected.to validate_presence_of(:name) }
  it { is_expected.to validate_presence_of(:short_name) }
  it { is_expected.to validate_uniqueness_of(:short_name) }
  it { is_expected.to be_versioned }

  describe '#to_time_table' do
    let(:calendar) { create(:calendar, date_ranges: [Date.today...(Date.today + 1.month)]) }

    it 'should convert calendar to an instance of Chouette::TimeTable' do
      time_table = calendar.convert_to_time_table
      expect(time_table).to be_an_instance_of(Chouette::TimeTable)
      expect(time_table.periods[0].period_start).to eq(calendar.periods[0].begin)
      expect(time_table.periods[0].period_end).to eq(calendar.periods[0].end)
      expect(time_table.dates.map(&:date)).to match_array(calendar.dates)
    end
  end

  describe 'validations' do
    it 'validates that dates and date_ranges do not overlap' do
      expect(build(:calendar, dates: [Date.today], date_ranges: [Date.today..Date.tomorrow])).to_not be_valid
    end

    it 'validates that there are no duplicates in dates' do
      expect(build(:calendar, dates: [Date.yesterday, Date.yesterday], date_ranges: [Date.today..Date.tomorrow])).to_not be_valid
    end
  end

  describe 'before_validation' do
    let(:calendar) { create(:calendar, date_ranges: []) }

    it 'shoud fill date_ranges with date ranges' do
      expected_range = Date.today..Date.tomorrow
      calendar.date_ranges << expected_range
      calendar.valid?

      expect(calendar.date_ranges.map { |period| period.begin..period.end }).to eq([expected_range])
    end
  end

end