diff options
Diffstat (limited to 'spec')
18 files changed, 546 insertions, 74 deletions
diff --git a/spec/controllers/referentials_controller_spec.rb b/spec/controllers/referentials_controller_spec.rb index f97480600..5e0b1e505 100644 --- a/spec/controllers/referentials_controller_spec.rb +++ b/spec/controllers/referentials_controller_spec.rb @@ -15,8 +15,7 @@ describe ReferentialsController, :type => :controller do end context "user's organisation doesn't match referential's organisation" do - pending "hotfix opens all unknow actions need to close the uneeded later" do - #it 'raises a ActiveRecord::RecordNotFound' do + it 'raises a ActiveRecord::RecordNotFound' do expect { put :archive, id: other_referential.id }.to raise_error(ActiveRecord::RecordNotFound) end end @@ -26,7 +25,7 @@ describe ReferentialsController, :type => :controller do it 'gets compliance control set for current organisation' do compliance_control_set = create(:compliance_control_set, organisation: @user.organisation) create(:compliance_control_set) - get :select_compliance_control_set, referential_id: referential.id + get :select_compliance_control_set, id: referential.id expect(assigns[:compliance_control_sets]).to eq([compliance_control_set]) end end @@ -43,16 +42,51 @@ describe ReferentialsController, :type => :controller do end end + describe "GET #new" do + context "when duplicating" do + let(:workbench){ create :workbench} + let(:request){ + get :new, + workbench_id: workbench.id, + from: referential.id + } + + it "duplicates the given referential" do + request + new_referential = assigns(:referential) + expect(new_referential.line_referential).to eq referential.line_referential + expect(new_referential.stop_area_referential).to eq referential.stop_area_referential + expect(new_referential.objectid_format).to eq referential.objectid_format + expect(new_referential.prefix).to eq referential.prefix + expect(new_referential.slug).to eq "#{referential.slug}_clone" + expect(new_referential.workbench).to eq workbench + end + end + end + describe "POST #create" do + let(:workbench){ create :workbench} context "when duplicating" do - it "displays a flash message", pending: 'requires more params to create a valid Referential' do + let(:request){ post :create, - from: referential.id, - current_workbench_id: referential.workbench_id, - referential: { - name: 'Duplicated' - } + workbench_id: workbench.id, + referential: { + name: 'Duplicated', + created_from_id: referential.id, + stop_area_referential: referential.stop_area_referential, + line_referential: referential.line_referential, + objectid_format: referential.objectid_format, + workbench_id: referential.workbench_id + } + } + + it "creates the new referential" do + expect{request}.to change{Referential.count}.by 1 + expect(Referential.last.name).to eq "Duplicated" + end + it "displays a flash message" do + request expect(controller).to set_flash[:notice].to( I18n.t('notice.referentials.duplicate') ) diff --git a/spec/decorators/referential_decorator_spec.rb b/spec/decorators/referential_decorator_spec.rb index efc438132..1224aaf75 100644 --- a/spec/decorators/referential_decorator_spec.rb +++ b/spec/decorators/referential_decorator_spec.rb @@ -1,7 +1,8 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do include Support::DecoratorHelpers - let( :object ){ build_stubbed :referential } + let( :workbench ){ build_stubbed :workbench } + let( :object ){ build_stubbed :referential, workbench: workbench } let( :referential ){ object } let( :user ){ build_stubbed :user } @@ -35,7 +36,7 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do expect_action_link_hrefs.to eq([ [object], referential_time_tables_path(object), - new_referential_path(from: object) + new_workbench_referential_path(referential.workbench, from: object.id) ]) end end @@ -50,8 +51,8 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do [object], [:edit, object], referential_time_tables_path(object), - new_referential_path(from: object), - referential_select_compliance_control_set_path(object), + new_workbench_referential_path(referential.workbench, from: object.id), + select_compliance_control_set_referential_path(object), archive_referential_path(object), referential_path(object) ]) @@ -65,8 +66,8 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do expect_action_link_hrefs(action).to eq([ [:edit, object], referential_time_tables_path(object), - new_referential_path(from: object), - referential_select_compliance_control_set_path(object), + new_workbench_referential_path(referential.workbench, from: object.id), + select_compliance_control_set_referential_path(object), archive_referential_path(object), "#", referential_path(object) @@ -91,7 +92,7 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do expect_action_link_hrefs.to eq([ [object], referential_time_tables_path(object), - new_referential_path(from: object) + new_workbench_referential_path(referential.workbench, from: object.id) ]) end end diff --git a/spec/features/referentials_spec.rb b/spec/features/referentials_spec.rb index 9af0ed32e..d4890fda4 100644 --- a/spec/features/referentials_spec.rb +++ b/spec/features/referentials_spec.rb @@ -55,7 +55,7 @@ describe "Referentials", :type => :feature do context 'user has the permission to create referentials' do it 'shows the clone link for referetnial' do - expect(page).to have_link(I18n.t('actions.clone'), href: new_referential_path(from: referential.id)) + expect(page).to have_link(I18n.t('actions.clone'), href: new_workbench_referential_path(referential.workbench, from: referential.id)) end end @@ -63,7 +63,7 @@ describe "Referentials", :type => :feature do it 'does not show the clone link for referetnial' do @user.update_attribute(:permissions, []) visit referential_path(referential) - expect(page).not_to have_link(I18n.t('actions.clone'), href: new_referential_path(from: referential.id)) + expect(page).not_to have_link(I18n.t('actions.clone'), href: new_workbench_referential_path(referential.workbench, from: referential.id)) end end @@ -108,8 +108,9 @@ describe "Referentials", :type => :feature do end describe "create" do + let(:workbench){ @user.organisation.workbenches.last } it "should" do - visit new_referential_path + visit new_workbench_referential_path(workbench) fill_in "Nom", :with => "Test" click_button "Valider" @@ -132,7 +133,7 @@ describe "Referentials", :type => :feature do context "when user is from the same organisation" do xit "should" do - visit new_referential_path(from: referential.id, current_workbench_id: @user.organisation.workbenches.first.id) + visit new_workbench_referential_path(referential.workbench, from: referential.id, current_workbench_id: @user.organisation.workbenches.first.id) select "2018", :from => "referential_metadatas_attributes_0_periods_attributes_0_begin_1i" @@ -187,7 +188,8 @@ describe "Referentials", :type => :feature do end describe "destroy" do - let(:referential) { create(:referential, :organisation => @user.organisation) } + let(:workbench){ @user.organisation.workbenches.last } + let(:referential) { create(:referential, :organisation => @user.organisation, workbench: workbench) } it "should remove referential" do visit referential_path(referential) diff --git a/spec/features/workbenches/workbenches_permissions_spec.rb b/spec/features/workbenches/workbenches_permissions_spec.rb index d58293538..1c073a4c5 100644 --- a/spec/features/workbenches/workbenches_permissions_spec.rb +++ b/spec/features/workbenches/workbenches_permissions_spec.rb @@ -22,7 +22,7 @@ describe 'Workbenches', type: :feature do let( :permission ){ true } it 'shows the corresponding button' do - expected_href = new_referential_path(workbench_id: workbench) + expected_href = new_workbench_referential_path(workbench) expect( page ).to have_link('Créer', href: expected_href) end end diff --git a/spec/features/workbenches/workbenches_show_spec.rb b/spec/features/workbenches/workbenches_show_spec.rb index 7be813b94..405fdce82 100644 --- a/spec/features/workbenches/workbenches_show_spec.rb +++ b/spec/features/workbenches/workbenches_show_spec.rb @@ -232,7 +232,7 @@ RSpec.describe 'Workbenches', type: :feature do context 'user has the permission to create referentials' do it 'shows the link for a new referetnial' do - expect(page).to have_link(I18n.t('actions.add'), href: new_referential_path(workbench_id: workbench.id)) + expect(page).to have_link(I18n.t('actions.add'), href: new_workbench_referential_path(workbench)) end end @@ -240,7 +240,7 @@ RSpec.describe 'Workbenches', type: :feature do it 'does not show the clone link for referential' do @user.update_attribute(:permissions, []) visit referential_path(referential) - expect(page).not_to have_link(I18n.t('actions.add'), href: new_referential_path(workbench_id: workbench.id)) + expect(page).not_to have_link(I18n.t('actions.add'), href: new_workbench_referential_path(workbench)) end end end diff --git a/spec/fixtures/simple_importer/lines_mapping.csv b/spec/fixtures/simple_importer/lines_mapping.csv new file mode 100644 index 000000000..b26d0ab59 --- /dev/null +++ b/spec/fixtures/simple_importer/lines_mapping.csv @@ -0,0 +1,11 @@ +id;timetable_route_id;route_name;stop_sequence;stop_distance;station_code;station_name;border;Ligne Chouette;Transporteur
+3354;1136;Paris centre - Bercy > Lille > Londres;1;0;XPB;Paris City Center - Bercy;f;Paris <> Londres - OUIBUS;OUIBUS
+3355;1136;Paris centre - Bercy > Lille > Londres;2;232;XDB;Lille;f;Paris <> Londres - OUIBUS;OUIBUS
+3749;1136;Paris centre - Bercy > Lille > Londres;3;350;COF;Coquelles - France;t;Paris <> Londres - OUIBUS;OUIBUS
+4772;1136;Paris centre - Bercy > Lille > Londres;4;350;COU;Coquelles - UK;t;Paris <> Londres - OUIBUS;OUIBUS
+3357;1136;Paris centre - Bercy > Lille > Londres;5;527;ZEP;London;f;Paris <> Londres - OUIBUS;OUIBUS
+3358;1137;Londres > Lille > Paris centre - Bercy;1;0;ZEP;London;f;Paris <> Londres - OUIBUS;OUIBUS
+3559;1137;Londres > Lille > Paris centre - Bercy;2;177;COU;Coquelles - UK;t;Paris <> Londres - OUIBUS;OUIBUS
+3743;1137;Londres > Lille > Paris centre - Bercy;3;177;COF;Coquelles - France;t;Paris <> Londres - OUIBUS;OUIBUS
+3360;1137;Londres > Lille > Paris centre - Bercy;4;295;XDB;Lille;f;Paris <> Londres - OUIBUS;OUIBUS
+3361;1137;Londres > Lille > Paris centre - Bercy;5;527;XPB;Paris City Center - Bercy;f;Paris <> Londres - OUIBUS;OUIBUS
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..9b11aa02c --- /dev/null +++ b/spec/fixtures/simple_importer/stop_area_incomplete.csv @@ -0,0 +1,3 @@ +name;lat;long;type;street_name +Foo;45.00;12;ZDEP +;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/fixtures/simple_importer/stop_points_full.csv b/spec/fixtures/simple_importer/stop_points_full.csv new file mode 100644 index 000000000..9c5b3c1b7 --- /dev/null +++ b/spec/fixtures/simple_importer/stop_points_full.csv @@ -0,0 +1,11 @@ +"id";"timetable_route_id";"route_name";"stop_sequence";"stop_distance";"station_code";"station_name";"border"
+3354;1136;"Paris centre - Bercy > Lille > Londres";1;0;"XPB";"Paris City Center - Bercy";f
+3355;1136;"Paris centre - Bercy > Lille > Londres";2;232;"XDB";"Lille";f
+3749;1136;"Paris centre - Bercy > Lille > Londres";3;350;"COF";"Coquelles - France";t
+4772;1136;"Paris centre - Bercy > Lille > Londres";4;350;"COU";"Coquelles - UK";t
+3357;1136;"Paris centre - Bercy > Lille > Londres";5;527;"ZEP";"London";f
+3358;1137;"Londres > Lille > Paris centre - Bercy";1;0;"ZEP";"London";f
+3559;1137;"Londres > Lille > Paris centre - Bercy";2;177;"COU";"Coquelles - UK";t
+3743;1137;"Londres > Lille > Paris centre - Bercy";3;177;"COF";"Coquelles - France";t
+3360;1137;"Londres > Lille > Paris centre - Bercy";4;295;"XDB";"Lille";f
+3361;1137;"Londres > Lille > Paris centre - Bercy";5;527;"XPB";"Paris City Center - Bercy";f
diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb index 5bddbb16f..478875118 100644 --- a/spec/helpers/table_builder_helper_spec.rb +++ b/spec/helpers/table_builder_helper_spec.rb @@ -15,8 +15,9 @@ describe TableBuilderHelper, type: :helper do describe "#table_builder_2" do it "builds a table" do - referential = build_stubbed(:workbench_referential) + referential = create(:workbench_referential) workbench = referential.workbench + referential.organisation.workbenches << workbench user_context = UserContext.new( build_stubbed( @@ -30,7 +31,8 @@ describe TableBuilderHelper, type: :helper do ), referential: referential ) - allow(helper).to receive(:current_user).and_return(user_context) + allow(helper).to receive(:pundit_user).and_return(user_context) + allow(helper).to receive(:current_user).and_return(user_context.user) referentials = [referential] @@ -90,7 +92,7 @@ describe TableBuilderHelper, type: :helper do </ul> <ul class="other"> <li class=""><a href="/referentials/#{referential.id}/time_tables">Calendriers</a></li> - <li class=""><a href="/referentials/new?from=#{referential.id}">Dupliquer</a></li> + <li class=""><a href="/workbenches/#{workbench.id}/referentials/new?from=#{referential.id}">Dupliquer</a></li> <li class=""><a href="/referentials/#{referential.id}/select_compliance_control_set">Valider</a></li> <li class=""><a rel="nofollow" data-method="put" href="/referentials/#{referential.id}/archive">Conserver</a></li> </ul> @@ -193,7 +195,8 @@ describe TableBuilderHelper, type: :helper do ), referential: referential ) - allow(helper).to receive(:current_user).and_return(user_context) + allow(helper).to receive(:pundit_user).and_return(user_context) + allow(helper).to receive(:current_user).and_return(user_context.user) allow(helper).to receive(:current_referential) .and_return(referential) @@ -307,7 +310,8 @@ describe TableBuilderHelper, type: :helper do ), referential: referential ) - allow(helper).to receive(:current_user).and_return(user_context) + allow(helper).to receive(:pundit_user).and_return(user_context) + allow(helper).to receive(:current_user).and_return(user_context.user) allow(helper).to receive(:current_referential) .and_return(referential) @@ -398,8 +402,8 @@ describe TableBuilderHelper, type: :helper do end context "on a single row" do - let(:referential){ build_stubbed :referential } - let(:other_referential){ build_stubbed :referential } + let(:referential){ build_stubbed :workbench_referential } + let(:other_referential){ build_stubbed :workbench_referential } let(:user_context){ UserContext.new( build_stubbed( @@ -432,7 +436,9 @@ describe TableBuilderHelper, type: :helper do let(:items){ [item, other_item] } before(:each){ - allow(helper).to receive(:current_user).and_return(user_context) + allow(helper).to receive(:pundit_user).and_return(user_context) + allow(helper).to receive(:current_user).and_return(user_context.user) + allow(helper).to receive(:mutual_workbench).and_return(referential.workbench) } context "with all rows non-selectable" do diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb index 19a74a0e7..7c767e4d1 100644 --- a/spec/models/chouette/journey_pattern_spec.rb +++ b/spec/models/chouette/journey_pattern_spec.rb @@ -71,6 +71,30 @@ describe Chouette::JourneyPattern, :type => :model do end end + describe "set_distances" do + let(:journey_pattern) { create :journey_pattern } + let(:distances){ [] } + it "should raise an error" do + expect{journey_pattern.set_distances(distances)}.to raise_error + end + + context "with consistent data" do + let(:distances){ [0, 100, "200", 500, 1000] } + + it "should set costs" do + expect{journey_pattern.set_distances(distances)}.to_not raise_error + start, stop = journey_pattern.stop_points[0..1] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 100 + start, stop = journey_pattern.stop_points[1..2] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 100 + start, stop = journey_pattern.stop_points[2..3] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 300 + start, stop = journey_pattern.stop_points[3..4] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 500 + end + end + end + describe "state_update" do def journey_pattern_to_state jp jp.attributes.slice('name', 'published_name', 'registration_number').tap do |item| diff --git a/spec/models/chouette/vehicle_journey_at_stop_spec.rb b/spec/models/chouette/vehicle_journey_at_stop_spec.rb index a97559a0c..f79d19c88 100644 --- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb +++ b/spec/models/chouette/vehicle_journey_at_stop_spec.rb @@ -10,7 +10,7 @@ RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do context '#checksum_attributes' do it 'should return attributes' do - expected = [at_stop.departure_time.to_s(:time), at_stop.arrival_time.to_s(:time)] + expected = [at_stop.departure_time.utc.to_s(:time), at_stop.arrival_time.utc.to_s(:time)] expected << at_stop.departure_day_offset.to_s expected << at_stop.arrival_day_offset.to_s expect(at_stop.checksum_attributes).to include(*expected) diff --git a/spec/models/simple_importer_spec.rb b/spec/models/simple_importer_spec.rb new file mode 100644 index 000000000..60d7b7882 --- /dev/null +++ b/spec/models/simple_importer_spec.rb @@ -0,0 +1,394 @@ +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 + let(:importer){ importer = SimpleImporter.new(configuration_name: :test, filepath: filepath) } + let(:filepath){ fixtures_path 'simple_importer', filename } + let(:filename){ "stop_area.csv" } + let(:stop_area_referential){ create(:stop_area_referential, objectid_format: :stif_netex) } + + 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: stop_area_referential + config.add_value :kind, :commercial + end + end + + it "should import the given file" do + expect{importer.import verbose: false}.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 "when overriding configuration" do + before(:each){ + importer.configure do |config| + config.add_value :latitude, 88 + end + } + + it "should import the given file and not mess with the global configuration" do + expect{importer.import}.to change{Chouette::StopArea.count}.by 1 + expect(importer.status).to eq "success" + stop = Chouette::StopArea.last + expect(stop.latitude).to eq 88 + importer = SimpleImporter.new(configuration_name: :test, filepath: filepath) + expect{importer.import}.to change{Chouette::StopArea.count}.by 0 + expect(stop.reload.latitude).to eq 45 + end + end + + context "with an already existing record" do + let(:filename){ "stop_area.csv" } + before(:each){ + create :stop_area, name: "Nom du Stop" + } + it "should only update the record" do + 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 + + context "in another scope" do + before(:each) do + ref = create(:stop_area_referential) + importer.configure do |config| + config.context = { stop_area_referential: ref } + config.scope = ->{ context[:stop_area_referential].stop_areas } + end + end + + it "should create the record" do + expect{importer.import verbose: false}.to change{Chouette::StopArea.count}.by 1 + expect(importer.status).to eq "success" + end + end + end + + context "with a missing column" do + let(:filename){ "stop_area_missing_street_name.csv" } + it "should set an error message" do + expect{importer.import(verbose: false)}.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 an incomplete dataset" do + let(:filename){ "stop_area_incomplete.csv" } + it "should fail" do + expect{importer.import}.to_not raise_error + expect(importer.status).to eq "failed" + expect(importer.reload.journal.last["message"]).to eq({"name" => ["doit être rempli(e)"]}) + end + + it "should be transactional" do + expect{importer.import}.to_not change {Chouette::StopArea.count} + end + end + + context "with a wrong filepath" do + let(:filename){ "not_found.csv" } + it "should fail" do + 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 custom behaviour" do + let!(:present){ create :stop_area, name: "Nom du Stop", stop_area_referential: stop_area_referential } + let!(:missing){ create :stop_area, name: "Another", stop_area_referential: stop_area_referential } + before(:each){ + importer.configure do |config| + config.before do |importer| + stop_area_referential.stop_areas.each &:deactivate! + end + + config.before(:each_save) do |importer, stop_area| + stop_area.activate! + end + end + } + + it "should disable all missing areas" do + expect{importer.import}.to change{Chouette::StopArea.count}.by 0 + expect(present.reload.activated?).to be_truthy + expect(missing.reload.activated?).to be_falsy + end + + context "with an error" do + let(:filename){ "stop_area_incomplete.csv" } + it "should do nothing" do + expect{importer.import}.to_not change {Chouette::StopArea.count} + expect(present.reload.activated?).to be_truthy + expect(missing.reload.activated?).to be_truthy + end + end + end + + context "with a full file" do + let(:filename){ "stop_area_full.csv" } + let!(:missing){ create :stop_area, name: "Another", stop_area_referential: stop_area_referential } + + 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, stop_area_referential.id + config.add_value :long_lat_type, "WGS84" + config.add_value :kind, :commercial + config.before do |importer| + stop_area_referential.stop_areas.each &:deactivate! + end + + config.before(:each_save) do |importer, stop_area| + stop_area.activate + end + end + end + + it "should import the given file" do + expect{importer.import(verbose: false)}.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" + expect(first.activated?).to be_truthy + expect(last.activated?).to be_truthy + expect(missing.reload.activated?).to be_falsy + end + + context "with a relation in reverse order" do + let(:filename){ "stop_area_full_reverse.csv" } + + it "should import the given file" do + 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 + + context "with a specific importer" do + let(:filename){ "stop_points_full.csv" } + + before(:each) do + create :line, name: "Paris <> Londres - OUIBUS" + + SimpleImporter.define :test do |config| + config.model = Chouette::Route + config.separator = ";" + config.context = {stop_area_referential: stop_area_referential} + + config.before do |importer| + mapping = {} + path = Rails.root + "spec/fixtures/simple_importer/lines_mapping.csv" + CSV.foreach(path, importer.configuration.csv_options) do |row| + if row["Ligne Chouette"].present? + mapping[row["timetable_route_id"]] ||= Chouette::Line.find_by(name: importer.encode_string(row["Ligne Chouette"])) + end + end + importer.context[:mapping] = mapping + end + + config.custom_handler do |row| + line = nil + fail_with_error "MISSING LINE FOR ROUTE: #{encode_string row["route_name"]}" do + line = context[:mapping][row["timetable_route_id"]] + raise unless line + end + @current_record = Chouette::Route.find_or_initialize_by number: row["timetable_route_id"] + @current_record.name = encode_string row["route_name"] + @current_record.published_name = encode_string row["route_name"] + + @current_record.line = line + if @prev_route != @current_record + if @prev_route && @prev_route.valid? + journey_pattern = @prev_route.full_journey_pattern + fail_with_error "WRONG DISTANCES FOR ROUTE #{@prev_route.name} (#{@prev_route.number}): #{@distances.count} distances for #{@prev_route.stop_points.count} stops" do + journey_pattern.stop_points = @prev_route.stop_points + journey_pattern.set_distances @distances + end + fail_with_error ->(){ journey_pattern.errors.messages } do + journey_pattern.save! + end + end + @distances = [] + end + @distances.push row["stop_distance"] + position = row["stop_sequence"].to_i - 1 + + stop_area = context[:stop_area_referential].stop_areas.where(registration_number: row["station_code"]).last + unless stop_area + stop_area = Chouette::StopArea.new registration_number: row["station_code"] + stop_area.name = row["station_name"] + stop_area.kind = row["border"] == "f" ? :commercial : :non_commercial + stop_area.area_type = row["border"] == "f" ? :zdep : :border + stop_area.stop_area_referential = context[:stop_area_referential] + fail_with_error ->{p stop_area; "UNABLE TO CREATE STOP_AREA: #{stop_area.errors.messages}" }, abort_row: true do + stop_area.save! + end + end + stop_point = @current_record.stop_points.find_by(stop_area_id: stop_area.id) + if stop_point + stop_point.set_list_position position + else + stop_point = @current_record.stop_points.build(stop_area_id: stop_area.id, position: position) + stop_point.for_boarding = :normal + stop_point.for_alighting = :normal + end + + @prev_route = @current_record + end + + config.after(:each_save) do |importer, route| + opposite_route_name = route.name.split(" > ").reverse.join(' > ') + opposite_route = Chouette::Route.where(name: opposite_route_name).where('id < ?', route.id).last + if opposite_route && opposite_route.line == route.line + route.update_attribute :wayback, :inbound + opposite_route.update_attribute :wayback, :outbound + route.update_attribute :opposite_route_id, opposite_route.id + opposite_route.update_attribute :opposite_route_id, route.id + end + end + + config.after do |importer| + prev_route = importer.instance_variable_get "@prev_route" + if prev_route && prev_route.valid? + journey_pattern = prev_route.full_journey_pattern + importer.fail_with_error "WRONG DISTANCES FOR ROUTE #{prev_route.name}: #{importer.instance_variable_get("@distances").count} distances for #{prev_route.stop_points.count} stops" do + journey_pattern.set_distances importer.instance_variable_get("@distances") + journey_pattern.stop_points = prev_route.stop_points + end + importer.fail_with_error ->(){ journey_pattern.errors.messages } do + journey_pattern.save! + end + end + end + end + end + + it "should import the given file" do + routes_count = Chouette::Route.count + journey_pattern_count = Chouette::JourneyPattern.count + stop_areas_count = Chouette::StopArea.count + + expect{importer.import(verbose: false)}.to change{Chouette::StopPoint.count}.by 10 + expect(importer.status).to eq "success" + expect(Chouette::Route.count).to eq routes_count + 2 + expect(Chouette::JourneyPattern.count).to eq journey_pattern_count + 2 + expect(Chouette::StopArea.count).to eq stop_areas_count + 5 + route = Chouette::Route.find_by number: 1136 + expect(route.stop_areas.count).to eq 5 + expect(route.opposite_route).to eq Chouette::Route.find_by(number: 1137) + journey_pattern = route.full_journey_pattern + expect(journey_pattern.stop_areas.count).to eq 5 + start, stop = journey_pattern.stop_points[0..1] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 232 + start, stop = journey_pattern.stop_points[1..2] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 118 + start, stop = journey_pattern.stop_points[2..3] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 0 + start, stop = journey_pattern.stop_points[3..4] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 177 + + route = Chouette::Route.find_by number: 1137 + expect(route.opposite_route).to eq Chouette::Route.find_by(number: 1136) + expect(route.stop_areas.count).to eq 5 + journey_pattern = route.full_journey_pattern + expect(journey_pattern.stop_areas.count).to eq 5 + start, stop = journey_pattern.stop_points[0..1] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 177 + start, stop = journey_pattern.stop_points[1..2] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 0 + start, stop = journey_pattern.stop_points[2..3] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 118 + start, stop = journey_pattern.stop_points[3..4] + expect(journey_pattern.costs_between(start, stop)[:distance]).to eq 232 + + stop_area = Chouette::StopArea.where(registration_number: "XPB").last + expect(stop_area.kind).to eq :commercial + expect(stop_area.area_type).to eq :zdep + + stop_area = Chouette::StopArea.where(registration_number: "XDB").last + expect(stop_area.kind).to eq :commercial + expect(stop_area.area_type).to eq :zdep + + stop_area = Chouette::StopArea.where(registration_number: "COF").last + expect(stop_area.kind).to eq :non_commercial + expect(stop_area.area_type).to eq :border + + stop_area = Chouette::StopArea.where(registration_number: "COU").last + expect(stop_area.kind).to eq :non_commercial + expect(stop_area.area_type).to eq :border + + stop_area = Chouette::StopArea.where(registration_number: "ZEP").last + expect(stop_area.kind).to eq :commercial + expect(stop_area.area_type).to eq :zdep + end + end + end +end diff --git a/spec/support/decorator_helpers.rb b/spec/support/decorator_helpers.rb index b2c41e842..544604f61 100644 --- a/spec/support/decorator_helpers.rb +++ b/spec/support/decorator_helpers.rb @@ -8,6 +8,7 @@ module Support let( :features ){ [] } let( :filtered_action_links){} before do + allow(subject.h).to receive(:duplicate_workbench_referential_path).and_return new_workbench_referential_path(referential.workbench, from: referential.id) allow_any_instance_of(Draper::HelperProxy).to receive(:policy).and_return policy allow_any_instance_of(AF83::Decorator::Link).to receive(:check_feature){|f| features.include?(f) diff --git a/spec/views/referentials/show.html.erb_spec.rb b/spec/views/referentials/show.html.erb_spec.rb index ea3bc1fe1..a7f37d180 100644 --- a/spec/views/referentials/show.html.erb_spec.rb +++ b/spec/views/referentials/show.html.erb_spec.rb @@ -1,73 +1,48 @@ require 'spec_helper' describe "referentials/show", type: :view do + let!(:referential) do - referential = create(:referential, organisation: organisation) + referential = create(:workbench_referential) assign :referential, referential.decorate(context: { current_organisation: referential.organisation }) end + let(:organisation){ referential.try(:organisation) } let(:permissions){ [] } let(:current_organisation) { organisation } - let(:current_offer_workbench) { create :workbench, organisation: organisation} - let(:current_workgroup) { current_offer_workbench.workgroup } let(:readonly){ false } before :each do assign :reflines, [] - allow(view).to receive(:current_offer_workbench).and_return(current_offer_workbench) allow(view).to receive(:current_organisation).and_return(current_organisation) - allow(view).to receive(:current_workgroup).and_return(current_workgroup) allow(view).to receive(:current_user).and_return(current_user) - allow(view).to receive(:resource).and_return(referential) allow(view).to receive(:has_feature?).and_return(true) allow(view).to receive(:user_signed_in?).and_return true + allow(view).to receive(:mutual_workbench).and_return referential.workbench controller.request.path_parameters[:id] = referential.id allow(view).to receive(:params).and_return({action: :show}) allow(referential).to receive(:referential_read_only?){ readonly } + render template: "referentials/show", layout: "layouts/application" end - describe "action links" do - set_invariant "referential.object.full_name", "referential_full_name" - set_invariant "referential.object.updated_at", "01/01/2000 00:00".to_time - set_invariant "referential.object.id", "99" - - before(:each){ - render template: "referentials/show", layout: "layouts/application" - } - context "with a readonly referential" do - let(:readonly){ true } - it { should match_actions_links_snapshot "referentials/show_readonly" } - - %w(create destroy update).each do |p| - with_permission "referentials.#{p}" do - it { should match_actions_links_snapshot "referentials/show_readonly_#{p}" } - end - end - end - - context "with a non-readonly referential" do - it { should match_actions_links_snapshot "referentials/show" } + it "should not present edit button" do + expect(rendered).to_not have_selector("a[href=\"#{view.edit_referential_path(referential)}\"]") + end - %w(create destroy update).each do |p| - with_permission "referentials.#{p}" do - it { should match_actions_links_snapshot "referentials/show_#{p}" } - end - end + with_permission "referentials.update" do + it "should present edit button" do + expect(rendered).to have_selector("a[href=\"#{view.edit_referential_path(referential)}\"]") end - %w(purchase_windows referential_vehicle_journeys).each do |f| - with_feature f do - it { should match_actions_links_snapshot "referentials/show_#{f}" } - - %w(create update destroy).each do |p| - with_permission "referentials.#{p}" do - it { should match_actions_links_snapshot "referentials/show_#{f}_#{p}" } - end - end + context "with a readonly referential" do + let(:readonly){ true } + it "should not present edit button" do + expect(rendered).to_not have_selector("a[href=\"#{view.edit_referential_path(referential)}\"]") end end end + end |
