diff options
4 files changed, 69 insertions, 60 deletions
diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb index be69de0ed..cd515be7f 100644 --- a/app/models/chouette/vehicle_journey_at_stop.rb +++ b/app/models/chouette/vehicle_journey_at_stop.rb @@ -26,23 +26,5 @@ module Chouette @_destroy = false end - def increasing_times_validate( previous) - result = true - return result unless previous - - if exceeds_gap?( previous.departure_time, departure_time) - result = false - errors.add( :departure_time, 'departure time gap overflow') - end - if exceeds_gap?( previous.arrival_time, arrival_time) - result = false - errors.add( :arrival_time, 'arrival time gap overflow') - end - result - end - def exceeds_gap?(earlier, later) - (4 * 3600) < ((later - earlier) % (3600 * 24)) - end - end end diff --git a/app/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator.rb b/app/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator.rb index cd43ea953..058bdf79a 100644 --- a/app/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator.rb +++ b/app/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator.rb @@ -23,5 +23,24 @@ module Chouette previous = vjas end end + + def increasing_times_validate( previous) + result = true + return result unless previous + + if exceeds_gap?( previous.departure_time, departure_time) + result = false + errors.add( :departure_time, 'departure time gap overflow') + end + if exceeds_gap?( previous.arrival_time, arrival_time) + result = false + errors.add( :arrival_time, 'arrival time gap overflow') + end + result + end + + def exceeds_gap?(earlier, later) + (4 * 3600) < ((later - earlier) % (3600 * 24)) + 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 ba5b2bf74..328f91726 100644 --- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb +++ b/spec/models/chouette/vehicle_journey_at_stop_spec.rb @@ -1,46 +1,4 @@ require 'spec_helper' describe Chouette::VehicleJourneyAtStop, :type => :model do - let!(:vehicle_journey) { create(:vehicle_journey_odd)} - subject { vehicle_journey.vehicle_journey_at_stops.first } - - describe "#exceeds_gap?" do - it "should return false if gap < 1.hour" do - t1 = Time.now - t2 = Time.now + 3.minutes - expect(subject.exceeds_gap?(t1, t2)).to be_falsey - end - it "should return true if gap > 4.hour" do - t1 = Time.now - t2 = Time.now + (4.hours + 1.minutes) - expect(subject.exceeds_gap?(t1, t2)).to be_truthy - end - end - - describe "#increasing_times_validate" do - let(:vjas1){ vehicle_journey.vehicle_journey_at_stops[0]} - let(:vjas2){ vehicle_journey.vehicle_journey_at_stops[1]} - context "when vjas#arrival_time exceeds gap" do - it "should add errors on arrival_time" do - vjas1.arrival_time = vjas2.arrival_time - 5.hour - expect(vjas2.increasing_times_validate(vjas1)).to be_falsey - expect(vjas2.errors).not_to be_empty - expect(vjas2.errors[:arrival_time]).not_to be_blank - end - end - context "when vjas#departure_time exceeds gap" do - it "should add errors on departure_time" do - vjas1.departure_time = vjas2.departure_time - 5.hour - expect(vjas2.increasing_times_validate(vjas1)).to be_falsey - expect(vjas2.errors).not_to be_empty - expect(vjas2.errors[:departure_time]).not_to be_blank - end - end - context "when vjas does'nt exceed gap" do - it "should not add errors" do - expect(vjas2.increasing_times_validate(vjas1)).to be_truthy - expect(vjas2.errors).to be_empty - end - end - end end diff --git a/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator_spec.rb b/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator_spec.rb index ea161ec79..a667fbfcc 100644 --- a/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator_spec.rb +++ b/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_time_order_validator_spec.rb @@ -22,4 +22,54 @@ describe Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator do expect(subject).not_to be_valid end end + + describe "#exceeds_gap?" do + let!(:vehicle_journey) { create(:vehicle_journey_odd) } + subject { vehicle_journey.vehicle_journey_at_stops.first } + + it "should return false if gap < 1.hour" do + t1 = Time.now + t2 = Time.now + 3.minutes + expect(subject.exceeds_gap?(t1, t2)).to be_falsey + end + + it "should return true if gap > 4.hour" do + t1 = Time.now + t2 = Time.now + (4.hours + 1.minutes) + expect(subject.exceeds_gap?(t1, t2)).to be_truthy + end + end + + describe "#increasing_times_validate" do + let!(:vehicle_journey) { create(:vehicle_journey_odd) } + subject { vehicle_journey.vehicle_journey_at_stops.first } + + let(:vjas1) { vehicle_journey.vehicle_journey_at_stops[0] } + let(:vjas2) { vehicle_journey.vehicle_journey_at_stops[1] } + + context "when vjas#arrival_time exceeds gap" do + it "should add errors on arrival_time" do + vjas1.arrival_time = vjas2.arrival_time - 5.hour + expect(vjas2.increasing_times_validate(vjas1)).to be_falsey + expect(vjas2.errors).not_to be_empty + expect(vjas2.errors[:arrival_time]).not_to be_blank + end + end + + context "when vjas#departure_time exceeds gap" do + it "should add errors on departure_time" do + vjas1.departure_time = vjas2.departure_time - 5.hour + expect(vjas2.increasing_times_validate(vjas1)).to be_falsey + expect(vjas2.errors).not_to be_empty + expect(vjas2.errors[:departure_time]).not_to be_blank + end + end + + context "when vjas does'nt exceed gap" do + it "should not add errors" do + expect(vjas2.increasing_times_validate(vjas1)).to be_truthy + expect(vjas2.errors).to be_empty + end + end + end end |
