From 88396a92d9bd41bdaa6249964ad876e0c85f74fd Mon Sep 17 00:00:00 2001 From: Anna Headley <845363+hackartisan@users.noreply.github.com> Date: Tue, 7 Nov 2023 11:49:52 -0500 Subject: [PATCH] Finish test setup and sent desired search_builder_class Co-authored-by: Carolyn Cole Co-authored-by: James R. Griffin III Co-authored-by: Eliot Jordan --- app/controllers/catalog_controller.rb | 2 ++ spec/controllers/catalog_controller_spec.rb | 34 ++++++++------------- 2 files changed, 14 insertions(+), 22 deletions(-) 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