ninoxe 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +5 -13
  2. data/app/models/chouette/footnote.rb +7 -0
  3. data/app/models/chouette/line.rb +6 -1
  4. data/app/models/chouette/vehicle_journey.rb +7 -4
  5. data/db/migrate/20150115153453_create_footnotes.rb +11 -0
  6. data/db/migrate/20150119160029_create_vehicle_journey_footnotes.rb +8 -0
  7. data/lib/factories/chouette_footnotes.rb +6 -0
  8. data/lib/ninoxe/version.rb +1 -1
  9. data/spec/dummy/README.rdoc +261 -0
  10. data/spec/dummy/Rakefile +6 -0
  11. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  12. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  13. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  14. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  15. data/spec/dummy/app/mailers/.gitkeep +0 -0
  16. data/spec/dummy/app/models/.gitkeep +0 -0
  17. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  18. data/spec/dummy/config/application.rb +56 -0
  19. data/spec/dummy/config/boot.rb +10 -0
  20. data/{config/database.yml.me → spec/dummy/config/database.yml} +2 -2
  21. data/spec/dummy/config/environment.rb +7 -0
  22. data/spec/dummy/config/environments/development.rb +37 -0
  23. data/spec/dummy/config/environments/production.rb +67 -0
  24. data/spec/dummy/config/environments/test.rb +37 -0
  25. data/spec/dummy/config/initializers/active_record.rb +2 -0
  26. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  27. data/spec/dummy/config/initializers/inflections.rb +15 -0
  28. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  29. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  30. data/spec/dummy/config/initializers/session_store.rb +8 -0
  31. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  32. data/spec/dummy/config/locales/en.yml +5 -0
  33. data/spec/dummy/config/routes.rb +4 -0
  34. data/spec/dummy/config.ru +4 -0
  35. data/spec/dummy/db/schema.rb +485 -0
  36. data/spec/dummy/db/test.sqlite3 +0 -0
  37. data/spec/dummy/lib/assets/.gitkeep +0 -0
  38. data/spec/dummy/log/.gitkeep +0 -0
  39. data/spec/dummy/public/404.html +26 -0
  40. data/spec/dummy/public/422.html +26 -0
  41. data/spec/dummy/public/500.html +25 -0
  42. data/spec/dummy/public/favicon.ico +0 -0
  43. data/spec/dummy/script/rails +6 -0
  44. data/spec/models/chouette/access_link_spec.rb +78 -0
  45. data/spec/models/chouette/access_point_spec.rb +266 -0
  46. data/spec/models/chouette/active_record_spec.rb +121 -0
  47. data/spec/models/chouette/area_type_spec.rb +53 -0
  48. data/spec/models/chouette/company_spec.rb +51 -0
  49. data/spec/models/chouette/connection_link_spec.rb +56 -0
  50. data/spec/models/chouette/direction_spec.rb +60 -0
  51. data/spec/models/chouette/exporter_spec.rb +28 -0
  52. data/spec/models/chouette/file_validator_spec.rb +28 -0
  53. data/spec/models/chouette/footnote_spec.rb +9 -0
  54. data/spec/models/chouette/group_of_line_spec.rb +31 -0
  55. data/spec/models/chouette/journey_pattern_spec.rb +62 -0
  56. data/spec/models/chouette/line_spec.rb +119 -0
  57. data/spec/models/chouette/loader_spec.rb +69 -0
  58. data/spec/models/chouette/network_spec.rb +22 -0
  59. data/spec/models/chouette/object_id_spec.rb +146 -0
  60. data/spec/models/chouette/route_spec.rb +234 -0
  61. data/spec/models/chouette/stop_area_spec.rb +440 -0
  62. data/spec/models/chouette/stop_point_spec.rb +38 -0
  63. data/spec/models/chouette/time_table_period_spec.rb +66 -0
  64. data/spec/models/chouette/time_table_spec.rb +1218 -0
  65. data/spec/models/chouette/transport_mode_spec.rb +64 -0
  66. data/spec/models/chouette/trident_active_record_spec.rb +115 -0
  67. data/spec/models/chouette/vehicle_journey_at_stop_spec.rb +46 -0
  68. data/spec/models/chouette/vehicle_journey_spec.rb +223 -0
  69. data/spec/presenters/chouette/geometry/general_presenter.rb +1 -0
  70. data/spec/presenters/chouette/geometry/line_presenter_spec.rb +13 -0
  71. data/spec/spec_helper.rb +45 -0
  72. metadata +160 -33
  73. data/app/models/chouette/time_table_vehicle_journey.rb +0 -5
  74. data/lib/ninoxe.rb~ +0 -7
