aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-07-17 18:28:21 +0200
committerTeddy Wing2017-07-21 15:59:53 +0200
commitdca839c6b1d55413d613a02ad8e8d98b688b3155 (patch)
treec6eded086a18345e17c207a5f421f595d4b65079
parent0166ef93691416b687091c4ff0aa6fb308a449e2 (diff)
downloadchouette-core-dca839c6b1d55413d613a02ad8e8d98b688b3155.tar.bz2
VehicleJourneyAtStop: Add new specs for day offset validation
Replace our numericality validation tests with an equivalent set of tests since we're now using a custom validation function. Move `#outside_range` into the class scope so we can test it. Ignoring model validation tests and hooking into this method as the "real" important validator. Refs #3597
-rw-r--r--app/models/chouette/vehicle_journey_at_stop.rb13
-rw-r--r--spec/models/chouette/vehicle_journey_at_stop_spec.rb30
2 files changed, 28 insertions, 15 deletions
diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb
index 6e719fc61..1cb1c4767 100644
--- a/app/models/chouette/vehicle_journey_at_stop.rb
+++ b/app/models/chouette/vehicle_journey_at_stop.rb
@@ -35,10 +35,6 @@ module Chouette
end
def day_offset_must_be_within_range
- def outside_range(offset)
- offset < 0 || offset > DAY_OFFSET_MAX
- end
-
def error_message
I18n.t(
'vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max',
@@ -47,14 +43,19 @@ module Chouette
)
end
- if outside_range(arrival_day_offset)
+ if day_offset_outside_range?(arrival_day_offset)
errors.add(:arrival_day_offset, error_message)
end
- if outside_range(departure_day_offset)
+ if day_offset_outside_range?(departure_day_offset)
errors.add(:departure_day_offset, error_message)
end
end
+ def day_offset_outside_range?(offset)
+ offset < 0 || offset > DAY_OFFSET_MAX
+ end
+
+
end
end
diff --git a/spec/models/chouette/vehicle_journey_at_stop_spec.rb b/spec/models/chouette/vehicle_journey_at_stop_spec.rb
index 23f33fc8f..99c8af066 100644
--- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb
+++ b/spec/models/chouette/vehicle_journey_at_stop_spec.rb
@@ -1,16 +1,28 @@
require 'spec_helper'
RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do
- it do
- should validate_numericality_of(:arrival_day_offset)
- .is_greater_than_or_equal_to(0)
- .is_less_than_or_equal_to(Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX)
- end
+ describe "#day_offset_outside_range" do
+ it "disallows negative offsets" do
+ at_stop = build_stubbed(:vehicle_journey_at_stop)
+
+ expect(at_stop.day_offset_outside_range?(-1)).to be true
+ end
+
+ it "disallows offsets greater than DAY_OFFSET_MAX" do
+ at_stop = build_stubbed(:vehicle_journey_at_stop)
- it do
- should validate_numericality_of(:departure_day_offset)
- .is_greater_than_or_equal_to(0)
- .is_less_than_or_equal_to(Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX)
+ expect(at_stop.day_offset_outside_range?(
+ Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX + 1
+ )).to be true
+ end
+
+ it "allows offsets between 0 and DAY_OFFSET_MAX inclusive" do
+ at_stop = build_stubbed(:vehicle_journey_at_stop)
+
+ expect(at_stop.day_offset_outside_range?(
+ Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX
+ )).to be false
+ end
end
describe "#validate" do