| 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
 | require 'rails_helper'
RSpec.describe CleanUp, :type => :model do
  it { should validate_presence_of(:begin_date) }
  it { should validate_presence_of(:date_type) }
  it { should belong_to(:referential) }
  context '#exclude_dates_in_overlapping_period with :before date_type' do
    let(:time_table) { create(:time_table) }
    let(:period) { time_table.periods[0] }
    let(:cleaner) { create(:clean_up, date_type: :before) }
    it 'should add exclude date into period for overlapping period' do
      days_in_period = (period.period_start..period.period_end).count
      cleaner.begin_date = period.period_end
      expect { cleaner.exclude_dates_in_overlapping_period(period) }.to change {
        time_table.dates.where(in_out: false).count
      }.by(days_in_period - 1)
    end
    it 'should not add exclude date if no overlapping found' do
      cleaner.begin_date = period.period_start
      expect { cleaner.exclude_dates_in_overlapping_period(period) }.to_not change {
        time_table.dates.where(in_out: false).count
      }
    end
  end
  context '#exclude_dates_in_overlapping_period with :after date_type' do
    let(:time_table) { create(:time_table) }
    let(:period) { time_table.periods[0] }
    let(:cleaner) { create(:clean_up, date_type: :after) }
    it 'should add exclude date into period for overlapping period' do
      days_in_period = (period.period_start..period.period_end).count
      cleaner.begin_date = period.period_start + 1.day
      expect { cleaner.exclude_dates_in_overlapping_period(period) }.to change {
        time_table.dates.where(in_out: false).count
      }.by(days_in_period - 2)
    end
    it 'should not add exclude date if no overlapping found' do
      cleaner.begin_date = period.period_end
      expect { cleaner.exclude_dates_in_overlapping_period(period) }.to_not change {
        time_table.dates.where(in_out: false).count
      }
    end
  end
   context '#exclude_dates_in_overlapping_period with :between date_type' do
    let(:time_table) { create(:time_table) }
    let(:period) { time_table.periods[0] }
    let(:cleaner) { create(:clean_up, date_type: :between, begin_date: period.period_start + 3.day, end_date: period.period_end) }
    it 'should add exclude date into period for overlapping period' do
      expected_day_out = (cleaner.begin_date..cleaner.end_date).count
      expect { cleaner.exclude_dates_in_overlapping_period(period) }.to change {
        time_table.dates.where(in_out: false).count
      }.by(expected_day_out)
    end
    it 'should not add exclude date if no overlapping found' do
      cleaner.begin_date = period.period_end  + 1.day
      cleaner.end_date   = cleaner.begin_date + 1.day
      expect { cleaner.exclude_dates_in_overlapping_period(period) }.to_not change {
        time_table.dates.where(in_out: false).count
      }
    end
  end
  context '#overlapping_periods' do
    let(:cleaner) { create(:clean_up, date_type: :before, end_date: nil) }
    let(:time_table) { create(:time_table) }
    it 'should detect overlapping periods' do
      cleaner.begin_date = time_table.periods[0].period_start
      expect(cleaner.overlapping_periods).to include(time_table.periods[0])
    end
    it 'should not return none overlapping periods' do
      cleaner.begin_date = time_table.periods[0].period_start - 1.day
      expect(cleaner.overlapping_periods).to_not include(time_table.periods[0])
    end
  end
  context '#clean' do
    let(:cleaner) { create(:clean_up, date_type: :before) }
    it 'should call destroy_time_tables_before' do
      cleaner.date_type = :before
      expect(cleaner).to receive(:destroy_time_tables_before)
      expect(cleaner).to receive(:destroy_time_tables_dates_before)
      expect(cleaner).to receive(:destroy_time_tables_periods_before)
      cleaner.clean
    end
    it 'should call destroy_time_tables_after' do
      cleaner.date_type = :after
      expect(cleaner).to receive(:destroy_time_tables_after)
      expect(cleaner).to receive(:destroy_time_tables_dates_after)
      expect(cleaner).to receive(:destroy_time_tables_periods_after)
      cleaner.clean
    end
    it 'should call destroy_time_tables_between' do
      cleaner.date_type = :between
      expect(cleaner).to receive(:destroy_time_tables_between)
      expect(cleaner).to receive(:destroy_time_tables_dates_between)
      expect(cleaner).to receive(:destroy_time_tables_periods_between)
      cleaner.clean
    end
  end
  context '#destroy_time_tables_dates_between' do
    let!(:time_table) { create(:time_table) }
    let(:cleaner) { create(:clean_up, date_type: :between) }
    before do
      time_table.periods.clear
      time_table.save
      cleaner.begin_date = time_table.start_date
      cleaner.end_date   = time_table.end_date
    end
    it 'should destroy record' do
      expect{ cleaner.destroy_time_tables_dates_between }.to change {
        Chouette::TimeTableDate.count
      }.by(-time_table.dates.count)
    end
    it 'should not destroy record not in range' do
      cleaner.begin_date = time_table.end_date + 1.day
      cleaner.end_date   = cleaner.begin_date + 1.day
      expect{ cleaner.destroy_time_tables_dates_between }.to_not change {
        Chouette::TimeTableDate.count
      }
    end
  end
  context '#destroy_time_tables_dates_after' do
    let!(:time_table_date) { create(:time_table_date, date: Date.yesterday, in_out: true) }
    let(:cleaner) { create(:clean_up, date_type: :after, begin_date: time_table_date.date) }
    it 'should destroy record' do
      count = Chouette::TimeTableDate.where('date > ?', cleaner.begin_date).count
      expect{ cleaner.destroy_time_tables_dates_after }.to change {
        Chouette::TimeTableDate.count
      }.by(-count)
    end
  end
  context '#destroy_time_tables_between' do
    let!(:time_table) { create(:time_table ) }
    let(:cleaner) { create(:clean_up, date_type: :after, begin_date: time_table.start_date, end_date: time_table.end_date) }
    it 'should destroy time_tables with validity period in purge range' do
      expect{ cleaner.destroy_time_tables_between }.to change {
        Chouette::TimeTable.count
      }.by(-1)
    end
    it 'should not destroy time_tables if not totaly inside purge range' do
      cleaner.begin_date = time_table.start_date + 1.day
      expect{ cleaner.destroy_time_tables_between }.to_not change {
        Chouette::TimeTable.count
      }
    end
  end
  context '#destroy_time_tables_after' do
    let!(:time_table) { create(:time_table ) }
    let(:cleaner) { create(:clean_up, date_type: :after, begin_date: time_table.start_date - 1.day) }
    it 'should destroy time_tables with start_date > purge begin_date' do
      expect{ cleaner.destroy_time_tables_after }.to change {
        Chouette::TimeTable.count
      }.by(-1)
    end
    it 'should not destroy time_tables with start_date < purge begin date' do
      cleaner.begin_date = time_table.end_date
      expect{ cleaner.destroy_time_tables_after }.to_not change {
        Chouette::TimeTable.count
      }
    end
  end
  context '#destroy_time_tables' do
    let!(:time_table) { create(:time_table) }
    let(:cleaner) { create(:clean_up) }
    it 'should destroy all time_tables' do
      expect{cleaner.destroy_time_tables(Chouette::TimeTable.all)}.to change {
        Chouette::TimeTable.count
      }.by(-1)
    end
    it 'should destroy time_table vehicle_journey association' do
      vj = create(:vehicle_journey, time_tables: [time_table])
      cleaner.destroy_time_tables(Chouette::TimeTable.all)
      expect(vj.reload.time_tables).to be_empty
    end
  end
  context '#destroy_vehicle_journey_without_time_table' do
    let(:cleaner) { create(:clean_up) }
    it 'should destroy vehicle_journey' do
      vj = create(:vehicle_journey)
      expect{cleaner.destroy_vehicle_journey_without_time_table
      }.to change { Chouette::VehicleJourney.count }.by(-1)
    end
    it 'should not destroy vehicle_journey with time_table' do
      create(:vehicle_journey, time_tables: [create(:time_table)])
      expect{cleaner.destroy_vehicle_journey_without_time_table
      }.to_not change { Chouette::VehicleJourney.count }
    end
  end
  context '#destroy_time_tables_before' do
    let!(:time_table) { create(:time_table ) }
    let(:cleaner) { create(:clean_up, date_type: :before, begin_date: time_table.end_date + 1.day) }
    it 'should destroy time_tables with end_date < purge begin_date' do
      expect{ cleaner.destroy_time_tables_before }.to change {
        Chouette::TimeTable.count
      }.by(-1)
    end
    it 'should not destroy time_tables with end_date > purge begin date' do
      cleaner.begin_date = Date.today
      expect{ cleaner.destroy_time_tables_before }.to_not change {
        Chouette::TimeTable.count
      }
    end
  end
end
 |