aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-07-21 12:23:19 +0200
committerTeddy Wing2017-07-21 15:59:53 +0200
commit1dc71242b81f6bdcde4c5e093c36124011599675 (patch)
tree05e26240032739b711f20493084e4938bd688b50
parenta62c80f08417ff337cdabbd997e2f9bbae44e6b3 (diff)
downloadchouette-core-1dc71242b81f6bdcde4c5e093c36124011599675.tar.bz2
VehicleJourneyAtStop: Handle nil value when checking offset within range
Old `VehicleJourneyAtStop`s were created before I added a database default of 0 to those records. Thus old data will not have the proper 0 value to check against, and our `<` `>` comparisons trigger an error due to a comparison with `nil`. To get around the error, set a default value of 0 (the current day) when the offset isn't set. I think that ideally this should be solved at the database level with a data migration, but hopefully this is okay for now. Whenever we do decide to do a data migration, however, this code needs to be deleted. Refs #3597
-rw-r--r--app/models/chouette/vehicle_journey_at_stop.rb4
-rw-r--r--spec/models/chouette/vehicle_journey_at_stop_spec.rb10
2 files changed, 14 insertions, 0 deletions
diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb
index 3c350b2ae..35d94aa75 100644
--- a/app/models/chouette/vehicle_journey_at_stop.rb
+++ b/app/models/chouette/vehicle_journey_at_stop.rb
@@ -59,6 +59,10 @@ module Chouette
end
def day_offset_outside_range?(offset)
+ # At-stops that were created before the database-default of 0 will have
+ # nil offsets. Handle these gracefully by forcing them to a 0 offset.
+ offset ||= 0
+
offset < 0 || offset > DAY_OFFSET_MAX
end
diff --git a/spec/models/chouette/vehicle_journey_at_stop_spec.rb b/spec/models/chouette/vehicle_journey_at_stop_spec.rb
index 58616b066..38196bb88 100644
--- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb
+++ b/spec/models/chouette/vehicle_journey_at_stop_spec.rb
@@ -19,6 +19,16 @@ RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do
Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX
)).to be false
end
+
+ it "forces a nil offset to 0" do
+ at_stop = build_stubbed(
+ :vehicle_journey_at_stop,
+ arrival_day_offset: nil,
+ departure_day_offset: nil
+ )
+
+ expect(at_stop.day_offset_outside_range?(nil)).to be false
+ end
end
describe "#validate" do