ninoxe 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/app/models/chouette/vehicle_journey_at_stop.rb +5 -5
  3. data/lib/ninoxe/version.rb +1 -1
  4. data/spec/dummy/README.rdoc +28 -0
  5. data/spec/dummy/Rakefile +6 -0
  6. data/spec/dummy/app/assets/images/.keep +0 -0
  7. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  8. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  9. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  10. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  11. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  12. data/spec/dummy/app/mailers/.keep +0 -0
  13. data/spec/dummy/app/models/.keep +0 -0
  14. data/spec/dummy/app/models/concerns/.keep +0 -0
  15. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  16. data/spec/dummy/bin/bundle +3 -0
  17. data/spec/dummy/bin/rails +4 -0
  18. data/spec/dummy/bin/rake +4 -0
  19. data/spec/dummy/config.ru +4 -0
  20. data/spec/dummy/config/application.rb +28 -0
  21. data/spec/dummy/config/boot.rb +5 -0
  22. data/spec/dummy/config/database.yml +19 -0
  23. data/spec/dummy/config/environment.rb +5 -0
  24. data/spec/dummy/config/environments/development.rb +29 -0
  25. data/spec/dummy/config/environments/production.rb +80 -0
  26. data/spec/dummy/config/environments/test.rb +36 -0
  27. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  28. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  29. data/spec/dummy/config/initializers/inflections.rb +16 -0
  30. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  31. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  32. data/spec/dummy/config/initializers/session_store.rb +3 -0
  33. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  34. data/spec/dummy/config/locales/en.yml +23 -0
  35. data/spec/dummy/config/locales/fr.yml +23 -0
  36. data/spec/dummy/config/routes.rb +4 -0
  37. data/spec/dummy/db/schema.rb +490 -0
  38. data/spec/dummy/db/test.sqlite3 +0 -0
  39. data/spec/dummy/lib/assets/.keep +0 -0
  40. data/spec/dummy/log/.keep +0 -0
  41. data/spec/dummy/public/404.html +58 -0
  42. data/spec/dummy/public/422.html +58 -0
  43. data/spec/dummy/public/500.html +57 -0
  44. data/spec/dummy/public/favicon.ico +0 -0
  45. data/spec/dummy/script/rails +6 -0
  46. data/spec/factories/chouette_access_links.rb +13 -0
  47. data/spec/factories/chouette_access_points.rb +12 -0
  48. data/spec/factories/chouette_companies.rb +9 -0
  49. data/spec/factories/chouette_connection_links.rb +13 -0
  50. data/spec/factories/chouette_footnotes.rb +10 -0
  51. data/spec/factories/chouette_group_of_lines.rb +9 -0
  52. data/spec/factories/chouette_journey_pattern.rb +40 -0
  53. data/spec/factories/chouette_lines.rb +44 -0
  54. data/spec/factories/chouette_networks.rb +9 -0
  55. data/spec/factories/chouette_routes.rb +26 -0
  56. data/spec/factories/chouette_stop_areas.rb +12 -0
  57. data/spec/factories/chouette_stop_points.rb +10 -0
  58. data/spec/factories/chouette_time_table.rb +37 -0
  59. data/spec/factories/chouette_vehicle_journey.rb +66 -0
  60. data/spec/factories/chouette_vehicle_journey_at_stop.rb +8 -0
  61. data/spec/models/chouette/access_link_spec.rb +82 -0
  62. data/spec/models/chouette/access_point_spec.rb +269 -0
  63. data/spec/models/chouette/active_record_spec.rb +121 -0
  64. data/spec/models/chouette/area_type_spec.rb +53 -0
  65. data/spec/models/chouette/company_spec.rb +48 -0
  66. data/spec/models/chouette/connection_link_spec.rb +60 -0
  67. data/spec/models/chouette/direction_spec.rb +60 -0
  68. data/spec/models/chouette/exporter_spec.rb +28 -0
  69. data/spec/models/chouette/file_validator_spec.rb +28 -0
  70. data/spec/models/chouette/footnote_spec.rb +9 -0
  71. data/spec/models/chouette/group_of_line_spec.rb +31 -0
  72. data/spec/models/chouette/journey_pattern_spec.rb +64 -0
  73. data/spec/models/chouette/line_spec.rb +120 -0
  74. data/spec/models/chouette/loader_spec.rb +69 -0
  75. data/spec/models/chouette/network_spec.rb +19 -0
  76. data/spec/models/chouette/object_id_spec.rb +149 -0
  77. data/spec/models/chouette/route_spec.rb +238 -0
  78. data/spec/models/chouette/stop_area_spec.rb +443 -0
  79. data/spec/models/chouette/stop_point_spec.rb +41 -0
  80. data/spec/models/chouette/time_table_period_spec.rb +66 -0
  81. data/spec/models/chouette/time_table_spec.rb +1266 -0
  82. data/spec/models/chouette/transport_mode_spec.rb +64 -0
  83. data/spec/models/chouette/trident_active_record_spec.rb +115 -0
  84. data/spec/models/chouette/vehicle_journey_at_stop_spec.rb +47 -0
  85. data/spec/models/chouette/vehicle_journey_spec.rb +223 -0
  86. data/spec/presenters/chouette/geometry/general_presenter.rb +1 -0
  87. data/spec/presenters/chouette/geometry/line_presenter_spec.rb +13 -0
  88. data/spec/spec_helper.rb +58 -0
  89. metadata +178 -10
  90. data/config/database.yml.me +0 -11
  91. data/lib/ninoxe.rb~ +0 -7
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chouette::TimeTablePeriod, :type => :model do
4
+
5
+ let!(:time_table) { create(:time_table)}
6
+ subject { create(:time_table_period ,:time_table => time_table, :period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,6) ) }
7
+ let!(:p2) {create(:time_table_period ,:time_table => time_table, :period_start => Date.new(2014,7,6), :period_end => Date.new(2014,7,14) ) }
8
+
9
+ it { is_expected.to validate_presence_of :period_start }
10
+ it { is_expected.to validate_presence_of :period_end }
11
+
12
+ describe "#overlap" do
13
+ context "when periods intersect, " do
14
+ it "should detect period overlap" do
15
+ expect(subject.overlap?(p2)).to be_truthy
16
+ expect(p2.overlap?(subject)).to be_truthy
17
+ end
18
+ end
19
+ context "when periods don't intersect, " do
20
+ before(:each) do
21
+ p2.period_start = Date.new(2014,7,7)
22
+ end
23
+ it "should not detect period overlap" do
24
+ expect(subject.overlap?(p2)).to be_falsey
25
+ expect(p2.overlap?(subject)).to be_falsey
26
+ end
27
+ end
28
+ context "when period 1 contains period 2, " do
29
+ before(:each) do
30
+ p2.period_start = Date.new(2014,7,1)
31
+ p2.period_end = Date.new(2014,7,6)
32
+ end
33
+ it "should detect period overlap" do
34
+ expect(subject.overlap?(p2)).to be_truthy
35
+ expect(p2.overlap?(subject)).to be_truthy
36
+ end
37
+ end
38
+ end
39
+ describe "#contains" do
40
+ context "when periods intersect, " do
41
+ it "should not detect period inclusion" do
42
+ expect(subject.contains?(p2)).to be_falsey
43
+ expect(p2.contains?(subject)).to be_falsey
44
+ end
45
+ end
46
+ context "when periods don't intersect, " do
47
+ before(:each) do
48
+ p2.period_start = Date.new(2014,7,7)
49
+ end
50
+ it "should not detect period inclusion" do
51
+ expect(subject.contains?(p2)).to be_falsey
52
+ expect(p2.contains?(subject)).to be_falsey
53
+ end
54
+ end
55
+ context "when period 1 contains period 2, " do
56
+ before(:each) do
57
+ p2.period_start = Date.new(2014,7,1)
58
+ p2.period_end = Date.new(2014,7,6)
59
+ end
60
+ it "should detect period inclusion" do
61
+ expect(subject.contains?(p2)).to be_truthy
62
+ expect(p2.contains?(subject)).to be_falsey
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,1266 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chouette::TimeTable, :type => :model do
4
+
5
+ subject { create(:time_table) }
6
+
7
+ it { is_expected.to validate_presence_of :comment }
8
+ it { is_expected.to 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
+ expect(subject.periods_max_date).to eq(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
+ expect(subject.periods_max_date).to eq(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
+ expect(subject.periods_max_date).to eq(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
+ expect(subject.periods_max_date).to eq(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
+ expect(subject.periods_max_date).to 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
+ expect(read.start_date).to eq(read.dates.select{|d| d.in_out}.map(&:date).compact.min)
74
+ expect(read.end_date).to eq(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
+ expect(read.start_date).to eq(new_start_date)
91
+ expect(read.end_date).to eq(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
+ expect(read.end_date).to eq(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
+ expect(read.start_date).to eq(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
+ expect(read.start_date).to eq(new_start_date)
141
+ expect(read.end_date).to eq(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
+ expect(subject.periods_min_date).to eq(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
+ expect(subject.periods_min_date).to eq(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
+ expect(subject.periods_min_date).to eq(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
+ expect(subject.periods_min_date).to eq(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
+ expect(subject.periods_min_date).to 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
+ expect(period.period_start).to be_nil
217
+ expect(period.period_end).to 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
+ expect(subject.start_date).to eq(subject.bounding_dates.min)
228
+ expect(subject.end_date).to eq(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
+ expect(subject.start_date).to eq(subject.bounding_dates.min)
251
+ expect(subject.start_date).to eq(tm.bounding_dates.min)
252
+ expect(subject.start_date).to eq(4.days.since.to_date)
253
+ expect(subject.end_date).to eq(subject.bounding_dates.max)
254
+ expect(subject.end_date).to eq(tm.bounding_dates.max)
255
+ expect(subject.end_date).to eq(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
+ expect(subject.start_date).to eq(subject.bounding_dates.min)
278
+ expect(subject.start_date).to eq(tm.bounding_dates.min)
279
+ expect(subject.start_date).to eq(1.days.since.to_date)
280
+ expect(subject.end_date).to eq(subject.bounding_dates.max)
281
+ expect(subject.end_date).to eq(tm.bounding_dates.max)
282
+ expect(subject.end_date).to eq(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
+ expect(subject.id).to 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
+ expect(tm.periods.empty?).to be_falsey
325
+ expect(tm.start_date).to eq(Date.new(2014, 01, 01))
326
+ expect(tm.end_date).to eq(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
+ expect(subject.start_date).to eq(subject.bounding_dates.min)
340
+ expect(subject.end_date).to eq(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
+ expect(subject.start_date).to eq(subject.bounding_dates.min)
351
+ expect(subject.end_date).to eq(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
+ expect(subject.start_date).to be_nil
362
+ expect(subject.end_date).to 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
+ expect(subject.start_date).to eq(subject.bounding_dates.min)
387
+ expect(subject.start_date).to eq(tm.bounding_dates.min)
388
+ expect(subject.start_date).to eq(1.days.since.to_date)
389
+ expect(subject.end_date).to eq(subject.bounding_dates.max)
390
+ expect(subject.end_date).to eq(tm.bounding_dates.max)
391
+ expect(subject.end_date).to eq(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
+ expect(subject.id).to 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
+ expect(tm.dates.empty?).to be_falsey
428
+ expect(tm.start_date).to eq(Date.new(2015, 01, 01))
429
+ expect(tm.end_date).to eq(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
+ expect(subject.valid_days).to eq([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
+ expect(time_table.intersects([Date.today])).to eq([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
+ expect(time_table.intersects([ Date.new(2013, 05, 29), Date.new(2013, 05, 30)])).to eq([])
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
+ expect(time_table.intersects([ Date.new(2013, 05, 27), Date.new(2013, 05, 28)])).to eq([ 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
+ expect(time_table.include_day?(Date.today)).to eq(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
+ expect(time_table.include_day?( Date.new(2013, 05, 27))).to eq(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
+ expect(time_table.include_in_dates?(Date.today)).to eq(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
+ expect(time_table.include_in_dates?( excluded_date)).to be_falsey
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
+ expect(time_table.include_in_periods?(Date.new(2012, 1, 2))).to eq(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
+ expect(time_table.include_in_periods?( excluded_date)).to be_falsey
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
+ expect(time_table.include_in_overlap_dates?(Date.new(2012, 1, 2))).to eq(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
+ expect(time_table.include_in_overlap_dates?(Date.new(2012, 1, 2))).to be_falsey
533
+ end
534
+ end
535
+
536
+ describe "#dates" do
537
+ it "should have with position 0" do
538
+ expect(subject.dates.first.position).to eq(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
+ expect(subject.dates.first.position).to eq(0)
546
+ end
547
+ end
548
+ end
549
+ describe "#validity_out_between?" do
550
+ let(:empty_tm) {build(:time_table)}
551
+ it "should be false if empty calendar" do
552
+ expect(empty_tm.validity_out_between?( Date.today, Date.today + 7.day)).to be_falsey
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
+ expect(subject.validity_out_between?( start_date, end_date)).to be_truthy
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
+ expect(subject.validity_out_between?( start_date, end_date)).to be_falsey
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
+ expect(subject.validity_out_between?( start_date, end_date)).to be_truthy
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
+ expect(subject.validity_out_between?( start_date, end_date)).to be_falsey
573
+ end
574
+ end
575
+ describe "#validity_out_from_on?" do
576
+ let(:empty_tm) {build(:time_table)}
577
+ it "should be false if empty calendar" do
578
+ expect(empty_tm.validity_out_from_on?( Date.today)).to be_falsey
579
+ end
580
+ it "should be true if caldendar ends on expected date" do
581
+ expected_date = subject.bounding_dates.max
582
+ expect(subject.validity_out_from_on?( expected_date)).to be_truthy
583
+ end
584
+ it "should be true if calendar ends before expected date" do
585
+ expected_date = subject.bounding_dates.max + 30.day
586
+ expect(subject.validity_out_from_on?( expected_date)).to be_truthy
587
+ end
588
+ it "should be false if calendars ends after expected date" do
589
+ expected_date = subject.bounding_dates.max - 30.day
590
+ expect(subject.validity_out_from_on?( expected_date)).to be_falsey
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
+ expect(subject.bounding_dates.min).to eq(subject.periods.map(&:period_start).min)
601
+ expect(subject.bounding_dates.max).to eq(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
+ expect(subject.bounding_dates.min).to eq(subject.dates.map(&:date).min)
611
+ expect(subject.bounding_dates.max).to eq(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
+ expect(min_date <= tm_date.date).to be_truthy
618
+ end
619
+ subject.periods.each do |tm_period|
620
+ expect(min_date <= tm_period.period_start).to be_truthy
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
+ expect(tm_date.date <= max_date).to be_truthy
628
+ end
629
+ subject.periods.each do |tm_period|
630
+ expect(tm_period.period_end <= max_date).to be_truthy
631
+ end
632
+
633
+ end
634
+ end
635
+ describe "#periods" do
636
+ it "should begin with position 0" do
637
+ expect(subject.periods.first.position).to eq(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
+ expect(subject.periods.first.position).to eq(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
+ expect(period.valid?).to be_truthy
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
+ expect(period.valid?).to be_falsey
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
+ expect(period.valid?).to be_falsey
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
+ expect(subject.effective_days_of_periods.size).to eq(3)
677
+ expect(subject.effective_days_of_periods[0]).to eq(Date.new(2014, 6, 30))
678
+ expect(subject.effective_days_of_periods[1]).to eq(Date.new(2014, 7, 1))
679
+ expect(subject.effective_days_of_periods[2]).to eq(Date.new(2014, 7, 2))
680
+ end
681
+ it "should return thursday" do
682
+ expect(subject.effective_days_of_periods(Chouette::TimeTable.valid_days(32)).size).to eq(1)
683
+ expect(subject.effective_days_of_periods(Chouette::TimeTable.valid_days(32))[0]).to eq(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
+ expect(days.size).to eq(3)
700
+ expect(days[0]).to eq(Date.new(2014, 7, 16))
701
+ expect(days[1]).to eq(Date.new(2014,7, 18))
702
+ expect(days[2]).to eq(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
+ expect(days.size).to eq(2)
720
+ expect(days[0]).to eq(Date.new(2014, 7, 17))
721
+ expect(days[1]).to eq(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
+ expect(days.size).to eq(5)
743
+ expect(days[0]).to eq(Date.new(2014, 6, 30))
744
+ expect(days[1]).to eq(Date.new(2014, 7, 2))
745
+ expect(days[2]).to eq(Date.new(2014, 7, 16))
746
+ expect(days[3]).to eq(Date.new(2014, 7, 18))
747
+ expect(days[4]).to eq(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
+ expect(periods.size).to eq(2)
773
+ expect(periods[0].period_start).to eq(Date.new(2014, 6, 1))
774
+ expect(periods[0].period_end).to eq(Date.new(2014, 6, 14))
775
+ expect(periods[1].period_start).to eq(Date.new(2014, 6, 30))
776
+ expect(periods[1].period_end).to eq(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
+ expect(days.size).to eq(2)
791
+ expect(days.include?(Date.new(2014,7,16))).to be_truthy
792
+ expect(days.include?(Date.new(2014,7,18))).to be_falsey
793
+ expect(days.include?(Date.new(2014,7,20))).to be_truthy
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
+ expect(days.size).to eq(3)
799
+ expect(days.include?(Date.new(2014,7,16))).to be_truthy
800
+ expect(days.include?(Date.new(2014,7,18))).to be_truthy
801
+ expect(days.include?(Date.new(2014,7,20))).to be_truthy
802
+ end
803
+ it "should add date" do
804
+ subject.add_included_day(Date.new(2014,7,21))
805
+ days = subject.included_days
806
+ expect(days.size).to eq(3)
807
+ expect(days.include?(Date.new(2014,7,16))).to be_truthy
808
+ expect(days.include?(Date.new(2014,7,20))).to be_truthy
809
+ expect(days.include?(Date.new(2014,7,21))).to be_truthy
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 = create(: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
+ expect(subject.periods.size).to eq(3)
833
+ expect(subject.periods[0].period_start).to eq(Date.new(2014, 6, 30))
834
+ expect(subject.periods[0].period_end).to eq(Date.new(2014, 7, 6))
835
+ expect(subject.periods[1].period_start).to eq(Date.new(2014, 7, 15))
836
+ expect(subject.periods[1].period_end).to eq(Date.new(2014, 7, 25))
837
+ expect(subject.periods[2].period_start).to eq(Date.new(2014, 8, 1))
838
+ expect(subject.periods[2].period_end).to eq(Date.new(2014, 8, 12))
839
+ end
840
+ it "should have common day_types" do
841
+ expect(subject.int_day_types).to eq(4|16|128)
842
+ end
843
+ it "should have dates for thursdays and fridays" do
844
+ expect(subject.dates.size).to eq(4)
845
+ expect(subject.dates[0].date).to eq(Date.new(2014,7,3))
846
+ expect(subject.dates[1].date).to eq(Date.new(2014,7,18))
847
+ expect(subject.dates[2].date).to eq(Date.new(2014,7,25))
848
+ expect(subject.dates[3].date).to eq(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 = create(: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 no period" do
872
+ expect(subject.periods.size).to eq(0)
873
+ end
874
+ it "should have no day_types" do
875
+ expect(subject.int_day_types).to eq(0)
876
+ end
877
+ it "should have date all common days" do
878
+ expect(subject.dates.size).to eq(3)
879
+ expect(subject.dates[0].date).to eq(Date.new(2014,7,16))
880
+ expect(subject.dates[1].date).to eq(Date.new(2014,7,19))
881
+ expect(subject.dates[2].date).to eq(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 = create(: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
+ expect(subject.periods.size).to eq(0)
904
+ end
905
+ it "should have no day_types" do
906
+ expect(subject.int_day_types).to eq(0)
907
+ end
908
+ it "should have date reduced for period" do
909
+ expect(subject.dates.size).to eq(2)
910
+ expect(subject.dates[0].date).to eq(Date.new(2014,7,18))
911
+ expect(subject.dates[1].date).to eq(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 = create(: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
+ expect(subject.periods.size).to eq(0)
929
+ end
930
+ it "should have no day_types" do
931
+ expect(subject.int_day_types).to eq(0)
932
+ end
933
+ it "should have 1 date " do
934
+ expect(subject.dates.size).to eq(1)
935
+ expect(subject.dates[0].date).to eq(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 = create(: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 0 periods" do
958
+ expect(subject.periods.size).to eq(0)
959
+ end
960
+ it "should have 0 day_types" do
961
+ expect(subject.int_day_types).to eq(0)
962
+ end
963
+ it "should have only dates " do
964
+ expect(subject.dates.size).to eq(11)
965
+ expect(subject.dates[0].date).to eq(Date.new(2014,6,30))
966
+ expect(subject.dates[1].date).to eq(Date.new(2014,7,2))
967
+ expect(subject.dates[2].date).to eq(Date.new(2014,7,3))
968
+ expect(subject.dates[3].date).to eq(Date.new(2014,7,5))
969
+ expect(subject.dates[4].date).to eq(Date.new(2014,7,7))
970
+ expect(subject.dates[5].date).to eq(Date.new(2014,7,9))
971
+ expect(subject.dates[6].date).to eq(Date.new(2014,7,10))
972
+ expect(subject.dates[7].date).to eq(Date.new(2014,7,12))
973
+ expect(subject.dates[8].date).to eq(Date.new(2014,7,14))
974
+ expect(subject.dates[9].date).to eq(Date.new(2014,7,17))
975
+ expect(subject.dates[10].date).to eq(Date.new(2014,8,4))
976
+ end
977
+ end
978
+ context "timetables have periods or dates " do
979
+ before do
980
+ subject.periods.clear
981
+ subject.dates.clear
982
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
983
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => true)
984
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
985
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,19), :in_out => true)
986
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
987
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,6), :in_out => true)
988
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,7), :in_out => true)
989
+ subject.int_day_types = 0
990
+ another_tt = create(:time_table , :int_day_types => (4|16|64|128) )
991
+ another_tt.periods.clear
992
+ another_tt.dates.clear
993
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,6), :period_end => Date.new(2014,8,12))
994
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,17), :period_end => Date.new(2014,7,25))
995
+ subject.disjoin! another_tt
996
+ subject.reload
997
+ end
998
+ it "should have 0 period" do
999
+ expect(subject.periods.size).to eq(0)
1000
+ end
1001
+ it "should have no remained day_types" do
1002
+ expect(subject.int_day_types).to eq(0)
1003
+ end
1004
+ it "should have date reduced for period" do
1005
+ expect(subject.dates.size).to eq(4)
1006
+ expect(subject.dates[0].date).to eq(Date.new(2014,7,16))
1007
+ expect(subject.dates[1].date).to eq(Date.new(2014,7,17))
1008
+ expect(subject.dates[2].date).to eq(Date.new(2014,7,20))
1009
+ expect(subject.dates[3].date).to eq(Date.new(2014,8,7))
1010
+ end
1011
+ end
1012
+ context "disjoined timetable have all periods in removed ones " do
1013
+ before do
1014
+ subject.periods.clear
1015
+ subject.dates.clear
1016
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,8))
1017
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,20))
1018
+ subject.int_day_types = 4|16|32|128
1019
+ another_tt = create(:time_table , :int_day_types => (4|16|64|128) )
1020
+ another_tt.periods.clear
1021
+ another_tt.dates.clear
1022
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,31), :period_end => Date.new(2014,8,12))
1023
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,20))
1024
+ subject.disjoin! another_tt
1025
+ subject.reload
1026
+ end
1027
+ it "should have 0 result periods" do
1028
+ expect(subject.periods.size).to eq(0)
1029
+ end
1030
+ it "should have no remained day_types" do
1031
+ expect(subject.int_day_types).to eq(0)
1032
+ end
1033
+ it "should have dates for period reduced" do
1034
+ expect(subject.dates.size).to eq(4)
1035
+ expect(subject.dates[0].date).to eq(Date.new(2014,7,3))
1036
+ expect(subject.dates[1].date).to eq(Date.new(2014,7,10))
1037
+ expect(subject.dates[2].date).to eq(Date.new(2014,7,17))
1038
+ expect(subject.dates[3].date).to eq(Date.new(2014,8,7))
1039
+ end
1040
+ end
1041
+
1042
+ context "timetable with dates against timetable with dates and periods" do
1043
+ before do
1044
+ subject.periods.clear
1045
+ subject.dates.clear
1046
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true)
1047
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => true)
1048
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
1049
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,19), :in_out => true)
1050
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true)
1051
+ subject.int_day_types = 0
1052
+ another_tt = create(:time_table , :int_day_types => (4|16|64|128) )
1053
+ another_tt.periods.clear
1054
+ another_tt.dates.clear
1055
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,7,31), :period_end => Date.new(2014,8,12))
1056
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => true)
1057
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true)
1058
+ subject.disjoin! another_tt
1059
+ subject.reload
1060
+ end
1061
+ it "should have 0 result periods" do
1062
+ expect(subject.periods.size).to eq(0)
1063
+ end
1064
+ it "should have no remained day_types" do
1065
+ subject.int_day_types == 0
1066
+ end
1067
+ it "should have 3 dates left" do
1068
+ expect(subject.dates.size).to eq(3)
1069
+ expect(subject.dates[0].date).to eq(Date.new(2014,7,16))
1070
+ expect(subject.dates[1].date).to eq(Date.new(2014,7,19))
1071
+ expect(subject.dates[2].date).to eq(Date.new(2014,7,20))
1072
+ end
1073
+ end
1074
+ context "timetable with dates against timetable with dates and periods all covered" do
1075
+ before do
1076
+ subject.periods.clear
1077
+ subject.dates.clear
1078
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,1), :in_out => true)
1079
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,2), :in_out => true)
1080
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,5), :in_out => true)
1081
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,6), :in_out => true)
1082
+ subject.int_day_types = 512
1083
+ another_tt = create(:time_table , :int_day_types => (32|64|512) )
1084
+ another_tt.periods.clear
1085
+ another_tt.dates.clear
1086
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,11))
1087
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,1), :in_out => true)
1088
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,2), :in_out => true)
1089
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,5), :in_out => true)
1090
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,6), :in_out => true)
1091
+ subject.disjoin! another_tt
1092
+ subject.reload
1093
+ end
1094
+ it "should have 0 result periods" do
1095
+ expect(subject.periods.size).to eq(0)
1096
+ end
1097
+ it "should have no remained day_types" do
1098
+ subject.int_day_types == 0
1099
+ end
1100
+ it "should have 0 dates left" do
1101
+ expect(subject.dates.size).to eq(0)
1102
+ end
1103
+ end
1104
+
1105
+ context "with only periods : disjoined timetable have no empty period" do
1106
+ before do
1107
+ subject.periods.clear
1108
+ subject.dates.clear
1109
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,8))
1110
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,10), :period_end => Date.new(2014,8,31))
1111
+ subject.int_day_types = 4|8
1112
+ another_tt = create(:time_table , :int_day_types => (4|8) )
1113
+ another_tt.periods.clear
1114
+ another_tt.dates.clear
1115
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,4), :period_end => Date.new(2014,8,7))
1116
+ subject.disjoin! another_tt
1117
+ subject.reload
1118
+ end
1119
+ it "should have 0 result periods" do
1120
+ expect(subject.periods.size).to eq(0)
1121
+ end
1122
+ it "should have 0 day_types" do
1123
+ expect(subject.int_day_types).to eq(0)
1124
+ end
1125
+ it "should have 6 dates " do
1126
+ expect(subject.dates.size).to eq(6)
1127
+ expect(subject.dates[0].date).to eq(Date.new(2014,8,11))
1128
+ expect(subject.dates[1].date).to eq(Date.new(2014,8,12))
1129
+ expect(subject.dates[2].date).to eq(Date.new(2014,8,18))
1130
+ expect(subject.dates[3].date).to eq(Date.new(2014,8,19))
1131
+ expect(subject.dates[4].date).to eq(Date.new(2014,8,25))
1132
+ expect(subject.dates[5].date).to eq(Date.new(2014,8,26))
1133
+ end
1134
+ end
1135
+
1136
+ context "with only periods : disjoined timetable have no one day period" do
1137
+ before do
1138
+ subject.periods.clear
1139
+ subject.dates.clear
1140
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,6))
1141
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,10), :period_end => Date.new(2014,8,31))
1142
+ subject.int_day_types = 4|8|16
1143
+ another_tt = create(:time_table , :int_day_types => (4|8) )
1144
+ another_tt.periods.clear
1145
+ another_tt.dates.clear
1146
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,4), :period_end => Date.new(2014,8,5))
1147
+ subject.disjoin! another_tt
1148
+ subject.reload
1149
+ end
1150
+ it "should have 0 result periods" do
1151
+ expect(subject.periods.size).to eq(0)
1152
+ end
1153
+ it "should have 0 day_types" do
1154
+ expect(subject.int_day_types).to eq(0)
1155
+ end
1156
+ it "should have 10 dates " do
1157
+ expect(subject.dates.size).to eq(10)
1158
+ expect(subject.dates[0].date).to eq(Date.new(2014,8,6))
1159
+ expect(subject.dates[1].date).to eq(Date.new(2014,8,11))
1160
+ expect(subject.dates[2].date).to eq(Date.new(2014,8,12))
1161
+ expect(subject.dates[3].date).to eq(Date.new(2014,8,13))
1162
+ expect(subject.dates[4].date).to eq(Date.new(2014,8,18))
1163
+ expect(subject.dates[5].date).to eq(Date.new(2014,8,19))
1164
+ expect(subject.dates[6].date).to eq(Date.new(2014,8,20))
1165
+ expect(subject.dates[7].date).to eq(Date.new(2014,8,25))
1166
+ expect(subject.dates[8].date).to eq(Date.new(2014,8,26))
1167
+ expect(subject.dates[9].date).to eq(Date.new(2014,8,27))
1168
+ end
1169
+ end
1170
+
1171
+ context "with periods against dates: disjoined timetable have no unused excluded date" do
1172
+ before do
1173
+ subject.periods.clear
1174
+ subject.dates.clear
1175
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,1), :period_end => Date.new(2014,8,8))
1176
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2014,8,10), :period_end => Date.new(2014,8,31))
1177
+ subject.int_day_types = 4|8|16
1178
+ another_tt = create(:time_table , :int_day_types => (0) )
1179
+ another_tt.periods.clear
1180
+ another_tt.dates.clear
1181
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,4), :in_out => true)
1182
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,5), :in_out => true)
1183
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,8,7), :in_out => true)
1184
+ subject.disjoin! another_tt
1185
+ subject.reload
1186
+ end
1187
+ it "should have same 0 result periods" do
1188
+ expect(subject.periods.size).to eq(0)
1189
+ end
1190
+ it "should have 0 day_types" do
1191
+ expect(subject.int_day_types).to eq(0)
1192
+ end
1193
+ it "should have only 10 dates " do
1194
+ expect(subject.dates.size).to eq(10)
1195
+ expect(subject.dates[0].date).to eq(Date.new(2014,8,6))
1196
+ expect(subject.dates[1].date).to eq(Date.new(2014,8,11))
1197
+ expect(subject.dates[2].date).to eq(Date.new(2014,8,12))
1198
+ expect(subject.dates[3].date).to eq(Date.new(2014,8,13))
1199
+ expect(subject.dates[4].date).to eq(Date.new(2014,8,18))
1200
+ expect(subject.dates[5].date).to eq(Date.new(2014,8,19))
1201
+ expect(subject.dates[6].date).to eq(Date.new(2014,8,20))
1202
+ expect(subject.dates[7].date).to eq(Date.new(2014,8,25))
1203
+ expect(subject.dates[8].date).to eq(Date.new(2014,8,26))
1204
+ expect(subject.dates[9].date).to eq(Date.new(2014,8,27))
1205
+ end
1206
+ end
1207
+
1208
+
1209
+ context "with same definition : dsjointed timetable should be empty" do
1210
+ before do
1211
+ subject.periods.clear
1212
+ subject.dates.clear
1213
+ subject.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2015,6,1), :period_end => Date.new(2015,6,30))
1214
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2015,6,16), :in_out => true)
1215
+ subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2015,6,22), :in_out => false)
1216
+ subject.int_day_types = 4|8|16|32|64|128|256
1217
+ another_tt = create(:time_table , :int_day_types => ( 4|8|16|32|64|128|256) )
1218
+ another_tt.periods.clear
1219
+ another_tt.dates.clear
1220
+ another_tt.periods << Chouette::TimeTablePeriod.new(:period_start => Date.new(2015,6,1), :period_end => Date.new(2015,6,30))
1221
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2015,6,16), :in_out => true)
1222
+ another_tt.dates << Chouette::TimeTableDate.new( :date => Date.new(2015,6,22), :in_out => false)
1223
+ subject.disjoin! another_tt
1224
+ subject.reload
1225
+ end
1226
+ it "should have same 0 result periods" do
1227
+ expect(subject.periods.size).to eq(0)
1228
+ end
1229
+ it "should have 0 day_types" do
1230
+ expect(subject.int_day_types).to eq(0)
1231
+ end
1232
+ it "should have 0 dates " do
1233
+ expect(subject.dates.size).to eq(0)
1234
+ end
1235
+ end
1236
+ end
1237
+
1238
+ describe "#duplicate" do
1239
+ it "should be a copy of" do
1240
+ target=subject.duplicate
1241
+ expect(target.id).to be_nil
1242
+ expect(target.comment).to eq("Copy of "+subject.comment)
1243
+ expect(target.objectid).to eq(subject.objectid+"_1")
1244
+ expect(target.int_day_types).to eq(subject.int_day_types)
1245
+ expect(target.dates.size).to eq(subject.dates.size)
1246
+ target.dates.each do |d|
1247
+ expect(d.time_table_id).to be_nil
1248
+ end
1249
+ expect(target.periods.size).to eq(subject.periods.size)
1250
+ target.periods.each do |p|
1251
+ expect(p.time_table_id).to be_nil
1252
+ end
1253
+ end
1254
+ end
1255
+
1256
+ describe "#tags" do
1257
+ it "should accept tags" do
1258
+ subject.tag_list = "toto, titi"
1259
+ subject.save
1260
+ subject.reload
1261
+ expect(Chouette::TimeTable.tag_counts.size).to eq(2)
1262
+ expect(subject.tag_list.size).to eq(2)
1263
+ end
1264
+ end
1265
+
1266
+ end