diff options
| author | Zog | 2018-01-30 16:49:22 +0100 |
|---|---|---|
| committer | Johan Van Ryseghem | 2018-02-20 09:50:28 +0100 |
| commit | 51e08724766bf2ca4837436178984c33d22cf16a (patch) | |
| tree | 247dc64ac91662e22c6f953ba8e14632fc3a983c /spec | |
| parent | a01519fc871e22a220157cfa8c8d6d5b5c80f5cb (diff) | |
| download | chouette-core-51e08724766bf2ca4837436178984c33d22cf16a.tar.bz2 | |
Refs #5765 @6h; Add a customizable importer mechanism
Diffstat (limited to 'spec')
6 files changed, 160 insertions, 0 deletions
diff --git a/spec/fixtures/simple_importer/stop_area.csv b/spec/fixtures/simple_importer/stop_area.csv new file mode 100644 index 000000000..9361d022b --- /dev/null +++ b/spec/fixtures/simple_importer/stop_area.csv @@ -0,0 +1,2 @@ +name;lat;long;type;street_name +Nom du Stop;45.00;12;ZDEP;99 rue des Poissonieres diff --git a/spec/fixtures/simple_importer/stop_area_full.csv b/spec/fixtures/simple_importer/stop_area_full.csv new file mode 100644 index 000000000..250caab30 --- /dev/null +++ b/spec/fixtures/simple_importer/stop_area_full.csv @@ -0,0 +1,3 @@ +"id";"station_code";"uic_code";"country_code";"province";"district";"county";"station_name";"inactive";"change_timestamp";"longitude";"latitude";"parent_station_code";"additional_info";"external_reference";"timezone";"address";"postal_code";"city" +5669;"PAR";"PAR";"FRA";"";"";"";"Paris - All stations";f;"2017-07-17 11:56:53.138";2.35222190000002;48.856614;"";"";"{""Région"":""Ile-de-France""}";"Europe/Paris";"";"";"" +5748;"XED";"XED";"FRA";"";"";"";"Paris MLV";t;"2017-05-29 11:24:34.575";2.783409;48.870569;"PAR";"";"{""Région"":""Ile-de-France""}";"Europe/Paris";"";"";"" diff --git a/spec/fixtures/simple_importer/stop_area_full_reverse.csv b/spec/fixtures/simple_importer/stop_area_full_reverse.csv new file mode 100644 index 000000000..9ea15f6cc --- /dev/null +++ b/spec/fixtures/simple_importer/stop_area_full_reverse.csv @@ -0,0 +1,3 @@ +"id";"station_code";"uic_code";"country_code";"province";"district";"county";"station_name";"inactive";"change_timestamp";"longitude";"latitude";"parent_station_code";"additional_info";"external_reference";"timezone";"address";"postal_code";"city" +5748;"XED";"XED";"FRA";"";"";"";"Paris MLV";t;"2017-05-29 11:24:34.575";2.783409;48.870569;"PAR";"";"{""Région"":""Ile-de-France""}";"Europe/Paris";"";"";"" +5669;"PAR";"PAR";"FRA";"";"";"";"Paris - All stations";f;"2017-07-17 11:56:53.138";2.35222190000002;48.856614;"";"";"{""Région"":""Ile-de-France""}";"Europe/Paris";"";"";"" diff --git a/spec/fixtures/simple_importer/stop_area_incomplete.csv b/spec/fixtures/simple_importer/stop_area_incomplete.csv new file mode 100644 index 000000000..cd9447acd --- /dev/null +++ b/spec/fixtures/simple_importer/stop_area_incomplete.csv @@ -0,0 +1,2 @@ +name;lat;long;type;street_name +;45.00;12;ZDEP diff --git a/spec/fixtures/simple_importer/stop_area_missing_street_name.csv b/spec/fixtures/simple_importer/stop_area_missing_street_name.csv new file mode 100644 index 000000000..aa845c3f5 --- /dev/null +++ b/spec/fixtures/simple_importer/stop_area_missing_street_name.csv @@ -0,0 +1,2 @@ +name;lat;long;type;foo +Nom du Stop;45.00;12;ZDEP;blabla diff --git a/spec/models/simple_importer_spec.rb b/spec/models/simple_importer_spec.rb new file mode 100644 index 000000000..50958970c --- /dev/null +++ b/spec/models/simple_importer_spec.rb @@ -0,0 +1,148 @@ +RSpec.describe SimpleImporter do + describe "#define" do + context "with an incomplete configuration" do + + it "should raise an error" do + expect do + SimpleImporter.define :foo + end.to raise_error + end + end + context "with a complete configuration" do + before do + SimpleImporter.define :foo do |config| + config.model = "example" + end + end + + it "should define an importer" do + expect{SimpleImporter.find_configuration(:foo)}.to_not raise_error + expect{SimpleImporter.new(configuration_name: :foo, filepath: "")}.to_not raise_error + expect{SimpleImporter.find_configuration(:bar)}.to raise_error + expect{SimpleImporter.new(configuration_name: :bar, filepath: "")}.to raise_error + expect{SimpleImporter.create(configuration_name: :foo, filepath: "")}.to change{SimpleImporter.count}.by 1 + end + end + end + + describe "#import" do + before(:each) do + SimpleImporter.define :test do |config| + config.model = Chouette::StopArea + config.separator = ";" + config.key = "name" + config.add_column :name + config.add_column :lat, attribute: :latitude + config.add_column :lat, attribute: :longitude, value: ->(raw){ raw.to_f + 1 } + config.add_column :type, attribute: :area_type, value: ->(raw){ raw&.downcase } + config.add_column :street_name + config.add_column :stop_area_referential, value: create(:stop_area_referential, objectid_format: :stif_netex) + end + end + + it "should import the given file" do + importer = SimpleImporter.new(configuration_name: :test, filepath: Rails.root + "spec/fixtures/simple_importer/stop_area.csv") + expect{importer.import}.to change{Chouette::StopArea.count}.by 1 + expect(importer.status).to eq "success" + stop = Chouette::StopArea.last + expect(stop.name).to eq "Nom du Stop" + expect(stop.latitude).to eq 45.00 + expect(stop.longitude).to eq 46.00 + expect(stop.area_type).to eq "zdep" + expect(importer.reload.journal.last["event"]).to eq("creation") + end + + context "with an already existing record" do + before(:each){ + create :stop_area, name: "Nom du Stop" + } + it "should only update the record" do + importer = SimpleImporter.new(configuration_name: :test, filepath: Rails.root + "spec/fixtures/simple_importer/stop_area.csv") + expect{importer.import}.to change{Chouette::StopArea.count}.by 0 + expect(importer.status).to eq "success" + stop = Chouette::StopArea.last + expect(stop.name).to eq "Nom du Stop" + expect(stop.latitude).to eq 45.00 + expect(stop.longitude).to eq 46.00 + expect(stop.area_type).to eq "zdep" + expect(importer.reload.journal.last["event"]).to eq("update") + end + end + + context "with a missing column" do + it "should set an error message" do + importer = SimpleImporter.new(configuration_name: :test, filepath: Rails.root + "spec/fixtures/simple_importer/stop_area_missing_street_name.csv") + expect{importer.import}.to_not raise_error + expect(importer.status).to eq "success_with_warnings" + expect(importer.reload.journal.first["event"]).to eq("column_not_found") + end + end + + context "with a incomplete dataset" do + it "should create a StopArea" do + importer = SimpleImporter.new(configuration_name: :test, filepath: Rails.root + "spec/fixtures/simple_importer/stop_area_incomplete.csv") + expect{importer.import}.to_not raise_error + expect(importer.status).to eq "failed" + expect(importer.reload.journal.first["message"]).to eq({"name" => ["doit être rempli(e)"]}) + end + end + + context "with a wrong filepath" do + it "should create a StopArea" do + importer = SimpleImporter.new(configuration_name: :test, filepath: Rails.root + "spec/fixtures/simple_importer/not_found.csv") + expect{importer.import}.to_not raise_error + expect(importer.status).to eq "failed" + expect(importer.reload.journal.first["message"]).to eq "File not found: #{importer.filepath}" + end + end + + context "with a full file" do + before(:each) do + SimpleImporter.define :test do |config| + config.model = Chouette::StopArea + config.separator = ";" + config.key = "station_code" + config.add_column :station_code, attribute: :registration_number + config.add_column :country_code + config.add_column :station_name, attribute: :name + config.add_column :inactive, attribute: :deleted_at, value: ->(raw){ raw == "t" ? Time.now : nil } + config.add_column :change_timestamp, attribute: :updated_at + config.add_column :longitude + config.add_column :latitude + config.add_column :parent_station_code, attribute: :parent, value: ->(raw){ raw.present? && resolve(:station_code, raw){|value| Chouette::StopArea.find_by(registration_number: value) } } + config.add_column :parent_station_code, attribute: :area_type, value: ->(raw){ raw.present? ? "zdep" : "gdl" } + config.add_column :timezone, attribute: :time_zone + config.add_column :address, attribute: :street_name + config.add_column :postal_code, attribute: :zip_code + config.add_column :city, attribute: :city_name + config.add_value :stop_area_referential_id, create(:stop_area_referential, objectid_format: :stif_netex).id + config.add_value :long_lat_type, "WGS84" + end + end + + it "should import the given file" do + importer = SimpleImporter.new(configuration_name: :test, filepath: Rails.root + "spec/fixtures/simple_importer/stop_area_full.csv") + expect{importer.import}.to change{Chouette::StopArea.count}.by 2 + expect(importer.status).to eq "success" + first = Chouette::StopArea.find_by registration_number: "PAR" + last = Chouette::StopArea.find_by registration_number: "XED" + + expect(last.parent).to eq first + expect(first.area_type).to eq "gdl" + expect(last.area_type).to eq "zdep" + expect(first.long_lat_type).to eq "WGS84" + end + + context "with a relation in reverse order" do + it "should import the given file" do + importer = SimpleImporter.new(configuration_name: :test, filepath: Rails.root + "spec/fixtures/simple_importer/stop_area_full_reverse.csv") + expect{importer.import}.to change{Chouette::StopArea.count}.by 2 + expect(importer.status).to eq "success" + first = Chouette::StopArea.find_by registration_number: "XED" + last = Chouette::StopArea.find_by registration_number: "PAR" + expect(first.parent).to eq last + end + end + end + end +end |