@@ -0,0 +1,1218 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chouette::TimeTable do
4
+
5
+ subject { Factory(:time_table) }
6
+
7
+ it { should validate_presence_of :comment }
8
+ it { should validate_uniqueness_of :objectid }
9
+
10
+ describe "#periods_max_date" do
11
+ context "when all period extends from 04/10/2013 to 04/15/2013," do
12
+ before(:each) do
13
+ p1 = Chouette::TimeTablePeriod.new( :period_start => Date.strptime("04/10/2013", '%m/%d/%Y'), :period_end => Date.strptime("04/12/2013", '%m/%d/%Y'))
14
+ p2 = Chouette::TimeTablePeriod.new( :period_start => Date.strptime("04/13/2013", '%m/%d/%Y'), :period_end => Date.strptime("04/15/2013", '%m/%d/%Y'))
15
+ subject.periods = [ p1, p2]
16
+ subject.save
17
+ end
18
+
19
+ it "should retreive 04/15/2013" do
20
+ subject.periods_max_date.should == Date.strptime("04/15/2013", '%m/%d/%Y')
21
+ end
22
+ context "when 04/15/2013 is excluded, periods_max_dates selects the day before" do
23
+ before(:each) do
24
+ excluded_date = Date.strptime("04/15/2013", '%m/%d/%Y')
25
+ subject.dates = [ Chouette::TimeTableDate.new( :date => excluded_date, :in_out => false)]
26
+ subject.save
27
+ end
28
+ it "should retreive 04/14/2013" do
29
+ subject.periods_max_date.should == Date.strptime("04/14/2013", '%m/%d/%Y')
30
+ end
31
+ end
32
+ context "when day_types select only sunday and saturday," do
33
+ before(:each) do
34
+ # jeudi, vendredi
35
+ subject.update_attributes( :int_day_types => (2**(1+6) + 2**(1+7)))
36
+ end
37
+ it "should retreive 04/14/2013" do
38
+ subject.periods_max_date.should == Date.strptime("04/14/2013", '%m/%d/%Y')
39
+ end
40
+ end
41
+ context "when day_types select only friday," do
42
+ before(:each) do
43
+ # jeudi, vendredi
44
+ subject.update_attributes( :int_day_types => (2**(1+6)))
45
+ end
46
+ it "should retreive 04/12/2013" do
47
+ subject.periods_max_date.should == Date.strptime("04/13/2013", '%m/%d/%Y')
48
+ end
49
+ end
50
+ context "when day_types select only thursday," do
51
+ before(:each) do
52
+ # mardi
53
+ subject.update_attributes( :int_day_types => (2**(1+2)))
54
+ end
55
+ it "should retreive 04/12/2013" do
56
+ # 04/15/2013 is monday !
57
+ subject.periods_max_date.should be_nil
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "update_attributes on periods and dates" do
64
+
65
+ context "update days selection" do
66
+ it "should update start_date and end_end" do
67
+ days_hash = {}.tap do |hash|
68
+ [ :monday,:tuesday,:wednesday,:thursday,:friday,:saturday,:sunday ].each { |d| hash[d] = false }
69
+ end
70
+ subject.update_attributes( days_hash)
71
+
72
+ read = Chouette::TimeTable.find( subject.id )
73
+ read.start_date.should == read.dates.select{|d| d.in_out}.map(&:date).compact.min
74
+ read.end_date.should == read.dates.select{|d| d.in_out}.map(&:date).compact.max
75
+
76
+ end
77
+ end
78
+ context "add a new period" do
79
+ let!( :new_start_date ){ subject.start_date - 20.days }
80
+ let!( :new_end_date ){ subject.end_date + 20.days }
81
+ let!( :new_period_attributes ) {
82
+ pa = periods_attributes
83
+ pa[ "11111111111" ] = { "period_end" => new_end_date, "period_start" => new_start_date, "_destroy" => "", "position" => pa.size.to_s, "id" => "", "time_table_id" => subject.id.to_s}
84
+ pa
85
+ }
86
+ it "should update start_date and end_end" do
87
+ subject.update_attributes( :periods_attributes => new_period_attributes)
88
+
89
+ read = Chouette::TimeTable.find( subject.id )
90
+ read.start_date.should == new_start_date
91
+ read.end_date.should == new_end_date
92
+ end
93
+ end
94
+ context "update period end" do
95
+ let!( :new_end_date ){ subject.end_date + 20.days }
96
+ let!( :new_period_attributes ) {
97
+ pa = periods_attributes
98
+ pa[ "0" ].merge! "period_end" => new_end_date
99
+ pa
100
+ }
101
+ it "should update end_date" do
102
+ subject.update_attributes :periods_attributes => new_period_attributes
103
+
104
+ read = Chouette::TimeTable.find( subject.id )
105
+ read.end_date.should == new_end_date
106
+ end
107
+ end
108
+ context "update period start" do
109
+ let!( :new_start_date ){ subject.start_date - 20.days }
110
+ let!( :new_period_attributes ) {
111
+ pa = periods_attributes
112
+ pa[ "0" ].merge! "period_start" => new_start_date
113
+ pa
114
+ }
115
+ it "should update start_date" do
116
+ subject.update_attributes :periods_attributes => new_period_attributes
117
+
118
+ read = Chouette::TimeTable.find( subject.id )
119
+ read.start_date.should == new_start_date
120
+ end
121
+ end
122
+ context "remove periods and dates and add a new period" do
123
+ let!( :new_start_date ){ subject.start_date + 1.days }
124
+ let!( :new_end_date ){ subject.end_date - 1.days }
125
+ let!( :new_dates_attributes ) {
126
+ da = dates_attributes
127
+ da.each { |k,v| v.merge! "_destroy" => true}
128
+ da
129
+ }
130
+ let!( :new_period_attributes ) {
131
+ pa = periods_attributes
132
+ pa.each { |k,v| v.merge! "_destroy" => true}
133
+ pa[ "11111111111" ] = { "period_end" => new_end_date, "period_start" => new_start_date, "_destroy" => "", "position" => pa.size.to_s, "id" => "", "time_table_id" => subject.id.to_s}
134
+ pa
135
+ }
136
+ it "should update start_date and end_date with new period added" do
137
+ subject.update_attributes :periods_attributes => new_period_attributes, :dates_attributes => new_dates_attributes
138
+
139
+ read = Chouette::TimeTable.find( subject.id )
140
+ read.start_date.should == new_start_date
141
+ read.end_date.should == new_end_date
142
+ end
143
+ end
144
+ def dates_attributes
145
+ {}.tap do |hash|
146
+ subject.dates.each_with_index do |p, index|
147
+ hash.merge! index.to_s => p.attributes.merge( "_destroy" => "" )
148
+ end
149
+ end
150
+ end
151
+ def periods_attributes
152
+ {}.tap do |hash|
153
+ subject.periods.each_with_index do |p, index|
154
+ hash.merge! index.to_s => p.attributes.merge( "_destroy" => "" )
155
+ end
156
+ end
157
+ end
158
+ end
159
+
160
+ describe "#periods_min_date" do
161
+ context "when all period extends from 04/10/2013 to 04/15/2013," do
162
+ before(:each) do
163
+ p1 = Chouette::TimeTablePeriod.new( :period_start => Date.strptime("04/10/2013", '%m/%d/%Y'), :period_end => Date.strptime("04/12/2013", '%m/%d/%Y'))
164
+ p2 = Chouette::TimeTablePeriod.new( :period_start => Date.strptime("04/13/2013", '%m/%d/%Y'), :period_end => Date.strptime("04/15/2013", '%m/%d/%Y'))
165
+ subject.periods = [ p1, p2]
166
+ subject.save
167
+ end
168
+
169
+ it "should retreive 04/10/2013" do
170
+ subject.periods_min_date.should == Date.strptime("04/10/2013", '%m/%d/%Y')
171
+ end
172
+ context "when 04/10/2013 is excluded, periods_min_dates select the day after" do
173
+ before(:each) do
174
+ excluded_date = Date.strptime("04/10/2013", '%m/%d/%Y')
175
+ subject.dates = [ Chouette::TimeTableDate.new( :date => excluded_date, :in_out => false)]
176
+ subject.save
177
+ end
178
+ it "should retreive 04/11/2013" do
179
+ subject.periods_min_date.should == Date.strptime("04/11/2013", '%m/%d/%Y')
180
+ end
181
+ end
182
+ context "when day_types select only tuesday and friday," do
183
+ before(:each) do
184
+ # jeudi, vendredi
185
+ subject.update_attributes( :int_day_types => (2**(1+4) + 2**(1+5)))
186
+ end
187
+ it "should retreive 04/11/2013" do
188
+ subject.periods_min_date.should == Date.strptime("04/11/2013", '%m/%d/%Y')
189
+ end
190
+ end
191
+ context "when day_types select only friday," do
192
+ before(:each) do
193
+ # jeudi, vendredi
194
+ subject.update_attributes( :int_day_types => (2**(1+5)))
195
+ end
196
+ it "should retreive 04/12/2013" do
197
+ subject.periods_min_date.should == Date.strptime("04/12/2013", '%m/%d/%Y')
198
+ end
199
+ end
200
+ context "when day_types select only thursday," do
201
+ before(:each) do
202
+ # mardi
203
+ subject.update_attributes( :int_day_types => (2**(1+2)))
204
+ end
205
+ it "should retreive 04/12/2013" do
206
+ # 04/15/2013 is monday !
207
+ subject.periods_min_date.should be_nil
208
+ end
209
+ end
210
+ end
211
+ end
212
+ describe "#periods.build" do
213
+ it "should add a new instance of period, and periods_max_date should not raise error" do
214
+ period = subject.periods.build
215
+ subject.periods_max_date
216
+ period.period_start.should be_nil
217
+ period.period_end.should be_nil
218
+ end
219
+ end
220
+ describe "#periods" do
221
+ context "when a period is added," do
222
+ before(:each) do
223
+ subject.periods << Chouette::TimeTablePeriod.new( :period_start => (subject.bounding_dates.min - 1), :period_end => (subject.bounding_dates.max + 1))
224
+ subject.save
225
+ end
226
+ it "should update shortcut" do
227
+ subject.start_date.should == subject.bounding_dates.min
228
+ subject.end_date.should == subject.bounding_dates.max
229
+ end
230
+ end
231
+ context "when a period is removed," do
232
+ before(:each) do
233
+ subject.dates = []
234
+ subject.periods = []
235
+ subject.periods << Chouette::TimeTablePeriod.new(
236
+ :period_start => 4.days.since.to_date,
237
+ :period_end => 6.days.since.to_date)
238
+ subject.periods << Chouette::TimeTablePeriod.new(
239
+ :period_start => 1.days.since.to_date,
240
+ :period_end => 10.days.since.to_date)
241
+ subject.save
242
+ subject.periods = subject.periods - [subject.periods.last]
243
+ subject.save_shortcuts
244
+ end
245
+ def read_tm
246
+ Chouette::TimeTable.find subject.id
247
+ end
248
+ it "should update shortcut" do
249
+ tm = read_tm
250
+ subject.start_date.should == subject.bounding_dates.min
251
+ subject.start_date.should == tm.bounding_dates.min
252
+ subject.start_date.should == 4.days.since.to_date
253
+ subject.end_date.should == subject.bounding_dates.max
254
+ subject.end_date.should == tm.bounding_dates.max
255
+ subject.end_date.should == 6.days.since.to_date
256
+ end
257
+ end
258
+ context "when a period is updated," do
259
+ before(:each) do
260
+ subject.dates = []
261
+ subject.periods = []
262
+ subject.periods << Chouette::TimeTablePeriod.new(
263
+ :period_start => 4.days.since.to_date,
264
+ :period_end => 6.days.since.to_date)
265
+ subject.periods << Chouette::TimeTablePeriod.new(
266
+ :period_start => 1.days.since.to_date,
267
+ :period_end => 10.days.since.to_date)
268
+ subject.save
269
+ subject.periods.last.period_end = 15.days.since.to_date
270
+ subject.save
271
+ end
272
+ def read_tm
273
+ Chouette::TimeTable.find subject.id
274
+ end
275
+ it "should update shortcut" do
276
+ tm = read_tm
277
+ subject.start_date.should == subject.bounding_dates.min
278
+ subject.start_date.should == tm.bounding_dates.min
279
+ subject.start_date.should == 1.days.since.to_date
280
+ subject.end_date.should == subject.bounding_dates.max
281
+ subject.end_date.should == tm.bounding_dates.max
282
+ subject.end_date.should == 15.days.since.to_date
283
+ end
284
+ end
285
+
286
+ end
287
+ describe "#periods.valid?" do
288
+ context "when an empty period is set," do
289
+ it "should not save tm if period invalid" do
290
+ subject = Chouette::TimeTable.new({"comment"=>"test",
291
+ "version"=>"",
292
+ "monday"=>"0",
293
+ "tuesday"=>"0",
294
+ "wednesday"=>"0",
295
+ "thursday"=>"0",
296
+ "friday"=>"0",
297
+ "saturday"=>"0",
298
+ "sunday"=>"0",
299
+ "objectid"=>"",
300
+ "periods_attributes"=>{"1397136188334"=>{"period_start"=>"",
301
+ "period_end"=>"",
302
+ "_destroy"=>""}}})
303
+ subject.save
304
+ subject.id.should be_nil
305
+ end
306
+ end
307
+ context "when a valid period is set," do
308
+ it "it should save tm if period valid" do
309
+ subject = Chouette::TimeTable.new({"comment"=>"test",
310
+ "version"=>"",
311
+ "monday"=>"1",
312
+ "tuesday"=>"1",
313
+ "wednesday"=>"1",
314
+ "thursday"=>"1",
315
+ "friday"=>"1",
316
+ "saturday"=>"1",
317
+ "sunday"=>"1",
318
+ "objectid"=>"",
319
+ "periods_attributes"=>{"1397136188334"=>{"period_start"=>"2014-01-01",
320
+ "period_end"=>"2015-01-01",
321
+ "_destroy"=>""}}})
322
+ subject.save
323
+ tm = Chouette::TimeTable.find subject.id
324
+ tm.periods.empty?.should be_false
325
+ tm.start_date.should == Date.new(2014, 01, 01)
326
+ tm.end_date.should == Date.new(2015, 01, 01)
327
+
328
+ end
329
+ end
330
+ end
331
+
332
+ describe "#dates" do
333
+ context "when a date is added," do
334
+ before(:each) do
335
+ subject.dates << Chouette::TimeTableDate.new( :date => (subject.bounding_dates.max + 1), :in_out => true)
336
+ subject.save
337
+ end
338
+ it "should update shortcut" do
339
+ subject.start_date.should == subject.bounding_dates.min
340
+ subject.end_date.should == subject.bounding_dates.max
341
+ end
342
+ end
343
+ context "when a date is removed," do
344
+ before(:each) do
345
+ subject.periods = []
346
+ subject.dates = subject.dates - [subject.bounding_dates.max + 1]
347
+ subject.save_shortcuts
348
+ end
349
+ it "should update shortcut" do
350
+ subject.start_date.should == subject.bounding_dates.min
351
+ subject.end_date.should == subject.bounding_dates.max
352
+ end
353
+ end
354
+ context "when all the dates and periods are removed," do
355
+ before(:each) do
356
+ subject.periods = []
357
+ subject.dates = []
358
+ subject.save_shortcuts
359
+ end
360
+ it "should update shortcut" do
361
+ subject.start_date.should be_nil
362
+ subject.end_date.should be_nil
363
+ end
364
+ end
365
+ context "when a date is updated," do
366
+ before(:each) do
367
+ subject.dates = []
368
+
369
+ subject.periods = []
370
+ subject.periods << Chouette::TimeTablePeriod.new(
371
+ :period_start => 4.days.since.to_date,
372
+ :period_end => 6.days.since.to_date)
373
+ subject.periods << Chouette::TimeTablePeriod.new(
374
+ :period_start => 1.days.since.to_date,
375
+ :period_end => 10.days.since.to_date)
376
+ subject.dates << Chouette::TimeTableDate.new( :date => 10.days.since.to_date, :in_out => true)
377
+ subject.save
378
+ subject.dates.last.date = 15.days.since.to_date
379
+ subject.save
380
+ end
381
+ def read_tm
382
+ Chouette::TimeTable.find subject.id
383
+ end
384
+ it "should update shortcut" do
385
+ tm = read_tm
386
+ subject.start_date.should == subject.bounding_dates.min
387
+ subject.start_date.should == tm.bounding_dates.min
388
+ subject.start_date.should == 1.days.since.to_date
389
+ subject.end_date.should == subject.bounding_dates.max
390
+ subject.end_date.should == tm.bounding_dates.max
391
+ subject.end_date.should == 15.days.since.to_date
392
+ end
393
+ end
394
+ end
395
+ describe "#dates.valid?" do
396
+ it "should not save tm if date invalid" do
397
+ subject = Chouette::TimeTable.new({"comment"=>"test",
398
+ "version"=>"",
399
+ "monday"=>"0",
400
+ "tuesday"=>"0",
401
+ "wednesday"=>"0",
402
+ "thursday"=>"0",
403
+ "friday"=>"0",
404
+ "saturday"=>"0",
405
+ "sunday"=>"0",
406
+ "objectid"=>"",
407
+ "dates_attributes"=>{"1397136189216"=>{"date"=>"",
408
+ "_destroy"=>"", "in_out" => true}}})
409
+ subject.save
410
+ subject.id.should be_nil
411
+ end
412
+ it "it should save tm if date valid" do
413
+ subject = Chouette::TimeTable.new({"comment"=>"test",
414
+ "version"=>"",
415
+ "monday"=>"1",
416
+ "tuesday"=>"1",
417
+ "wednesday"=>"1",
418
+ "thursday"=>"1",
419
+ "friday"=>"1",
420
+ "saturday"=>"1",
421
+ "sunday"=>"1",
422
+ "objectid"=>"",
423
+ "dates_attributes"=>{"1397136189216"=>{"date"=>"2015-01-01",
424
+ "_destroy"=>"", "in_out" => true}}})
425
+ subject.save
426
+ tm = Chouette::TimeTable.find subject.id
427
+ tm.dates.empty?.should be_false
428
+ tm.start_date.should == Date.new(2015, 01, 01)
429
+ tm.end_date.should == Date.new(2015, 01, 01)
430
+ end
431
+ end
432
+
433
+ describe "#valid_days" do
434
+ it "should begin with position 0" do
435
+ subject.int_day_types = 128
436
+ subject.valid_days.should == [6]
437
+ end
438
+ end
439
+
440
+ describe "#intersects" do
441
+ it "should return day if a date equal day" do
442
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1")
443
+ time_table.dates << Chouette::TimeTableDate.new( :date => Date.today, :in_out => true)
444
+ time_table.intersects([Date.today]).should == [Date.today]
445
+ end
446
+
447
+ it "should return [] if a period not include days" do
448
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 12)
449
+ time_table.periods << Chouette::TimeTablePeriod.new(
450
+ :period_start => Date.new(2013, 05, 27),
451
+ :period_end => Date.new(2013, 05, 30))
452
+ time_table.intersects([ Date.new(2013, 05, 29), Date.new(2013, 05, 30)]).should == []
453
+ end
454
+
455
+ it "should return days if a period include day" do
456
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 12) # Day type monday and tuesday
457
+ time_table.periods << Chouette::TimeTablePeriod.new(
458
+ :period_start => Date.new(2013, 05, 27),
459
+ :period_end => Date.new(2013, 05, 30))
460
+ time_table.intersects([ Date.new(2013, 05, 27), Date.new(2013, 05, 28)]).should == [ Date.new(2013, 05, 27), Date.new(2013, 05, 28)]
461
+ end
462
+
463
+
464
+ end
465
+
466
+ describe "#include_day?" do
467
+ it "should return true if a date equal day" do
468
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1")
469
+ time_table.dates << Chouette::TimeTableDate.new( :date => Date.today, :in_out => true)
470
+ time_table.include_day?(Date.today).should == true
471
+ end
472
+
473
+ it "should return true if a period include day" do
474
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 12) # Day type monday and tuesday
475
+ time_table.periods << Chouette::TimeTablePeriod.new(
476
+ :period_start => Date.new(2013, 05, 27),
477
+ :period_end => Date.new(2013, 05, 29))
478
+ time_table.include_day?( Date.new(2013, 05, 27)).should == true
479
+ end
480
+ end
481
+
482
+ describe "#include_in_dates?" do
483
+ it "should return true if a date equal day" do
484
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1")
485
+ time_table.dates << Chouette::TimeTableDate.new( :date => Date.today, :in_out => true)
486
+ time_table.include_in_dates?(Date.today).should == true
487
+ end
488
+
489
+ it "should return false if a period include day but that is exclued" do
490
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 12) # Day type monday and tuesday
491
+ excluded_date = Date.new(2013, 05, 27)
492
+ time_table.dates << Chouette::TimeTableDate.new( :date => excluded_date, :in_out => false)
493
+ time_table.include_in_dates?( excluded_date).should be_false
494
+ end
495
+ end
496
+
497
+ describe "#include_in_periods?" do
498
+ it "should return true if a period include day" do
499
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 4)
500
+ time_table.periods << Chouette::TimeTablePeriod.new(
501
+ :period_start => Date.new(2012, 1, 1),
502
+ :period_end => Date.new(2012, 01, 30))
503
+ time_table.include_in_periods?(Date.new(2012, 1, 2)).should == true
504
+ end
505
+
506
+ it "should return false if a period include day but that is exclued" do
507
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 12) # Day type monday and tuesday
508
+ excluded_date = Date.new(2013, 05, 27)
509
+ time_table.dates << Chouette::TimeTableDate.new( :date => excluded_date, :in_out => false)
510
+ time_table.periods << Chouette::TimeTablePeriod.new(
511
+ :period_start => Date.new(2013, 05, 27),
512
+ :period_end => Date.new(2013, 05, 29))
513
+ time_table.include_in_periods?( excluded_date).should be_false
514
+ end
515
+ end
516
+
517
+ describe "#include_in_overlap_dates?" do
518
+ it "should return true if a day is included in overlap dates" do
519
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 4)
520
+ time_table.periods << Chouette::TimeTablePeriod.new(
521
+ :period_start => Date.new(2012, 1, 1),
522
+ :period_end => Date.new(2012, 01, 30))
523
+ time_table.dates << Chouette::TimeTableDate.new( :date => Date.new(2012, 1, 2), :in_out => true)
524
+ time_table.include_in_overlap_dates?(Date.new(2012, 1, 2)).should == true
525
+ end
526
+ it "should return false if the day is excluded" do
527
+ time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 4)
528
+ time_table.periods << Chouette::TimeTablePeriod.new(
529
+ :period_start => Date.new(2012, 1, 1),
530
+ :period_end => Date.new(2012, 01, 30))
531
+ time_table.dates << Chouette::TimeTableDate.new( :date => Date.new(2012, 1, 2), :in_out => false)
532
+ time_table.include_in_overlap_dates?(Date.new(2012, 1, 2)).should be_false
533
+ end
534
+ end
535
+
536
+ describe "#dates" do
537
+ it "should have with position 0" do
538
+ subject.dates.first.position.should == 0
539
+ end
540
+ context "when first date has been removed" do
541
+ before do
542
+ subject.dates.first.destroy
543
+ end
544
+ it "should begin with position 0" do
545
+ subject.dates.first.position.should == 0
546
+ end
547
+ end
548
+ end
549
+ describe "#validity_out_between?" do
550
+ let(:empty_tm) {Factory.build(:time_table)}
551
+ it "should be false if empty calendar" do
552
+ empty_tm.validity_out_between?( Date.today, Date.today + 7.day).should be_false
553
+ end
554
+ it "should be true if caldendar is out during start_date and end_date period" do
555
+ start_date = subject.bounding_dates.max - 2.day
556
+ end_date = subject.bounding_dates.max + 2.day
557
+ subject.validity_out_between?( start_date, end_date).should be_true
558
+ end
559
+ it "should be false if calendar is out on start date" do
560
+ start_date = subject.bounding_dates.max
561
+ end_date = subject.bounding_dates.max + 2.day
562
+ subject.validity_out_between?( start_date, end_date).should be_false
563
+ end
564
+ it "should be false if calendar is out on end date" do
565
+ start_date = subject.bounding_dates.max - 2.day
566
+ end_date = subject.bounding_dates.max
567
+ subject.validity_out_between?( start_date, end_date).should be_true
568
+ end
569
+ it "should be false if calendar is out after start_date" do
570
+ start_date = subject.bounding_dates.max + 2.day
571
+ end_date = subject.bounding_dates.max + 4.day
572
+ subject.validity_out_between?( start_date, end_date).should be_false
573
+ end
574
+ end
575
+ describe "#validity_out_from_on?" do
576
+ let(:empty_tm) {Factory.build(:time_table)}
577
+ it "should be false if empty calendar" do
578
+ empty_tm.validity_out_from_on?( Date.today).should be_false
579
+ end
580
+ it "should be true if caldendar ends on expected date" do
581
+ expected_date = subject.bounding_dates.max
582
+ subject.validity_out_from_on?( expected_date).should be_true
583
+ end
584
+ it "should be true if calendar ends before expected date" do
585
+ expected_date = subject.bounding_dates.max + 30.day
586
+ subject.validity_out_from_on?( expected_date).should be_true
587
+ end
588
+ it "should be false if calendars ends after expected date" do
589
+ expected_date = subject.bounding_dates.max - 30.day
590
+ subject.validity_out_from_on?( expected_date).should be_false
591
+ end
592
+ end
593
+ describe "#bounding_dates" do
594
+ context "when timetable contains only periods" do
595
+ before do
596
+ subject.dates = []
597
+ subject.save
598
+ end
599
+ it "should retreive periods.period_start.min and periods.period_end.max" do
600
+ subject.bounding_dates.min.should == subject.periods.map(&:period_start).min
601
+ subject.bounding_dates.max.should == subject.periods.map(&:period_end).max
602
+ end
603
+ end
604
+ context "when timetable contains only dates" do
605
+ before do
606
+ subject.periods = []
607
+ subject.save
608
+ end
609
+ it "should retreive dates.min and dates.max" do
610
+ subject.bounding_dates.min.should == subject.dates.map(&:date).min
611
+ subject.bounding_dates.max.should == subject.dates.map(&:date).max
612
+ end
613
+ end
614
+ it "should contains min date" do
615
+ min_date = subject.bounding_dates.min
616
+ subject.dates.each do |tm_date|
617
+ (min_date <= tm_date.date).should be_true
618
+ end
619
+ subject.periods.each do |tm_period|
620
+ (min_date <= tm_period.period_start).should be_true
621
+ end
622
+
623
+ end
624
+ it "should contains max date" do
625
+ max_date = subject.bounding_dates.max
626
+ subject.dates.each do |tm_date|
627
+ (tm_date.date <= max_date).should be_true
628
+ end
629
+ subject.periods.each do |tm_period|
630
+ (tm_period.period_end <= max_date).should be_true
631
+ end
632
+
633
+ end
634
+ end
635
+ describe "#periods" do
636
+ it "should begin with position 0" do
637
+ subject.periods.first.position.should == 0
638
+ end
639
+ context "when first period has been removed" do
640
+ before do
641
+ subject.periods.first.destroy
642
+ end
643
+ it "should begin with position 0" do
644
+ subject.periods.first.position.should == 0
645
+ end
646
+ end
647
+ it "should have period_start before period_end" do
648
+ period = Chouette::TimeTablePeriod.new
649
+ period.period_start = Date.today
650
+ period.period_end = Date.today + 10
651
+ period.valid?.should be_true
652
+ end
653
+ it "should not have period_start after period_end" do
654
+ period = Chouette::TimeTablePeriod.new
655
+ period.period_start = Date.today
656
+ period.period_end = Date.today - 10
657
+ period.valid?.should be_false
658
+ end
659
+ it "should not have period_start equal to period_end" do
660
+ period = Chouette::TimeTablePeriod.new
661
+ period.period_start = Date.today
662
+ period.period_end = Date.today
663
+ period.valid?.should be_false
664
+ end
665
+ end
666
+
667
+ describe "#effective_days_of_periods" do
668
+ before do
669
+ subject.periods.clear
670
+ subject.periods << Chouette::TimeTablePeriod.new(
671
+ :period_start => Date.new(2014, 6, 30),
672
+ :period_end => Date.new(2014, 7, 6))
673
+ subject.int_day_types = 4|8|16
674
+ end
675
+ it "should return monday to wednesday" do
676
+ subject.effective_days_of_periods.size.should == 3
677
+ subject.effective_days_of_periods[0].should == Date.new(2014, 6, 30)
678
+ subject.effective_days_of_periods[1].should == Date.new(2014, 7, 1)
679
+ subject.effective_days_of_periods[2].should == Date.new(2014, 7, 2)
680
+ end
681
+ it "should return thursday" do
682
+ subject.effective_days_of_periods(Chouette::TimeTable.valid_days(32)).size.should == 1
683
+ subject.effective_days_of_periods(Chouette::TimeTable.valid_days(32))[0].should == Date.new(2014, 7, 3)
684
+ end
685
+
686
+ end
687
+
688
+ describe "#included_days" do
689
+ before do
690
+ subject.dates.clear
691
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
692
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => false)
693
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
694
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,19), :in_out => false)
695
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
696
+ end
697
+ it "should return 3 dates" do
698
+ days = subject.included_days
699
+ days.size.should == 3
700
+ days[0].should == Date.new(2014, 7, 16)
701
+ days[1].should == Date.new(2014,7, 18)
702
+ days[2].should == Date.new(2014, 7,20)
703
+ end
704
+ end
705
+
706
+
707
+
708
+ describe "#excluded_days" do
709
+ before do
710
+ subject.dates.clear
711
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
712
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => false)
713
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
714
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,19), :in_out => false)
715
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
716
+ end
717
+ it "should return 3 dates" do
718
+ days = subject.excluded_days
719
+ days.size.should == 2
720
+ days[0].should == Date.new(2014, 7, 17)
721
+ days[1].should == Date.new(2014,7, 19)
722
+ end
723
+ end
724
+
725
+
726
+
727
+ describe "#effective_days" do
728
+ before do
729
+ subject.periods.clear
730
+ subject.periods << Chouette::TimeTablePeriod.new(
731
+ :period_start => Date.new(2014, 6, 30),
732
+ :period_end => Date.new(2014, 7, 6))
733
+ subject.int_day_types = 4|8|16
734
+ subject.dates.clear
735
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,1), :in_out => false)
736
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
737
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
738
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
739
+ end
740
+ it "should return 5 dates" do
741
+ days = subject.effective_days
742
+ days.size.should == 5
743
+ days[0].should == Date.new(2014, 6, 30)
744
+ days[1].should == Date.new(2014, 7, 2)
745
+ days[2].should == Date.new(2014, 7, 16)
746
+ days[3].should == Date.new(2014, 7, 18)
747
+ days[4].should == Date.new(2014, 7, 20)
748
+ end
749
+ end
750
+
751
+
752
+
753
+ describe "#optimize_periods" do
754
+ before do
755
+ subject.periods.clear
756
+ subject.periods << Chouette::TimeTablePeriod.new(
757
+ :period_start => Date.new(2014, 6, 30),
758
+ :period_end => Date.new(2014, 7, 6))
759
+ subject.periods << Chouette::TimeTablePeriod.new(
760
+ :period_start => Date.new(2014, 7, 6),
761
+ :period_end => Date.new(2014, 7, 14))
762
+ subject.periods << Chouette::TimeTablePeriod.new(
763
+ :period_start => Date.new(2014, 6, 1),
764
+ :period_end => Date.new(2014, 6, 14))
765
+ subject.periods << Chouette::TimeTablePeriod.new(
766
+ :period_start => Date.new(2014, 6, 3),
767
+ :period_end => Date.new(2014, 6, 4))
768
+ subject.int_day_types = 4|8|16
769
+ end
770
+ it "should return 2 ordered periods" do
771
+ periods = subject.optimize_periods
772
+ periods.size.should == 2
773
+ periods[0].period_start.should == Date.new(2014, 6, 1)
774
+ periods[0].period_end.should == Date.new(2014, 6, 14)
775
+ periods[1].period_start.should == Date.new(2014, 6, 30)
776
+ periods[1].period_end.should == Date.new(2014, 7, 14)
777
+ end
778
+ end
779
+
780
+ describe "#add_included_day" do
781
+ before do
782
+ subject.dates.clear
783
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
784
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => false)
785
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
786
+ end
787
+ it "should do nothing" do
788
+ subject.add_included_day(Date.new(2014,7,16))
789
+ days = subject.included_days
790
+ days.size.should == 2
791
+ days.include?(Date.new(2014,7,16)).should be_true
792
+ days.include?(Date.new(2014,7,18)).should be_false
793
+ days.include?(Date.new(2014,7,20)).should be_true
794
+ end
795
+ it "should switch in_out flag" do
796
+ subject.add_included_day(Date.new(2014,7,18))
797
+ days = subject.included_days
798
+ days.size.should == 3
799
+ days.include?(Date.new(2014,7,16)).should be_true
800
+ days.include?(Date.new(2014,7,18)).should be_true
801
+ days.include?(Date.new(2014,7,20)).should be_true
802
+ end
803
+ it "should add date" do
804
+ subject.add_included_day(Date.new(2014,7,21))
805
+ days = subject.included_days
806
+ days.size.should == 3
807
+ days.include?(Date.new(2014,7,16)).should be_true
808
+ days.include?(Date.new(2014,7,20)).should be_true
809
+ days.include?(Date.new(2014,7,21)).should be_true
810
+ end
811
+ end
812
+
813
+
814
+ describe "#merge!" do
815
+ context "timetables have periods with common day_types " do
816
+ before do
817
+ subject.periods.clear
818
+ subject.dates.clear
819
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,5))
820
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,6))
821
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
822
+ subject.int_day_types = 4|16|32|128
823
+ another_tt = Factory(:time_table , :int_day_types => (4|16|64|128) )
824
+ another_tt.periods.clear
825
+ another_tt.dates.clear
826
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,5), :period_end => Date.new(2014,8,12))
827
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,15), :period_end => Date.new(2014,7,25))
828
+ subject.merge! another_tt
829
+ subject.reload
830
+ end
831
+ it "should have merged periods" do
832
+ subject.periods.size.should == 3
833
+ subject.periods[0].period_start.should == Date.new(2014, 6, 30)
834
+ subject.periods[0].period_end.should == Date.new(2014, 7, 6)
835
+ subject.periods[1].period_start.should == Date.new(2014, 7, 15)
836
+ subject.periods[1].period_end.should == Date.new(2014, 7, 25)
837
+ subject.periods[2].period_start.should == Date.new(2014, 8, 1)
838
+ subject.periods[2].period_end.should == Date.new(2014, 8, 12)
839
+ end
840
+ it "should have common day_types" do
841
+ subject.int_day_types.should == 4|16|128
842
+ end
843
+ it "should have dates for thursdays and fridays" do
844
+ subject.dates.size.should == 4
845
+ subject.dates[0].date.should == Date.new(2014,7,3)
846
+ subject.dates[1].date.should == Date.new(2014,7,18)
847
+ subject.dates[2].date.should == Date.new(2014,7,25)
848
+ subject.dates[3].date.should == Date.new(2014,8,8)
849
+ end
850
+ end
851
+
852
+ end
853
+
854
+ describe "#intersect!" do
855
+ context "timetables have periods with common day_types " do
856
+ before do
857
+ subject.periods.clear
858
+ subject.dates.clear
859
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,6))
860
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,20))
861
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
862
+ subject.int_day_types = 4|16|32|128
863
+ another_tt = Factory(:time_table , :int_day_types => (4|16|64|128) )
864
+ another_tt.periods.clear
865
+ another_tt.dates.clear
866
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,6), :period_end => Date.new(2014,8,12))
867
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,15), :period_end => Date.new(2014,7,25))
868
+ subject.intersect! another_tt
869
+ subject.reload
870
+ end
871
+ it "should have 1 common period" do
872
+ subject.periods.size.should == 1
873
+ subject.periods[0].period_start.should == Date.new(2014, 7, 15)
874
+ subject.periods[0].period_end.should == Date.new(2014, 7, 20)
875
+ end
876
+ it "should have common day_types" do
877
+ subject.int_day_types.should == 4|16|128
878
+ end
879
+ it "should have date for period reduced to one day" do
880
+ subject.dates.size.should == 1
881
+ subject.dates[0].date.should == Date.new(2014,8,6)
882
+ end
883
+ end
884
+ context "timetables have periods or dates " do
885
+ before do
886
+ subject.periods.clear
887
+ subject.dates.clear
888
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
889
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => true)
890
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
891
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,19), :in_out => true)
892
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
893
+ subject.int_day_types = 0
894
+ another_tt = Factory(:time_table , :int_day_types => (4|16|64|128) )
895
+ another_tt.periods.clear
896
+ another_tt.dates.clear
897
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,6), :period_end => Date.new(2014,8,12))
898
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,17), :period_end => Date.new(2014,7,25))
899
+ subject.intersect! another_tt
900
+ subject.reload
901
+ end
902
+ it "should have 0 period" do
903
+ subject.periods.size.should == 0
904
+ end
905
+ it "should have merges special flags" do
906
+ subject.int_day_types.should == 0
907
+ end
908
+ it "should have date reduced for period" do
909
+ subject.dates.size.should == 2
910
+ subject.dates[0].date.should == Date.new(2014,7,18)
911
+ subject.dates[1].date.should == Date.new(2014,7,19)
912
+ end
913
+ end
914
+ context "with only periods : intersect timetable have no one day period" do
915
+ before do
916
+ subject.periods.clear
917
+ subject.dates.clear
918
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,6))
919
+ subject.int_day_types = 4|8|16
920
+ another_tt = Factory(:time_table , :int_day_types => (4|8|16) )
921
+ another_tt.periods.clear
922
+ another_tt.dates.clear
923
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,6), :period_end => Date.new(2014,8,12))
924
+ subject.intersect! another_tt
925
+ subject.reload
926
+ end
927
+ it "should have 0 result periods" do
928
+ subject.periods.size.should == 0
929
+ end
930
+ it "should have no day_types" do
931
+ subject.int_day_types.should == 0
932
+ end
933
+ it "should have 1 date " do
934
+ subject.dates.size.should == 1
935
+ subject.dates[0].date.should == Date.new(2014,8,6)
936
+ end
937
+ end
938
+
939
+ end
940
+
941
+ describe "#disjoin!" do
942
+ context "timetables have periods with common day_types " do
943
+ before do
944
+ subject.periods.clear
945
+ subject.dates.clear
946
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,6))
947
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,20))
948
+ subject.int_day_types = 4|16|32|128
949
+ another_tt = Factory(:time_table , :int_day_types => (4|16|64|128) )
950
+ another_tt.periods.clear
951
+ another_tt.dates.clear
952
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,6), :period_end => Date.new(2014,8,12))
953
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,15), :period_end => Date.new(2014,8,2))
954
+ subject.disjoin! another_tt
955
+ subject.reload
956
+ end
957
+ it "should have 2 result periods" do
958
+ subject.periods.size.should == 2
959
+ subject.periods[0].period_start.should == Date.new(2014, 6, 30)
960
+ subject.periods[0].period_end.should == Date.new(2014, 7, 14)
961
+ subject.periods[1].period_start.should == Date.new(2014, 8, 3)
962
+ subject.periods[1].period_end.should == Date.new(2014, 8, 5)
963
+ end
964
+ it "should have remained day_types" do
965
+ subject.int_day_types.should == 4|16|32|128
966
+ end
967
+ it "should have dates for period reduced" do
968
+ subject.dates.size.should == 1
969
+ subject.dates[0].date.should == Date.new(2014,7,17)
970
+ end
971
+ end
972
+ context "timetables have periods or dates " do
973
+ before do
974
+ subject.periods.clear
975
+ subject.dates.clear
976
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
977
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => true)
978
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
979
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,19), :in_out => true)
980
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
981
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,6), :in_out => true)
982
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,7), :in_out => true)
983
+ subject.int_day_types = 0
984
+ another_tt = Factory(:time_table , :int_day_types => (4|16|64|128) )
985
+ another_tt.periods.clear
986
+ another_tt.dates.clear
987
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,6), :period_end => Date.new(2014,8,12))
988
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,17), :period_end => Date.new(2014,7,25))
989
+ subject.disjoin! another_tt
990
+ subject.reload
991
+ end
992
+ it "should have 0 period" do
993
+ subject.periods.size.should == 0
994
+ end
995
+ it "should have no remained day_types" do
996
+ subject.int_day_types.should == 0
997
+ end
998
+ it "should have date reduced for period" do
999
+ subject.dates.size.should == 4
1000
+ subject.dates[0].date.should == Date.new(2014,7,16)
1001
+ subject.dates[1].date.should == Date.new(2014,7,17)
1002
+ subject.dates[2].date.should == Date.new(2014,7,20)
1003
+ subject.dates[3].date.should == Date.new(2014,8,7)
1004
+ end
1005
+ end
1006
+ context "disjoined timetable have all periods in removed ones " do
1007
+ before do
1008
+ subject.periods.clear
1009
+ subject.dates.clear
1010
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,8))
1011
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,20))
1012
+ subject.int_day_types = 4|16|32|128
1013
+ another_tt = Factory(:time_table , :int_day_types => (4|16|64|128) )
1014
+ another_tt.periods.clear
1015
+ another_tt.dates.clear
1016
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,31), :period_end => Date.new(2014,8,12))
1017
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,20))
1018
+ subject.disjoin! another_tt
1019
+ subject.reload
1020
+ end
1021
+ it "should have 0 result periods" do
1022
+ subject.periods.size.should == 0
1023
+ end
1024
+ it "should have no remained day_types" do
1025
+ subject.int_day_types.should == 0
1026
+ end
1027
+ it "should have dates for period reduced" do
1028
+ subject.dates.size.should == 4
1029
+ subject.dates[0].date.should == Date.new(2014,7,3)
1030
+ subject.dates[1].date.should == Date.new(2014,7,10)
1031
+ subject.dates[2].date.should == Date.new(2014,7,17)
1032
+ subject.dates[3].date.should == Date.new(2014,8,7)
1033
+ end
1034
+ end
1035
+
1036
+ context "timetable with dates against timetable with dates and periods" do
1037
+ before do
1038
+ subject.periods.clear
1039
+ subject.dates.clear
1040
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
1041
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => true)
1042
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
1043
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,19), :in_out => true)
1044
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
1045
+ subject.int_day_types = 0
1046
+ another_tt = Factory(:time_table , :int_day_types => (4|16|64|128) )
1047
+ another_tt.periods.clear
1048
+ another_tt.dates.clear
1049
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,31), :period_end => Date.new(2014,8,12))
1050
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => true)
1051
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
1052
+ subject.disjoin! another_tt
1053
+ subject.reload
1054
+ end
1055
+ it "should have 0 result periods" do
1056
+ subject.periods.size.should == 0
1057
+ end
1058
+ it "should have no remained day_types" do
1059
+ subject.int_day_types == 0
1060
+ end
1061
+ it "should have 3 dates left" do
1062
+ subject.dates.size.should == 3
1063
+ subject.dates[0].date.should == Date.new(2014,7,16)
1064
+ subject.dates[1].date.should == Date.new(2014,7,19)
1065
+ subject.dates[2].date.should == Date.new(2014,7,20)
1066
+ end
1067
+ end
1068
+ context "timetable with dates against timetable with dates and periods all covered" do
1069
+ before do
1070
+ subject.periods.clear
1071
+ subject.dates.clear
1072
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,1), :in_out => true)
1073
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,2), :in_out => true)
1074
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,5), :in_out => true)
1075
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,6), :in_out => true)
1076
+ subject.int_day_types = 512
1077
+ another_tt = Factory(:time_table , :int_day_types => (32|64|512) )
1078
+ another_tt.periods.clear
1079
+ another_tt.dates.clear
1080
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,11))
1081
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,1), :in_out => true)
1082
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,2), :in_out => true)
1083
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,5), :in_out => true)
1084
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,6), :in_out => true)
1085
+ subject.disjoin! another_tt
1086
+ subject.reload
1087
+ end
1088
+ it "should have 0 result periods" do
1089
+ subject.periods.size.should == 0
1090
+ end
1091
+ it "should have no remained day_types" do
1092
+ subject.int_day_types == 0
1093
+ end
1094
+ it "should have 0 dates left" do
1095
+ subject.dates.size.should == 0
1096
+ end
1097
+ end
1098
+
1099
+ context "with only periods : disjoined timetable have no empty period" do
1100
+ before do
1101
+ subject.periods.clear
1102
+ subject.dates.clear
1103
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,8))
1104
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,10), :period_end => Date.new(2014,8,31))
1105
+ subject.int_day_types = 4|8
1106
+ another_tt = Factory(:time_table , :int_day_types => (4|8) )
1107
+ another_tt.periods.clear
1108
+ another_tt.dates.clear
1109
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,4), :period_end => Date.new(2014,8,7))
1110
+ subject.disjoin! another_tt
1111
+ subject.reload
1112
+ end
1113
+ it "should have 1 result periods" do
1114
+ subject.periods.size.should == 1
1115
+ subject.periods[0].period_start.should == Date.new(2014,8,10)
1116
+ subject.periods[0].period_end.should == Date.new(2014,8,31)
1117
+ end
1118
+ it "should have same day_types" do
1119
+ subject.int_day_types.should == 4|8
1120
+ end
1121
+ it "should have no dates " do
1122
+ subject.dates.size.should == 0
1123
+ end
1124
+ end
1125
+
1126
+ context "with only periods : disjoined timetable have no one day period" do
1127
+ before do
1128
+ subject.periods.clear
1129
+ subject.dates.clear
1130
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,6))
1131
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,10), :period_end => Date.new(2014,8,31))
1132
+ subject.int_day_types = 4|8|16
1133
+ another_tt = Factory(:time_table , :int_day_types => (4|8) )
1134
+ another_tt.periods.clear
1135
+ another_tt.dates.clear
1136
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,4), :period_end => Date.new(2014,8,5))
1137
+ subject.disjoin! another_tt
1138
+ subject.reload
1139
+ end
1140
+ it "should have 1 result periods" do
1141
+ subject.periods.size.should == 1
1142
+ subject.periods[0].period_start.should == Date.new(2014,8,10)
1143
+ subject.periods[0].period_end.should == Date.new(2014,8,31)
1144
+ end
1145
+ it "should have same day_types" do
1146
+ subject.int_day_types.should == 4|8|16
1147
+ end
1148
+ it "should have 1 date " do
1149
+ subject.dates.size.should == 1
1150
+ subject.dates[0].date.should == Date.new(2014,8,6)
1151
+ end
1152
+ end
1153
+
1154
+ context "with periods against dates: disjoined timetable have no unused excluded date" do
1155
+ before do
1156
+ subject.periods.clear
1157
+ subject.dates.clear
1158
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,8))
1159
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,10), :period_end => Date.new(2014,8,31))
1160
+ subject.int_day_types = 4|8|16
1161
+ another_tt = Factory(:time_table , :int_day_types => (0) )
1162
+ another_tt.periods.clear
1163
+ another_tt.dates.clear
1164
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,4), :in_out => true)
1165
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,5), :in_out => true)
1166
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,7), :in_out => true)
1167
+ subject.disjoin! another_tt
1168
+ subject.reload
1169
+ end
1170
+ it "should have same 2 result periods" do
1171
+ subject.periods.size.should == 2
1172
+ subject.periods[0].period_start.should == Date.new(2014,8,1)
1173
+ subject.periods[0].period_end.should == Date.new(2014,8,8)
1174
+ subject.periods[1].period_start.should == Date.new(2014,8,10)
1175
+ subject.periods[1].period_end.should == Date.new(2014,8,31)
1176
+ end
1177
+ it "should have same day_types" do
1178
+ subject.int_day_types.should == 4|8|16
1179
+ end
1180
+ it "should have only 2 excluded dates " do
1181
+ subject.included_days.size.should == 0
1182
+ subject.excluded_days.size.should == 2
1183
+ subject.excluded_days[0].should == Date.new(2014,8,4)
1184
+ subject.excluded_days[1].should == Date.new(2014,8,5)
1185
+ end
1186
+ end
1187
+
1188
+
1189
+ end
1190
+ describe "#duplicate" do
1191
+ it "should be a copy of" do
1192
+ target=subject.duplicate
1193
+ target.id.should be_nil
1194
+ target.comment.should == "Copy of "+subject.comment
1195
+ target.objectid.should == subject.objectid+"_1"
1196
+ target.int_day_types.should == subject.int_day_types
1197
+ target.dates.size.should == subject.dates.size
1198
+ target.dates.each do |d|
1199
+ d.time_table_id.should be_nil
1200
+ end
1201
+ target.periods.size.should == subject.periods.size
1202
+ target.periods.each do |p|
1203
+ p.time_table_id.should be_nil
1204
+ end
1205
+ end
1206
+ end
1207
+
1208
+ describe "#tags" do
1209
+ it "should accept tags" do
1210
+ subject.tag_list = "toto, titi"
1211
+ subject.save
1212
+ subject.reload
1213
+ Chouette::TimeTable.tag_counts.size.should == 2
1214
+ subject.tag_list.size.should == 2
1215
+ end
1216
+ end
1217
+
1218
+ end