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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
require 'rails_helper'
RSpec.describe CleanUp, :type => :model do
it { should validate_presence_of(:date_type).with_message(:presence) }
it { should validate_presence_of(:begin_date).with_message(:presence) }
it { should belong_to(:referential) }
context 'Clean Up With Date Type : Between' do
subject(:cleaner) { create(:clean_up, date_type: :between) }
it { should validate_presence_of(:end_date).with_message(:presence) }
it 'should have a end date strictly greater than the begin date' do
expect(cleaner).to be_valid
cleaner.end_date = cleaner.begin_date
expect(cleaner).not_to be_valid
end
end
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, begin_date: period.period_end) }
it 'should add exclude date into period for overlapping period' do
days_in_period = (period.period_start..period.period_end).count
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, begin_date: period.period_start + 1.day) }
it 'should add exclude date into period for overlapping period' do
days_in_period = (period.period_start..period.period_end).count
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 - 2)
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(:time_table) { create(:time_table) }
let(:period) { time_table.periods[0] }
let(:cleaner) { create(:clean_up, date_type: :before, begin_date: period.period_start) }
it 'should detect overlapping periods' do
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(:referential) { Referential.new }
let(:cleaner) { create(:clean_up, date_type: :before, referential: referential) }
before do
allow(referential).to receive(:switch)
end
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 + 2)
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: :between, begin_date: time_table.start_date - 1.day, end_date: time_table.end_date + 1.day) }
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, create(:time_table)])
cleaner.destroy_time_tables(Chouette::TimeTable.where(id: time_table.id))
expect(vj.reload.time_tables.map(&:id)).to_not include(time_table.id)
end
it 'should also destroy associated vehicle_journey if it belongs to any other time_table' do
vj = create(:vehicle_journey, time_tables: [time_table])
expect{cleaner.destroy_time_tables(Chouette::TimeTable.all)}.to change {
Chouette::VehicleJourney.count
}.by(-1)
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
describe "#destroy_routes_outside_referential" do
let(:line_referential) { create(:line_referential) }
let(:line) { create(:line, line_referential: line_referential) }
let(:metadata) { create(:referential_metadata, lines: [line]) }
let(:referential) { create(:workbench_referential, metadatas: [metadata]) }
let(:cleaner) { create(:clean_up, referential: referential) }
it "destroys routes not in the the referential" do
route = create(:route)
cleaner.destroy_routes_outside_referential
expect(Chouette::Route.exists?(route.id)).to be false
line.routes.each do |route|
expect(route).not_to be_destroyed
end
end
it "cascades destruction of vehicle journeys and journey patterns" do
vehicle_journey = create(:vehicle_journey)
cleaner.destroy_routes_outside_referential
expect(Chouette::Route.exists?(vehicle_journey.route.id)).to be false
expect(
Chouette::JourneyPattern.exists?(vehicle_journey.journey_pattern.id)
).to be false
expect(Chouette::VehicleJourney.exists?(vehicle_journey.id)).to be false
end
end
describe "#destroy_empty" do
it "calls the appropriate destroy methods" do
cleaner = create(:clean_up)
expect(cleaner).to receive(:destroy_vehicle_journeys)
expect(cleaner).to receive(:destroy_journey_patterns)
expect(cleaner).to receive(:destroy_routes)
cleaner.destroy_empty
end
end
describe "#run_methods" do
let(:cleaner) { create(:clean_up) }
it "calls methods in the :methods attribute" do
cleaner = create(
:clean_up,
methods: [:destroy_routes_outside_referential]
)
expect(cleaner).to receive(:destroy_routes_outside_referential)
cleaner.run_methods
end
it "doesn't do anything if :methods is nil" do
cleaner = create(:clean_up)
expect { cleaner.run_methods }.not_to raise_error
end
end
end
|