aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/import/gtfs_spec.rb
blob: 96b93dc62730465cc7e5dc6a3d06936a53c73848 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
require "rails_helper"

RSpec.describe Import::Gtfs do

  let(:referential) do
    create :referential do |referential|
      referential.line_referential.objectid_format = "netex"
      referential.stop_area_referential.objectid_format = "netex"
    end
  end

  let(:workbench) do
    create :workbench do |workbench|
      workbench.line_referential.objectid_format = "netex"
      workbench.stop_area_referential.objectid_format = "netex"
    end
  end

  def create_import(file)
    Import::Gtfs.new workbench: workbench, local_file: fixtures_path(file)
  end

  describe "#import_agencies" do
    let(:import) { create_import "google-sample-feed.zip" }
    it "should create a company for each agency" do
      import.import_agencies

      expect(workbench.line_referential.companies.pluck(:registration_number, :name)).to eq([["DTA","Demo Transit Authority"]])
    end
  end

  describe "#import_stops" do
    let(:import) { create_import "google-sample-feed.zip" }
    it "should create a company for each agency" do
      import.import_stops

      defined_attributes = [
        :registration_number, :name, :parent_id, :latitude, :longitude
      ]
      expected_attributes = [
        ["AMV", "Amargosa Valley (Demo)", nil, 36.641496, -116.40094],
        ["EMSI", "E Main St / S Irving St (Demo)", nil, 36.905697, -116.76218],
        ["DADAN", "Doing Ave / D Ave N (Demo)", nil, 36.909489, -116.768242],
        ["NANAA", "North Ave / N A Ave (Demo)", nil, 36.914944, -116.761472],
        ["NADAV", "North Ave / D Ave N (Demo)", nil, 36.914893, -116.76821],
        ["STAGECOACH", "Stagecoach Hotel & Casino (Demo)", nil, 36.915682, -116.751677],
        ["BULLFROG", "Bullfrog (Demo)", nil, 36.88108, -116.81797],
        ["BEATTY_AIRPORT", "Nye County Airport (Demo)", nil, 36.868446, -116.784582],
        ["FUR_CREEK_RES", "Furnace Creek Resort (Demo)", nil, 36.425288, -117.133162]
      ]

      expect(workbench.stop_area_referential.stop_areas.pluck(*defined_attributes)).to match_array(expected_attributes)
    end
  end

  describe "#import_routes" do
    let(:import) { create_import "google-sample-feed.zip" }
    it "should create a line for each route" do
      import.import_routes

      defined_attributes = [
        :registration_number, :name, :number, :published_name,
        "companies.registration_number",
        :comment, :url
      ]
      expected_attributes = [
        ["AAMV", "Airport - Amargosa Valley", "50", "Airport - Amargosa Valley", nil, nil, nil],
        ["CITY", "City", "40", "City", nil, nil, nil],
        ["STBA", "Stagecoach - Airport Shuttle", "30", "Stagecoach - Airport Shuttle", nil, nil, nil],
        ["BFC", "Bullfrog - Furnace Creek Resort", "20", "Bullfrog - Furnace Creek Resort", nil, nil, nil],
        ["AB", "Airport - Bullfrog", "10", "Airport - Bullfrog", nil, nil, nil]
      ]

      expect(workbench.line_referential.lines.includes(:company).pluck(*defined_attributes)).to match_array(expected_attributes)
    end
  end

  describe "#import_trips" do
    let(:import) { create_import "google-sample-feed.zip" }
    before do
      import.prepare_referential
      import.import_calendars
    end

    it "should create a Route for each trip" do
      import.import_trips

      defined_attributes = [
        "lines.registration_number", :wayback, :name, :published_name
      ]
      expected_attributes = [
        ["AB", "outbound", "to Bullfrog", "to Bullfrog"],
        ["AB", "inbound", "to Airport", "to Airport"],
        ["STBA", "inbound", "Shuttle", "Shuttle"],
        ["CITY", "outbound", "Outbound", "Outbound"],
        ["CITY", "inbound", "Inbound", "Inbound"],
        ["BFC", "outbound", "to Furnace Creek Resort", "to Furnace Creek Resort"],
        ["BFC", "inbound", "to Bullfrog", "to Bullfrog"],
        ["AAMV", "outbound", "to Amargosa Valley", "to Amargosa Valley"],
        ["AAMV", "inbound", "to Airport", "to Airport"],
        ["AAMV", "outbound", "to Amargosa Valley", "to Amargosa Valley"],
        ["AAMV", "inbound", "to Airport", "to Airport"]
      ]

      expect(import.referential.routes.includes(:line).pluck(*defined_attributes)).to match_array(expected_attributes)
    end

    it "should create a JourneyPattern for each trip" do
      import.import_trips

      defined_attributes = [
        :name
      ]
      expected_attributes = [
        "to Bullfrog", "to Airport", "Shuttle", "Outbound", "Inbound", "to Furnace Creek Resort", "to Bullfrog", "to Amargosa Valley", "to Airport", "to Amargosa Valley", "to Airport"
      ]

      expect(import.referential.journey_patterns.pluck(*defined_attributes)).to match_array(expected_attributes)
    end

    it "should create a VehicleJourney for each trip" do
      import.import_trips

      defined_attributes = ->(v) {
        [v.published_journey_name, v.time_tables.first&.comment]
      }
      expected_attributes = [
        ["to Bullfrog", "Calendar FULLW"],
        ["to Airport", "Calendar FULLW"],
        ["Shuttle", "Calendar FULLW"],
        ["CITY1", "Calendar FULLW"],
        ["CITY2", "Calendar FULLW"],
        ["to Furnace Creek Resort", "Calendar FULLW"],
        ["to Bullfrog", "Calendar FULLW"],
        ["to Amargosa Valley", "Calendar WE"],
        ["to Airport", "Calendar WE"],
        ["to Amargosa Valley", "Calendar WE"],
        ["to Airport", "Calendar WE"]
      ]

      expect(import.referential.vehicle_journeys.map(&defined_attributes)).to match_array(expected_attributes)
    end
  end

  describe "#import_stop_times" do
    let(:import) { create_import "google-sample-feed.zip" }

    before do
      import.prepare_referential
      import.import_calendars
      import.import_trips
    end

    it "should create a VehicleJourneyAtStop for each stop_time" do
      import.import_stop_times

      def t(value)
        Time.parse(value)
      end

      defined_attributes = [
        "stop_areas.registration_number", :position, :departure_time, :arrival_time,
      ]
      expected_attributes = [
        ["STAGECOACH", 0, t("2000-01-01 06:00:00 UTC"), t("2000-01-01 06:00:00 UTC")],
        ["BEATTY_AIRPORT", 1, t("2000-01-01 06:20:00 UTC"), t("2000-01-01 06:20:00 UTC")],
        ["STAGECOACH", 0, t("2000-01-01 06:00:00 UTC"), t("2000-01-01 06:00:00 UTC")],
        ["NANAA", 1, t("2000-01-01 06:07:00 UTC"), t("2000-01-01 06:05:00 UTC")],
        ["NADAV", 2, t("2000-01-01 06:14:00 UTC"), t("2000-01-01 06:12:00 UTC")],
        ["DADAN", 3, t("2000-01-01 06:21:00 UTC"), t("2000-01-01 06:19:00 UTC")],
        ["EMSI", 4, t("2000-01-01 06:28:00 UTC"), t("2000-01-01 06:26:00 UTC")],
        ["EMSI", 0, t("2000-01-01 06:30:00 UTC"), t("2000-01-01 06:28:00 UTC")],
        ["DADAN", 1, t("2000-01-01 06:37:00 UTC"), t("2000-01-01 06:35:00 UTC")],
        ["NADAV", 2, t("2000-01-01 06:44:00 UTC"), t("2000-01-01 06:42:00 UTC")],
        ["NANAA", 3, t("2000-01-01 06:51:00 UTC"), t("2000-01-01 06:49:00 UTC")],
        ["STAGECOACH", 4, t("2000-01-01 06:58:00 UTC"), t("2000-01-01 06:56:00 UTC")],
        ["BEATTY_AIRPORT", 0, t("2000-01-01 08:00:00 UTC"), t("2000-01-01 08:00:00 UTC")],
        ["BULLFROG", 1, t("2000-01-01 08:15:00 UTC"), t("2000-01-01 08:10:00 UTC")],
        ["BULLFROG", 0, t("2000-01-01 12:05:00 UTC"), t("2000-01-01 12:05:00 UTC")],
        ["BEATTY_AIRPORT", 1, t("2000-01-01 12:15:00 UTC"), t("2000-01-01 12:15:00 UTC")],
        ["BULLFROG", 0, t("2000-01-01 08:20:00 UTC"), t("2000-01-01 08:20:00 UTC")],
        ["FUR_CREEK_RES", 1, t("2000-01-01 09:20:00 UTC"), t("2000-01-01 09:20:00 UTC")],
        ["FUR_CREEK_RES", 0, t("2000-01-01 11:00:00 UTC"), t("2000-01-01 11:00:00 UTC")],
        ["BULLFROG", 1, t("2000-01-01 12:00:00 UTC"), t("2000-01-01 12:00:00 UTC")],
        ["BEATTY_AIRPORT", 0, t("2000-01-01 08:00:00 UTC"), t("2000-01-01 08:00:00 UTC")],
        ["AMV", 1, t("2000-01-01 09:00:00 UTC"), t("2000-01-01 09:00:00 UTC")],
        ["AMV", 0, t("2000-01-01 10:00:00 UTC"), t("2000-01-01 10:00:00 UTC")],
        ["BEATTY_AIRPORT", 1, t("2000-01-01 11:00:00 UTC"), t("2000-01-01 11:00:00 UTC")],
        ["BEATTY_AIRPORT", 0, t("2000-01-01 13:00:00 UTC"), t("2000-01-01 13:00:00 UTC")],
        ["AMV", 1, t("2000-01-01 14:00:00 UTC"), t("2000-01-01 14:00:00 UTC")],
        ["AMV", 0, t("2000-01-01 15:00:00 UTC"), t("2000-01-01 15:00:00 UTC")],
        ["BEATTY_AIRPORT", 1, t("2000-01-01 16:00:00 UTC"), t("2000-01-01 16:00:00 UTC")]
      ]
      expect(referential.vehicle_journey_at_stops.includes(stop_point: :stop_area).pluck(*defined_attributes)).to match_array(expected_attributes)
    end
  end

  describe "#import_calendars" do
    let(:import) { create_import "google-sample-feed.zip" }

    before do
      import.prepare_referential
    end

    it "should create a Timetable for each calendar" do
      import.import_calendars

      def d(value)
        Date.parse(value)
      end

      defined_attributes = ->(t) {
        [t.comment, t.valid_days, t.periods.first.period_start, t.periods.first.period_end]
      }
      expected_attributes = [
        ["Calendar FULLW", [1, 2, 3, 4, 5, 6, 7], d("Mon, 01 Jan 2007"), d("Fri, 31 Dec 2010")],
        ["Calendar WE", [6, 7], d("Mon, 01 Jan 2007"), d("Fri, 31 Dec 2010")]
      ]
      expect(referential.time_tables.map(&defined_attributes)).to match_array(expected_attributes)
    end
  end

  describe "#import_calendar_dates" do
    let(:import) { create_import "google-sample-feed.zip" }

    before do
      import.prepare_referential
      import.import_calendars
    end

    it "should create a Timetable::Date for each calendar date" do
      import.import_calendar_dates

      def d(value)
        Date.parse(value)
      end

      defined_attributes = ->(d) {
        [d.time_table.comment, d.date, d.in_out]
      }
      expected_attributes = [
        ["Calendar FULLW", d("Mon, 04 Jun 2007"), false]
      ]
      expect(referential.time_table_dates.map(&defined_attributes)).to match_array(expected_attributes)
    end
  end

  describe "#download_local_file" do
    let(:file) { "google-sample-feed.zip" }
    let(:import) do
      Import::Gtfs.create! name: "GTFS test", creator: "Test", workbench: workbench, file: open_fixture(file), download_host: "rails_host"
    end

    let(:download_url) { "#{import.download_host}/workbenches/#{import.workbench_id}/imports/#{import.id}/download?token=#{import.token_download}" }

    before do
      stub_request(:get, download_url).to_return(status: 200, body: read_fixture(file))
    end

    it "should download local_file" do
      expect(File.read(import.download_local_file)).to eq(read_fixture(file))
    end
  end

  describe "#download_uri" do
    let(:import) { Import::Gtfs.new }

    before do
      allow(import).to receive(:download_path).and_return("/download_path")
    end

    context "when download_host is 'front'" do
      before { allow(import).to receive(:download_host).and_return("front") }
      it "returns http://front/download_path" do
        expect(import.download_uri.to_s).to eq('http://front/download_path')
      end
    end

    context "when download_host is 'front:3000'" do
      before { allow(import).to receive(:download_host).and_return("front:3000") }
      it "returns http://front:3000/download_path" do
        expect(import.download_uri.to_s).to eq('http://front:3000/download_path')
      end
    end

    context "when download_host is 'http://front:3000'" do
      before { allow(import).to receive(:download_host).and_return("http://front:3000") }
      it "returns http://front:3000/download_path" do
        expect(import.download_uri.to_s).to eq('http://front:3000/download_path')
      end
    end

    context "when download_host is 'https://front:3000'" do
      before { allow(import).to receive(:download_host).and_return("https://front:3000") }
      it "returns https://front:3000/download_path" do
        expect(import.download_uri.to_s).to eq('https://front:3000/download_path')
      end
    end

    context "when download_host is 'http://front'" do
      before { allow(import).to receive(:download_host).and_return("http://front") }
      it "returns http://front/download_path" do
        expect(import.download_uri.to_s).to eq('http://front/download_path')
      end
    end

  end

  describe "#download_host" do
    it "should return host defined by Rails.application.config.rails_host" do
      allow(Rails.application.config).to receive(:rails_host).and_return("download_host")
      expect(Import::Gtfs.new.download_host).to eq("download_host")
    end
  end

  describe "#download_path" do
    let(:file) { "google-sample-feed.zip" }
    let(:import) do
      Import::Gtfs.create! name: "GTFS test", creator: "Test", workbench: workbench, file: open_fixture(file), download_host: "rails_host"
    end

    it "should return the pathwith the token" do
      expect(import.download_path).to eq("/workbenches/#{import.workbench_id}/imports/#{import.id}/download?token=#{import.token_download}")
    end
  end
end