| 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
 | # -*- coding: utf-8 -*-
require 'spec_helper'
require 'csv'
describe "VehicleJourneyImports", :type => :feature do
  login_user
  let!(:route) { create :route }
  let(:valid_file_path) {
    csv_file = update_csv_file_with_factory_data("vehicle_journey_imports_valid.csv")
    File.path(csv_file)
  }
  let(:invalid_file_path) {
    csv_file = update_csv_file_with_factory_data("vehicle_journey_imports_with_vjas_invalid.csv")
    File.path(csv_file)
  }
  def update_csv_file_with_factory_data(filename)
    csv_file = CSV.open("/tmp/#{filename}", "wb",{ :col_sep => ";"}) do |csv|
      counter = 0
      CSV.foreach( Rails.root.join("spec", "fixtures", "#{filename}").to_s , {:col_sep => ";"}) do |row|
        if counter == 0
          row2 = []
          row.each do |cell|
            cell = "" if cell == "import:VehicleJourney:1"
            cell = "" if cell == "import:VehicleJourney:2"
            cell = "" if cell == "import:VehicleJourney:3"
            row2 << cell
          end
          csv << row2
        elsif  counter < 8
          csv << row
        else
          csv << ( row[0] = route.stop_points[counter - 8].id; row)
        end
        counter += 1
      end
    end
    File.open("/tmp/#{filename}")
  end
  describe "new" do
    it "should create vehicle journey file and return to route show page" do
      visit new_referential_line_route_vehicle_journey_import_path(referential, route.line, route)
      attach_file('Fichier', valid_file_path)
      click_button "Lancer l'import"
      expect(page).to have_content(I18n.t("vehicle_journey_imports.new.success"))
      expect(page).to have_content(route.name)
    end
    it "should return error messages when file is invalid" do
      visit new_referential_line_route_vehicle_journey_import_path(referential, route.line, route)
      attach_file('Fichier', invalid_file_path)
      click_button "Lancer l'import"
      expect(page).to have_content(I18n.t("vehicle_journey_imports.errors.import_aborted"))
    end
    it "should return error message when file missing on upload" do
      visit new_referential_line_route_vehicle_journey_import_path(referential, route.line, route)
      click_button "Lancer l'import"
      expect(page).to have_content(I18n.t("vehicle_journey_imports.errors.import_aborted"))
    end
  end
end
 |