aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/time_table_combination_spec.rb
blob: 81f9dd7a68aae9bb920e67633fe4e692d90b3873 (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
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
require 'spec_helper'

describe TimeTableCombination, :type => :model do
  let!(:source){ create(:time_table)}
  let!(:combined){create(:time_table)}
  subject {build(:time_table_combination)}

  describe '#continuous_dates' do
    it 'should group continuous dates' do
      dates = source.dates.where(in_out: true)
      expect(source.continuous_dates.flatten.count).to eq(dates.count)

      # 6 more continuous dates, 2 isolated dates
      (10..15).each do |n|
        source.dates.create(date: Date.today + n.day, in_out: true)
      end

      (1..2).each do |n|
         source.dates.create(date: Date.today + n.day + 1.year, in_out: true)
      end

      expect(source.reload.continuous_dates[1].count).to eq(6)
      expect(source.reload.continuous_dates[2].count).to eq(2)
    end
  end

  describe '#convert_continuous_dates_to_periods' do
    it 'should convert continuous dates to periods' do
      source.dates.clear

      (10..12).each do |n|
        source.dates.create(date: Date.today + n.day, in_out: true)
      end

      (1..3).each do |n|
         source.dates.create(date: Date.today + n.day + 1.year, in_out: true)
      end

      expect {
        source.reload.convert_continuous_dates_to_periods
      }.to change {source.periods.count}.by(2)

      expect(source.reload.dates.where(in_out: true).count).to eq(0)
    end
  end

  describe "#combine" do
    context "when operation is union" do
      before(:each) do
        source.periods.clear
        source.dates.clear
        source.int_day_types = 508
        source.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,31))
        source.save
        combined.periods.clear
        combined.dates.clear
        combined.int_day_types = 508
        combined.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,15), :period_end => Date.new(2014,9,15))
        combined.save

        subject.operation     = 'union'
        subject.source_id     = source.id
        subject.time_table_id = combined.id
        subject.combined_type = 'time_table'

        subject.combine
        source.reload
      end

      it "should add combined to source" do
        expect(source.periods.size).to eq(1)
        expect(source.periods[0].period_start).to eq(Date.new(2014,8,1))
        expect(source.periods[0].period_end).to eq(Date.new(2014,9,15))
      end
    end
    context "when operation is intersect" do
      before(:each) do
        source.periods.clear
        source.dates.clear
        source.int_day_types = 508
        source.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,31))
        source.save
        combined.periods.clear
        combined.dates.clear
        combined.int_day_types = 508
        combined.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,15), :period_end => Date.new(2014,9,15))
        combined.save

        subject.operation     = 'intersection'
        subject.source_id     = source.id
        subject.time_table_id = combined.id
        subject.combined_type = 'time_table'

        subject.combine
        source.reload
      end

      it "should intersect combined to source" do
        expect(source.int_day_types).to eq(508)
        expect(source.periods.size).to eq(1)
  		  expect(source.dates.size).to eq(0)

        expect(source.periods.first.period_start.to_s).to eq('2014-08-15')
        expect(source.periods.first.period_end.to_s).to eq('2014-08-31')
     end
    end

    context "when operation is disjoin" do
      before(:each) do
        source.periods.clear
        source.dates.clear
        source.int_day_types = 508
        source.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,31))
        source.save
        combined.periods.clear
        combined.dates.clear
        combined.int_day_types = 508
        combined.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,15), :period_end => Date.new(2014,9,15))
        combined.save

        subject.operation     = 'disjunction'
        subject.source_id     = source.id
        subject.time_table_id = combined.id
        subject.combined_type = 'time_table'

        subject.combine
        source.reload
      end

      it "should disjoin combined to source" do
        expect(source.int_day_types).to eq(508)
        expect(source.periods.size).to eq(1)
		    expect(source.dates.size).to eq(0)

        expect(source.periods.first.period_start.to_s).to eq('2014-08-01')
        expect(source.periods.first.period_end.to_s).to eq('2014-08-14')
      end
    end
 end
end