aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/vehicle_journey_import_spec.rb
blob: 7b31dc80613d697fda04ceaea6ffbd7c0c62939d (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
# -*- coding: utf-8 -*-
require 'spec_helper'
require 'csv'

describe VehicleJourneyImport, :type => :model do

  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 = vehicle_journey1.id.to_s if cell == "import:VehicleJourney:1"
            cell = vehicle_journey2.id.to_s if cell == "import:VehicleJourney:2"
            cell = vehicle_journey3.id.to_s 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

  let!(:route) { create(:route) }
  let!(:other_route) { create(:route) }

  let!(:journey_pattern) { create(:journey_pattern, :route => route) }
  let!(:other_journey_pattern) { create(:journey_pattern_even, :route => route) }

  let!(:vehicle_journey1) { create(:vehicle_journey_common, :objectid => "import:VehicleJourney:1:loc", :route_id => route.id, :journey_pattern_id => journey_pattern.id) }
  let!(:vehicle_journey2) { create(:vehicle_journey_common, :objectid => "import:VehicleJourney:2:loc", :route_id => route.id, :journey_pattern_id => other_journey_pattern.id) }
  let!(:vehicle_journey3) { create(:vehicle_journey_common, :objectid => "import:VehicleJourney:3:loc", :route_id => route.id, :journey_pattern_id => journey_pattern.id) }

  let!(:stop_point0) { route.stop_points[0] }
  let!(:stop_point1) { route.stop_points[1] }
  let!(:stop_point2) { route.stop_points[2] }
  let!(:stop_point3) { route.stop_points[3] }
  let!(:stop_point4) { route.stop_points[4] }


  # Must use uploaded file and not classical ruby File!
  let(:valid_file) {
    csv_file = update_csv_file_with_factory_data("vehicle_journey_imports_valid.csv")
    double("CSV", :tempfile => csv_file, :original_filename => File.basename(csv_file), :path => File.path(csv_file) )
  }

  let(:invalid_file_on_vj) {
    csv_file = update_csv_file_with_factory_data("vehicle_journey_imports_with_vj_invalid.csv")
    double("CSV", :tempfile => csv_file, :original_filename => File.basename(csv_file), :path => File.path(csv_file) )
  }

  let(:invalid_file_on_vjas) {
    csv_file = update_csv_file_with_factory_data("vehicle_journey_imports_with_vjas_invalid.csv")
    double("CSV", :tempfile => csv_file, :original_filename => File.basename(csv_file), :path => File.path(csv_file) )
  }

  let(:invalid_file_on_vjas_object) {
    csv_file = update_csv_file_with_factory_data("vehicle_journey_imports_with_vjas_bad_order.csv")
    double("CSV", :tempfile => csv_file, :original_filename => File.basename(csv_file), :path => File.path(csv_file) )
  }

  subject { VehicleJourneyImport.new(:route => route, :file => valid_file) }

  describe ".save" do

    it "should validate presence of route" do
      expect(VehicleJourneyImport.new(:route => route).save).to be_falsey
    end

    it "should validate presence of file" do
      expect(VehicleJourneyImport.new(:file => valid_file).save).to be_falsey
    end

    it "should import vehicle_journeys and create the right number of objects" do
      expect(VehicleJourneyImport.new(:file => valid_file, :route => route).save).to be_truthy
      expect(Chouette::VehicleJourney.all.size).to eq(4)
      expect(Chouette::VehicleJourneyAtStop.all.size).to eq(17)
    end

    it "should not import vehicle_journeys and not create objects when vehicle journey at stops are not in ascendant order", :skip => "Time gap validation is in pending status" do
      expect(VehicleJourneyImport.new(:route => route, :file => invalid_file_on_vjas_object).save).to be_falsey
      expect(Chouette::VehicleJourney.all.size).to eq(3)
      expect(Chouette::VehicleJourneyAtStop.all.size).to eq(0)
    end

    # it "should not import vehicle_journeys and not create objects with invalid file" do
    #   expect(VehicleJourneyImport.new(:file => invalid_file_on_vj, :route => route).save).to be_false
    #   expect(Chouette::VehicleJourney.all.size).to eq(3)
    #   expect(Chouette::VehicleJourneyAtStop.all.size).to eq(0)
    # end

  end

  describe ".find_journey_pattern_schedule" do

    it "should return journey pattern with same stop points" do
      expect(subject.find_journey_pattern_schedule( 1, { stop_point0.id => "9:00", stop_point1.id => "9:05", stop_point2.id => "9:10", stop_point3.id => "9:15", stop_point4.id => "9:20"} )).to eq(journey_pattern)
      expect(subject.find_journey_pattern_schedule( 1, { stop_point1.id => "9:00", stop_point3.id => "9:10" } )).to eq(other_journey_pattern)
    end

    it "should return new journey_pattern if no journey pattern with same stop points is founded" do
      expect(subject.find_journey_pattern_schedule( 1, { stop_point0.id => "9:00", stop_point1.id => "9:05", stop_point2.id => nil, stop_point3.id => "9:15", stop_point4.id => "9:20"} )).to be_truthy
      expect(subject.find_journey_pattern_schedule( 1, { stop_point0.id => "9:00", stop_point1.id => "9:05", stop_point2.id => nil, stop_point3.id => "9:15", stop_point4.id => "9:20"} ).id).not_to eq(journey_pattern.id)
      expect(subject.find_journey_pattern_schedule( 1, { stop_point0.id => "9:00", stop_point1.id => "9:05", stop_point2.id => nil, stop_point3.id => "9:15", stop_point4.id => "9:20"} ).id).not_to eq(other_journey_pattern.id)
    end

  end

  describe ".load_imported_vehicle_journeys" do

    it "should return false when stop points in file are not the same in the route" do
      vehicle_journey_import = VehicleJourneyImport.new(:route => other_route, :file => valid_file)
      expect { vehicle_journey_import.load_imported_vehicle_journeys }.to raise_error(RuntimeError)
    end

    # it "should return errors when vehicle journeys in file are invalid" do
    #   vehicle_journey_import = VehicleJourneyImport.new(:route => route, :file => invalid_file_on_vj)

    #   expect { vehicle_journey_import.load_imported_vehicle_journeys }.to raise_error
    # end

    it "should return errors when vehicle journey at stops in file are invalid" do
      vehicle_journey_import = VehicleJourneyImport.new(:route => route, :file => invalid_file_on_vjas)
      expect { vehicle_journey_import.load_imported_vehicle_journeys }.to raise_error(ArgumentError)
    end

    it "should return errors when vehicle journey at stops are not in ascendant order" do
      vehicle_journey_import = VehicleJourneyImport.new(:route => route, :file => invalid_file_on_vjas_object)
      expect(vehicle_journey_import.load_imported_vehicle_journeys.size).to eq(3)
      expect(vehicle_journey_import.errors.messages).to be_empty
    end

    it "should load vehicle journeys" do
      expect(subject.load_imported_vehicle_journeys.size).to eq(4)
      expect(subject.errors.messages).to eq({})
    end

  end



end