aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuc Donnet2014-06-30 15:58:09 +0200
committerLuc Donnet2014-06-30 15:58:09 +0200
commit89dd167379ae6ff6420be959f31ed5b1755cbaca (patch)
tree792e4dfc426a0e2019aefd44b25a5eddb716ac1a
parent486d7f1eca4c35bfcc065feb16cec4429a66cfa3 (diff)
downloadchouette-core-89dd167379ae6ff6420be959f31ed5b1755cbaca.tar.bz2
Add spec requests for vehicle journey import
-rw-r--r--app/assets/javascripts/plugins/tooltip.js.coffee2
-rw-r--r--app/assets/javascripts/vehicle_journey_import.js.coffee0
-rw-r--r--app/controllers/vehicle_journey_imports_controller.rb27
-rw-r--r--app/models/vehicle_journey_import.rb5
-rw-r--r--app/views/vehicle_journey_imports/new.html.erb13
-rw-r--r--config/locales/vehicle_journey_imports.yml27
-rw-r--r--spec/controllers/vehicle_journey_imports_controller_spec.rb7
-rw-r--r--spec/requests/vehicle_journey_imports_spec.rb59
8 files changed, 94 insertions, 46 deletions
diff --git a/app/assets/javascripts/plugins/tooltip.js.coffee b/app/assets/javascripts/plugins/tooltip.js.coffee
new file mode 100644
index 000000000..ca521279e
--- /dev/null
+++ b/app/assets/javascripts/plugins/tooltip.js.coffee
@@ -0,0 +1,2 @@
+jQuery ->
+ $('input').tooltip() \ No newline at end of file
diff --git a/app/assets/javascripts/vehicle_journey_import.js.coffee b/app/assets/javascripts/vehicle_journey_import.js.coffee
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/app/assets/javascripts/vehicle_journey_import.js.coffee
diff --git a/app/controllers/vehicle_journey_imports_controller.rb b/app/controllers/vehicle_journey_imports_controller.rb
index e3a6dde77..d58b3c2b0 100644
--- a/app/controllers/vehicle_journey_imports_controller.rb
+++ b/app/controllers/vehicle_journey_imports_controller.rb
@@ -10,36 +10,15 @@ class VehicleJourneyImportsController < ChouetteController
def new
@vehicle_journey_import = VehicleJourneyImport.new(:route => route)
- flash[:notice] = "A CSV or Excel file can be used to import records. The first row should be the column name.
-<p>
-The following columns are allowed :
-<ul>
- <li>
- <strong>stop_point_id</strong> -
- Integer type
- </li>
- <li>
- <strong>stop_area_name</strong> -
- String type
- </li>
- <li>
- <strong>published_journey_name </strong> -
- String type
- </li>
- <li>
- <strong>published_journey_name </strong> -
- String type ....
- </li>
-</ul>
-</p>"
new!
end
def create
- @vehicle_journey_import = VehicleJourneyImport.new(params[:vehicle_journey_import].merge({:route => route}))
+ @vehicle_journey_import = VehicleJourneyImport.new( params[:vehicle_journey_import].present? ? params[:vehicle_journey_import].merge({:route => route}) : {:route => route} )
if @vehicle_journey_import.save
- redirect_to referential_line_route_path( @referential, @line, @route ), notice: "Import successful"
+ redirect_to referential_line_route_path( @referential, @line, @route ), notice: I18n.t("vehicle_journey_imports.new.success")
else
+ flash[:error] = I18n.t("stop_area_imports.errors.import_aborted") + "<br>" + @vehicle_journey_import.errors.full_messages.join("<br>")
render :new
end
end
diff --git a/app/models/vehicle_journey_import.rb b/app/models/vehicle_journey_import.rb
index 8da54abc6..15569c3d4 100644
--- a/app/models/vehicle_journey_import.rb
+++ b/app/models/vehicle_journey_import.rb
@@ -11,7 +11,7 @@ class VehicleJourneyImport
validates_presence_of :route
def initialize(attributes = {})
- attributes.each { |name, value| send("#{name}=", value) }
+ attributes.each { |name, value| send("#{name}=", value) } if attributes
end
def persisted?
@@ -34,7 +34,8 @@ class VehicleJourneyImport
end
end
rescue Exception => exception
- errors.add :base, I18n.t("vehicle_journey_imports.errors.exception", :message => exception.message)
+ Rails.logger.error(exception.message)
+ errors.add :base, I18n.t("vehicle_journey_imports.errors.exception")
false
end
end
diff --git a/app/views/vehicle_journey_imports/new.html.erb b/app/views/vehicle_journey_imports/new.html.erb
index a8459848c..65bfd6b17 100644
--- a/app/views/vehicle_journey_imports/new.html.erb
+++ b/app/views/vehicle_journey_imports/new.html.erb
@@ -13,19 +13,8 @@
</div>
<%= semantic_form_for [@referential, @line, @route, @vehicle_journey_import] do |form| %>
- <% if @vehicle_journey_import.errors.any? %>
- <div id="error_explanation">
- <h2><%= pluralize(@vehicle_journey_import.errors.count, "error") %> prohibited this import from completing:</h2>
- <ul>
- <% @vehicle_journey_import.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
- <br>
<%= form.inputs do %>
- <%= form.input :file, :as => :file %>
+ <%= form.input :file, :as => :file, :input_html => { :title => "#{t('.tooltip.file')}", :'data-placement' => "right", :'data-toggle' => "tooltip", :'data-trigger' =>"hover" } %>
<% end %>
<%= form.actions do %>
<%= form.action :submit, :as => :button , :label => t( 'formtastic.import' ) %>
diff --git a/config/locales/vehicle_journey_imports.yml b/config/locales/vehicle_journey_imports.yml
index f3d3be918..476996f41 100644
--- a/config/locales/vehicle_journey_imports.yml
+++ b/config/locales/vehicle_journey_imports.yml
@@ -2,24 +2,35 @@ en:
vehicle_journey_imports:
new:
title: "Import vehicle journey at stops"
- export_vehicle_journeys: "Export existing vehicle journey at stops :"
- form:
- file: "File"
+ export_vehicle_journeys: "Export existing vehicle journey at stops"
+ succcess: "Import is a success"
+ tooltip:
+ file: "Select a CSV or Excel file"
errors:
+ import_aborted: "Des erreurs ont empéchées le bon déroulement de l'import: "
not_same_stop_points: "Error column 1 : Not same stop points than in route %{route}"
invalid_vehicle_journey_at_stop: "Error column %{column} line %{line} : vehicle journey at stop invalid %{time}"
invalid_vehicle_journey: "Error column %{column}, vehicle journey is invalid : %{message}"
- exception: "An exception occured : %{message}"
+ exception: "Invalid file, you must provide valid csv, xls or xlsx file"
+ activemodel:
+ attributes:
+ vehicle_journey_import:
+ file: "File"
fr:
vehicle_journey_imports:
new:
title: "Import des horaires à l'arrêt"
export_vehicle_journeys: "Exporter les horaires existants"
- form:
- file: "Fichier"
+ succcess: "L'import des données est un succès"
+ tooltip:
+ file: "Sélectionner un fichier CSV ou Excel"
errors:
+ import_aborted: "Des erreurs ont empéchées le bon déroulement de l'import: "
not_same_stop_points: "Erreur colonne 1 : Pas les mêmes points d'arrêt que sur l'itinéraire %{route}"
invalid_vehicle_journey_at_stop: "Erreur colonne %{column} ligne %{line} : horaire à l'arrêt invalide %{time}"
invalid_vehicle_journey: "Erreur colonne %{column}, la course est invalide : %{message}"
- exception: "Une exception est survenu : %{message}"
- \ No newline at end of file
+ exception: "Le fichier est invalide, vous devez fournir un fichier csv, xls ou xlsx valide"
+ activemodel:
+ attributes:
+ vehicle_journey_import:
+ file: "Fichier" \ No newline at end of file
diff --git a/spec/controllers/vehicle_journey_imports_controller_spec.rb b/spec/controllers/vehicle_journey_imports_controller_spec.rb
new file mode 100644
index 000000000..091826205
--- /dev/null
+++ b/spec/controllers/vehicle_journey_imports_controller_spec.rb
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe ImportTasksController do
+ login_user
+
+
+end
diff --git a/spec/requests/vehicle_journey_imports_spec.rb b/spec/requests/vehicle_journey_imports_spec.rb
new file mode 100644
index 000000000..c7a772fd3
--- /dev/null
+++ b/spec/requests/vehicle_journey_imports_spec.rb
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+require 'spec_helper'
+
+describe "VehicleJourneyImports" 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") do |csv|
+ counter = 0
+ CSV.foreach( Rails.root.join("spec", "fixtures", "#{filename}").to_s ) do |row|
+ if counter == 0
+ csv << row
+ else
+ csv << ( row[0] = route.stop_points[counter - 1].id; row)
+ end
+ counter += 1
+ end
+
+ end
+
+ File.open("/tmp/#{filename}")
+ end
+
+ describe "new" do
+ it "should create stop areas and return to stop areas index 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("Séquence d'arrêts #{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