From 84683c111e16e19c94a71f36f1d66c0745ad33d8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 14 May 2018 12:41:27 +0200 Subject: CleanUp: Add `#destroy_routes_outside_referential` This builds on `#destroy_vehicle_journeys_outside_referential` (which will soon be removed in favour of this new method). It destroys orphaned routes in a referential. Refs #6854 --- spec/models/clean_up_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'spec') diff --git a/spec/models/clean_up_spec.rb b/spec/models/clean_up_spec.rb index afd2d8721..cf15ab934 100644 --- a/spec/models/clean_up_spec.rb +++ b/spec/models/clean_up_spec.rb @@ -264,4 +264,24 @@ RSpec.describe CleanUp, :type => :model do } end end + + describe "#destroy_routes_outside_referential" do + let(:line_referential) { create(:line_referential) } + let(:line) { create(:line, line_referential: line_referential) } + let(:metadata) { create(:referential_metadata, lines: [line]) } + let(:referential) { create(:workbench_referential, metadatas: [metadata]) } + let(:cleaner) { create(:clean_up, referential: referential) } + + it "destroys routes not in the the referential" do + route = create(:route) + + cleaner.destroy_routes_outside_referential + + expect(Chouette::Route.exists?(route.id)).to be false + + line.routes.each do |route| + expect(route).not_to be_destroyed + end + end + end end -- cgit v1.2.3 From c048a5a7e5295b7a84d87ed4e850affe598d5588 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 14 May 2018 12:58:56 +0200 Subject: CleanUp#destroy_routes_outside_referential: Test cascade Ensure that `JourneyPattern`s and `VehicleJourney`s associated with orphaned routes get deleted in cascade. Refs #6854 --- spec/models/clean_up_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'spec') diff --git a/spec/models/clean_up_spec.rb b/spec/models/clean_up_spec.rb index cf15ab934..be169abbb 100644 --- a/spec/models/clean_up_spec.rb +++ b/spec/models/clean_up_spec.rb @@ -283,5 +283,17 @@ RSpec.describe CleanUp, :type => :model do expect(route).not_to be_destroyed end end + + it "cascades destruction of vehicle journeys and journey patterns" do + vehicle_journey = create(:vehicle_journey) + + cleaner.destroy_routes_outside_referential + + expect(Chouette::Route.exists?(vehicle_journey.route.id)).to be false + expect( + Chouette::JourneyPattern.exists?(vehicle_journey.journey_pattern) + ).to be false + expect(Chouette::VehicleJourney.exists?(vehicle_journey)).to be false + end end end -- cgit v1.2.3 From 4ca91f7ccbb71013ffe869be8d7f5cf344d9917a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 14 May 2018 18:40:33 +0200 Subject: CleanUp spec: Pass ids to `#exists?` call Looks like I temporarily forgot that this method takes an ID. Refs #6854 --- spec/models/clean_up_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/models/clean_up_spec.rb b/spec/models/clean_up_spec.rb index be169abbb..8a96437fb 100644 --- a/spec/models/clean_up_spec.rb +++ b/spec/models/clean_up_spec.rb @@ -291,9 +291,9 @@ RSpec.describe CleanUp, :type => :model do expect(Chouette::Route.exists?(vehicle_journey.route.id)).to be false expect( - Chouette::JourneyPattern.exists?(vehicle_journey.journey_pattern) + Chouette::JourneyPattern.exists?(vehicle_journey.journey_pattern.id) ).to be false - expect(Chouette::VehicleJourney.exists?(vehicle_journey)).to be false + expect(Chouette::VehicleJourney.exists?(vehicle_journey.id)).to be false end end end -- cgit v1.2.3 From 72fd54c38b958d0be9d608a676e8fef84691d088 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 14 May 2018 18:42:59 +0200 Subject: CleanUp: Add `#run_methods` This method is coupled with a new virtual attribute that can be used in the initializer like: CleanUp.new(methods: [:destroy_routes]) The method will run all methods specified in the `:methods` list. The plan is to replace the calls to `destroy_routes` etc. with a call to this method. The result will be a more configurable clean-up process, allowing users to selectively choose what to clean up by declaring what methods in the `CleanUp` model to call. Refs #6854 --- spec/models/clean_up_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'spec') diff --git a/spec/models/clean_up_spec.rb b/spec/models/clean_up_spec.rb index 8a96437fb..f0c3a3233 100644 --- a/spec/models/clean_up_spec.rb +++ b/spec/models/clean_up_spec.rb @@ -296,4 +296,24 @@ RSpec.describe CleanUp, :type => :model do expect(Chouette::VehicleJourney.exists?(vehicle_journey.id)).to be false end end + + describe "#run_methods" do + let(:cleaner) { create(:clean_up) } + + it "calls methods in the :methods attribute" do + cleaner = create( + :clean_up, + methods: [:destroy_routes_outside_referential] + ) + + expect(cleaner).to receive(:destroy_routes_outside_referential) + cleaner.run_methods + end + + it "doesn't do anything if :methods is nil" do + cleaner = create(:clean_up) + + expect { cleaner.run_methods }.not_to raise_error + end + end end -- cgit v1.2.3 From ceec2e53425f8a1c726f4903d03e268386cb598e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 15 May 2018 15:24:29 +0200 Subject: CleanUp: Add `#destroy_empty` Gives us a single call site to trigger the destruction of vehicle journeys, journey patterns, and routes without content. This was previously done directly in the `#clean` method, but since it's not needed during referential duplication (only during merges), we don't want to enable it by default. Thus now to activate the same old functionality, you would create a `CleanUp` like this: CleanUp.new(methods: [:destroy_empty]) Refs #6854 --- spec/models/clean_up_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'spec') diff --git a/spec/models/clean_up_spec.rb b/spec/models/clean_up_spec.rb index f0c3a3233..f39ca2f2b 100644 --- a/spec/models/clean_up_spec.rb +++ b/spec/models/clean_up_spec.rb @@ -297,6 +297,18 @@ RSpec.describe CleanUp, :type => :model do end end + describe "#destroy_empty" do + it "calls the appropriate destroy methods" do + cleaner = create(:clean_up) + + expect(cleaner).to receive(:destroy_vehicle_journeys) + expect(cleaner).to receive(:destroy_journey_patterns) + expect(cleaner).to receive(:destroy_routes) + + cleaner.destroy_empty + end + end + describe "#run_methods" do let(:cleaner) { create(:clean_up) } -- cgit v1.2.3