diff options
| author | Teddy Wing | 2017-05-24 11:31:34 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-05-26 13:20:55 +0200 |
| commit | 16c9f2b445de2af540db4280bde39666101e1eb0 (patch) | |
| tree | acb823b50344eaa48829dbd24d3b73faa3a4756d | |
| parent | ec5f85038b00f1f69c85d94a042ecdad22d4e8cf (diff) | |
| download | chouette-core-16c9f2b445de2af540db4280bde39666101e1eb0.tar.bz2 | |
IncreasingTimeOrder: Make `increasing_times_validate` a class method
Making this a class method allows us to test it more easily. Previously
it was attached to `Chouette::VehicleJourneyAtStop`, and the tests I
copied call it on a VehicleJourneyAtStop.
It seems like instantiating a Validator class is kind of a pain, or at
least a bit weird, since it's for internal Rails use. So we change it to
a class method.
Change `#exceeds_gap?` in this class to a class method also so we can
continue to call it from `.increasing_times_validate`. Eventually we'll
switch to the new version in the `TimeDuration` module.
Comment out the tests for `#exceeds_gap?` for now as we'll be moving
those into a new spec file for `TimeDuration`
Refs #870
2 files changed, 38 insertions, 27 deletions
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 4b2fac40a..cc32ae520 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 @@ -12,7 +12,7 @@ module Chouette .vehicle_journey_at_stops .select { |vjas| vjas.departure_time && vjas.arrival_time } .each do |vjas| - unless vjas.increasing_times_validate(previous_at_stop) + unless self.class.increasing_times_validate(vjas, previous_at_stop) vehicle_journey.errors.add( :vehicle_journey_at_stops, 'time gap overflow' @@ -23,22 +23,23 @@ module Chouette end end - def increasing_times_validate(previous_at_stop) + def self.increasing_times_validate(at_stop, previous_at_stop) result = true return result unless previous_at_stop - if exceeds_gap?(previous_at_stop.departure_time, departure_time) + if self.exceeds_gap?(previous_at_stop.departure_time, at_stop.departure_time) result = false - errors.add( :departure_time, 'departure time gap overflow') + at_stop.errors.add(:departure_time, 'departure time gap overflow') end - if exceeds_gap?(previous_at_stop.arrival_time, arrival_time) + if self.exceeds_gap?(previous_at_stop.arrival_time, at_stop.arrival_time) result = false - errors.add( :arrival_time, 'arrival time gap overflow') + at_stop.errors.add(:arrival_time, 'arrival time gap overflow') end result end - def exceeds_gap?(earlier, later) + # TODO: Get rid of this and change to TimeDuration version + def self.exceeds_gap?(earlier, later) (4 * 3600) < ((later - earlier) % (3600 * 24)) 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 a667fbfcc..0b8565b5b 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 @@ -23,24 +23,25 @@ describe Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator do 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 + # TODO: Move to TimeDuration + # 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 + describe ".increasing_times_validate" do let!(:vehicle_journey) { create(:vehicle_journey_odd) } subject { vehicle_journey.vehicle_journey_at_stops.first } @@ -50,7 +51,10 @@ describe Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator do 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( + Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator + .increasing_times_validate(vjas2, vjas1) + ).to be_falsey expect(vjas2.errors).not_to be_empty expect(vjas2.errors[:arrival_time]).not_to be_blank end @@ -59,7 +63,10 @@ describe Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator do 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( + Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator + .increasing_times_validate(vjas2, vjas1) + ).to be_falsey expect(vjas2.errors).not_to be_empty expect(vjas2.errors[:departure_time]).not_to be_blank end @@ -67,7 +74,10 @@ describe Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator do context "when vjas does'nt exceed gap" do it "should not add errors" do - expect(vjas2.increasing_times_validate(vjas1)).to be_truthy + expect( + Chouette::VehicleJourneyAtStopsAreInIncreasingTimeOrderValidator + .increasing_times_validate(vjas2, vjas1) + ).to be_truthy expect(vjas2.errors).to be_empty end end |
