aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/chouette/route_spec.rb
blob: 1acc5a0f778626a3b22ffa4947baff88e3a32bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require 'spec_helper'

describe Chouette::Route, :type => :model do
  subject { create(:route) }

  it { is_expected.to validate_uniqueness_of :objectid }

  describe '#objectid' do
    subject { super().objectid }
    it { is_expected.to be_kind_of(Chouette::ObjectId) }
  end

  #it { is_expected.to validate_presence_of :name }
  it { is_expected.to validate_presence_of :line }
  #it { is_expected.to validate_presence_of :wayback_code }
  #it { is_expected.to validate_presence_of :direction_code }

  context "reordering methods" do
    let( :bad_stop_point_ids){subject.stop_points.map { |sp| sp.id + 1}}
    let( :ident){subject.stop_points.map(&:id)}
    let( :first_last_swap){ [ident.last] + ident[1..-2] + [ident.first]}

    describe "#reorder!" do
      context "invalid stop_point_ids" do
        let( :new_stop_point_ids) { bad_stop_point_ids}
        it { expect(subject.reorder!( new_stop_point_ids)).to be_falsey}
      end

      context "swaped last and first stop_point_ids" do
        let!( :new_stop_point_ids) { first_last_swap}
        let!( :old_stop_point_ids) { subject.stop_points.map(&:id) }
        let!( :old_stop_area_ids) { subject.stop_areas.map(&:id) }

        it "should keep stop_point_ids order unchanged" do
          expect(subject.reorder!( new_stop_point_ids)).to be_truthy
          expect(subject.stop_points.map(&:id)).to eq( old_stop_point_ids)
        end
        it "should have changed stop_area_ids order" do
          expect(subject.reorder!( new_stop_point_ids)).to be_truthy
          subject.reload
          expect(subject.stop_areas.map(&:id)).to eq( [old_stop_area_ids.last] + old_stop_area_ids[1..-2] + [old_stop_area_ids.first])
        end
      end
    end

    describe "#stop_point_permutation?" do
      context "invalid stop_point_ids" do
        let( :new_stop_point_ids) { bad_stop_point_ids}
        it { is_expected.not_to be_stop_point_permutation( new_stop_point_ids)}
      end
      context "unchanged stop_point_ids" do
        let( :new_stop_point_ids) { ident}
        it { is_expected.to be_stop_point_permutation( new_stop_point_ids)}
      end
      context "swaped last and first stop_point_ids" do
        let( :new_stop_point_ids) { first_last_swap}
        it { is_expected.to be_stop_point_permutation( new_stop_point_ids)}
      end
    end
  end

  describe "#stop_points_attributes=" do
      let( :journey_pattern) { create( :journey_pattern, :route => subject )}
      let( :vehicle_journey) { create( :vehicle_journey, :journey_pattern => journey_pattern)}
      def subject_stop_points_attributes
          {}.tap do |hash|
              subject.stop_points.each_with_index { |sp,index| hash[ index.to_s ] = sp.attributes }
          end
      end
      context "route having swapped a new stop" do
          let( :new_stop_point ){build( :stop_point, :route => subject)}
          def added_stop_hash
            subject_stop_points_attributes.tap do |h|
                h["4"] = new_stop_point.attributes.merge( "position" => "4", "_destroy" => "" )
            end
          end
          let!( :new_route_size ){ subject.stop_points.size+1 }

          it "should have added stop_point in route" do
              subject.update_attributes( :stop_points_attributes => added_stop_hash)
              expect(Chouette::Route.find( subject.id ).stop_points.size).to eq(new_route_size)
          end
          it "should have added stop_point in route's journey pattern" do
              subject.update_attributes( :stop_points_attributes => added_stop_hash)
              expect(Chouette::JourneyPattern.find( journey_pattern.id ).stop_points.size).to eq(new_route_size)
          end
          it "should have added stop_point in route's vehicle journey at stop" do
              subject.update_attributes( :stop_points_attributes => added_stop_hash)
              expect(Chouette::VehicleJourney.find( vehicle_journey.id ).vehicle_journey_at_stops.size).to eq(new_route_size)
          end
      end
      context "route having swapped stop" do
          def swapped_stop_hash
            subject_stop_points_attributes.tap do |h|
                h[ "1" ][ "position" ] = "3"
                h[ "3" ][ "position" ] = "1"
            end
          end
          let!( :new_stop_id_list ){ subject.stop_points.map(&:id).tap {|array| array.insert( 1, array.delete_at(3)); array.insert( 3, array.delete_at(2) )} }

          it "should have swap stop_points from route" do
              subject.update_attributes( :stop_points_attributes => swapped_stop_hash)
              expect(Chouette::Route.find( subject.id ).stop_points.map(&:id)).to eq(new_stop_id_list)
          end
          it "should have swap stop_points from route's journey pattern" do
              subject.update_attributes( :stop_points_attributes => swapped_stop_hash)
              expect(Chouette::JourneyPattern.find( journey_pattern.id ).stop_points.map(&:id)).to eq(new_stop_id_list)
          end
          it "should have swap stop_points from route's vehicle journey at stop" do
              subject.update_attributes( :stop_points_attributes => swapped_stop_hash)
              expect(Chouette::VehicleJourney.find( vehicle_journey.id ).vehicle_journey_at_stops.map(&:stop_point_id)).to match_array(new_stop_id_list)
          end
      end
      context "route having a deleted stop" do
          def removed_stop_hash
            subject_stop_points_attributes.tap do |h|
                h[ "1" ][ "_destroy" ] = "1"
            end
          end
          let!( :new_stop_id_list ){ subject.stop_points.map(&:id).tap {|array| array.delete_at(1) } }

          it "should ignore deleted stop_point from route" do
              subject.update_attributes( :stop_points_attributes => removed_stop_hash)
              expect(Chouette::Route.find( subject.id ).stop_points.map(&:id)).to eq(new_stop_id_list)
          end
          it "should ignore deleted stop_point from route's journey pattern" do
              subject.update_attributes( :stop_points_attributes => removed_stop_hash)
              expect(Chouette::JourneyPattern.find( journey_pattern.id ).stop_points.map(&:id)).to eq(new_stop_id_list)
          end
          it "should ignore deleted stop_point from route's vehicle journey at stop" do
              subject.update_attributes( :stop_points_attributes => removed_stop_hash)
              expect(Chouette::VehicleJourney.find( vehicle_journey.id ).vehicle_journey_at_stops.map(&:stop_point_id)).to match_array(new_stop_id_list)
          end
      end
  end

  describe "#stop_points" do
    context "#find_by_stop_area" do
      context "when arg is first quay id" do
        let(:first_stop_point) { subject.stop_points.first}
        it "should return first quay" do
          expect(subject.stop_points.find_by_stop_area( first_stop_point.stop_area_id)).to eq( first_stop_point)
        end
      end
    end
  end
  describe "#stop_areas" do
    let(:line){ create(:line)}
    let(:route_1){ create(:route, :line => line)}
    let(:route_2){ create(:route, :line => line)}
    it "should retreive all stop_area on route" do
      route_1.stop_areas.each do |sa|
        expect(sa.stop_points.map(&:route_id).uniq).to eq([route_1.id])
      end
    end

    context "when route is looping: last and first stop area are the same" do
      it "should retreive same stop_area one last and first position" do
        route_loop = create(:route, :line => line)
        first_stop = Chouette::StopPoint.where( :route_id => route_loop.id, :position => 0).first
        last_stop = create(:stop_point, :route => route_loop, :position => 4, :stop_area => first_stop.stop_area)

        expect(route_loop.stop_areas.size).to eq(6)
        expect(route_loop.stop_areas.select {|s| s.id == first_stop.stop_area.id}.size).to eq(2)
      end
    end
  end

  describe "#direction_code" do
    def self.legacy_directions
      %w{A R ClockWise CounterClockWise North NorthWest West SouthWest
        South SouthEast East NorthEast}
    end
    legacy_directions.each do |direction|
      context "when direction is #{direction}" do
        direction_code = Chouette::Direction.new( Chouette::Route.direction_binding[ direction])
        it "should be #{direction_code}" do
          subject.direction = direction
          expect(subject.direction_code).to eq(direction_code)
        end
      end
    end
    context "when direction is nil" do
      it "should be nil" do
        subject.direction = nil
        expect(subject.direction_code).to be_nil
      end
    end
  end
  describe "#direction_code=" do
    context "when unknown direction is provided" do
      it "should change direction to nil" do
        subject.direction_code = "dummy"
        expect(subject.direction).to be_nil
      end
    end
    context "when an existing direction (west) is provided" do
      it "should change direction Direction.west" do
        subject.direction_code = "west"
        expect(subject.direction).to eq("West")
      end
    end
  end
  describe "#wayback_code" do
    def self.legacy_waybacks
      %w{A R}
    end
    legacy_waybacks.each do |wayback|
      context "when wayback is #{wayback}" do
        wayback_code = Chouette::Wayback.new( Chouette::Route.wayback_binding[ wayback])
        it "should be #{wayback_code}" do
          subject.wayback = wayback
          expect(subject.wayback_code).to eq(wayback_code)
        end
      end
    end
    context "when wayback is nil" do
      it "should be nil" do
        subject.wayback = nil
        expect(subject.wayback_code).to be_nil
      end
    end
  end
  describe "#wayback_code=" do
    context "when unknown wayback is provided" do
      it "should change wayback to nil" do
        subject.wayback_code = "dummy"
        expect(subject.wayback).to be_nil
      end
    end
    context "when an existing wayback (straight_forward) is provided" do
      it "should change wayback Wayback.straight_forward" do
        subject.wayback_code = "straight_forward"
        expect(subject.wayback).to eq("A")
      end
    end
  end
end