diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index ab2bf3efc..4f1f82efe 100644 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -788,6 +788,8 @@ def basic_response end def search_service_context + return {} unless Flipflop.multi_algorithm? + return {} unless params[:search_algorithm] == "alternate" { search_builder_class: AlternateSearchBuilder } end end diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb index 0f0915454..aa7bcbf28 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -117,7 +117,15 @@ end end end + describe "switching search algorithms" do + let(:repository) { CatalogController.blacklight_config.repository } + + before do + allow(repository).to receive(:search).and_call_original + allow_any_instance_of(Blacklight::Configuration).to receive(:repository).and_return(repository) + end + context "when the multi-algorithm feature is on" do before do allow(Flipflop).to receive(:multi_algorithm?).and_return(true) @@ -125,12 +133,6 @@ context "when the search_algorithm parameter is not present" do it "uses the default search builder" do - # We want to make sure that the repository (in - # controller.blacklight_config.repository) receives search with an - # instance of SearchBuilder. - repository = CatalogController.blacklight_config.repository - allow(repository).to receive(:search).and_call_original - allow_any_instance_of(Blacklight::Configuration).to receive(:repository).and_return(repository) get :index, params: { q: "coffee" } expect(repository).to have_received(:search).with(instance_of(SearchBuilder)) @@ -139,13 +141,9 @@ context "when the search_algorithm parameter is set to 'alternate'" do it "uses the alternate search builder" do - allow(SearchBuilder).to receive(:new).and_call_original - allow(AlternateSearchBuilder).to receive(:new).and_call_original - get :index, params: { q: "coffee", search_algorithm: "alternate" } - expect(AlternateSearchBuilder).to have_received(:new) - expect(SearchBuilder).not_to have_received(:new) + expect(repository).to have_received(:search).with(instance_of(AlternateSearchBuilder)) end end end @@ -157,25 +155,17 @@ context "when the search_algorithm parameter is not present" do it "uses the default search builder" do - allow(SearchBuilder).to receive(:new).and_call_original - allow(AlternateSearchBuilder).to receive(:new).and_call_original - get :index, params: { q: "coffee" } - expect(SearchBuilder).to have_received(:new) - expect(AlternateSearchBuilder).not_to have_received(:new) + expect(repository).to have_received(:search).with(instance_of(SearchBuilder)) end end context "when the search_algorithm parameter is set to alternate" do it "uses the alternate search builder" do - allow(SearchBuilder).to receive(:new).and_call_original - allow(AlternateSearchBuilder).to receive(:new).and_call_original - - get :index, params: { q: "coffee" } + get :index, params: { q: "coffee", search_algorithm: "alternate" } - expect(SearchBuilder).to have_received(:new) - expect(AlternateSearchBuilder).not_to have_received(:new) + expect(repository).to have_received(:search).with(instance_of(SearchBuilder)) end end end