spec/models/blacklight/solr/search_builder_spec.rb in blacklight-5.19.2 vs spec/models/blacklight/solr/search_builder_spec.rb in blacklight-6.0.0.pre1

- old
+ new

@@ -1,34 +1,41 @@ require 'spec_helper' -describe Blacklight::Solr::SearchBuilderBehavior do +describe Blacklight::Solr::SearchBuilder do let(:single_facet) { { format: 'Book' } } let(:multi_facets) { { format: 'Book', language_facet: 'Tibetan' } } let(:mult_word_query) { 'tibetan history' } let(:subject_search_params) { { commit: "search", search_field: "subject", action: "index", controller: "catalog", rows: "10", q: "wome" } } let(:blacklight_config) { CatalogController.blacklight_config.deep_copy } - let(:method_chain) { CatalogController.search_params_logic } let(:user_params) { Hash.new } let(:context) { CatalogController.new } before { allow(context).to receive(:blacklight_config).and_return(blacklight_config) } - let(:search_builder_class) do - Class.new(Blacklight::SearchBuilder) do - include Blacklight::Solr::SearchBuilderBehavior - end - end - let(:search_builder) { search_builder_class.new(method_chain, context) } + let(:search_builder) { described_class.new(context) } subject { search_builder.with(user_params) } context "with default processor chain" do - subject { search_builder_class.new true, context } - it "should use the class-level default_processor_chain" do - expect(subject.processor_chain).to eq search_builder_class.default_processor_chain + context "with two arguments" do + subject do + Deprecation.silence Blacklight::SearchBuilder do + described_class.new true, context + end + end + it "uses the class-level default_processor_chain" do + expect(subject.processor_chain).to eq described_class.default_processor_chain + end end + + context "with one arguments" do + subject { described_class.new context } + it "uses the class-level default_processor_chain" do + expect(subject.processor_chain).to eq described_class.default_processor_chain + end + end end context "with a complex parameter environment" do subject { search_builder.with(user_params).processed_parameters } @@ -77,22 +84,19 @@ end # SPECS for actual search parameter generation describe "#processed_parameters" do subject do - Deprecation.silence(Blacklight::SearchBuilder) do - search_builder.with(user_params).processed_parameters - end + search_builder.with(user_params).processed_parameters end context "when search_params_logic is customized" do + let(:search_builder) { described_class.new(method_chain, context) } let(:method_chain) { [:add_foo_to_solr_params] } it "allows customization of search_params_logic" do - # Normally you'd include a new module into (eg) your CatalogController - # but a sub-class defininig it directly is simpler for test. - allow(context).to receive(:add_foo_to_solr_params) do |solr_params, user_params| + allow(search_builder).to receive(:add_foo_to_solr_params) do |solr_params, user_params| solr_params[:wt] = "TESTING" end expect(subject[:wt]).to eq "TESTING" end @@ -100,30 +104,10 @@ it "should generate a facet limit" do expect(subject[:"f.subject_topic_facet.facet.limit"]).to eq 21 end - context 'with a negative facet limit' do - before do - blacklight_config.facet_fields['subject_topic_facet'].limit = -1 - end - - it 'is negative' do - expect(subject[:"f.subject_topic_facet.facet.limit"]).to eq -1 - end - end - - context 'with a facet limit set to 0' do - before do - blacklight_config.facet_fields['subject_topic_facet'].limit = 0 - end - - it 'is negative' do - expect(subject[:"f.subject_topic_facet.facet.limit"]).to eq 0 - end - end - it "should handle no facet_limits in config" do blacklight_config.facet_fields = {} expect(subject).not_to have_key(:"f.subject_topic_facet.facet.limit") end @@ -153,12 +137,12 @@ it 'should have default facet fields' do # remove local params from the facet.field expect(subject[:"facet.field"].map { |x| x.gsub(/\{![^}]+\}/, '') }).to match_array ["format", "subject_topic_facet", "pub_date", "language_facet", "lc_1letter_facet", "subject_geo_facet", "subject_era_facet"] end - it "should have default qt" do - expect(subject[:qt]).to eq "search" + it "should not have a default qt" do + expect(subject[:qt]).to be_nil end it "should have no fq" do expect(subject[:phrase_filters]).to be_blank expect(subject[:fq]).to be_blank end @@ -187,11 +171,11 @@ expect(subject[:q]).to be_blank expect(subject["spellcheck.q"]).to be_blank single_facet.each_value do |value| - expect(subject[:fq]).to include("{!raw f=#{single_facet.keys[0]}}#{value}") + expect(subject[:fq]).to include("{!term f=#{single_facet.keys[0]}}#{value}") end end end describe "for an empty facet limit param" do @@ -206,11 +190,11 @@ it 'should have fq set properly' do multi_facets.each_pair do |facet_field, value_list| value_list ||= [] value_list = [value_list] unless value_list.respond_to? :each value_list.each do |value| - expect(subject[:fq]).to include("{!raw f=#{facet_field}}#{value}" ) + expect(subject[:fq]).to include("{!term f=#{facet_field}}#{value}" ) end end end end @@ -220,11 +204,11 @@ it 'should have fq and q set properly' do multi_facets.each_pair do |facet_field, value_list| value_list ||= [] value_list = [value_list] unless value_list.respond_to? :each value_list.each do |value| - expect(subject[:fq]).to include("{!raw f=#{facet_field}}#{value}" ) + expect(subject[:fq]).to include("{!term f=#{facet_field}}#{value}" ) end end expect(subject[:q]).to eq mult_word_query end end @@ -375,72 +359,72 @@ describe "#facet_value_to_fq_string" do it "should use the configured field name" do blacklight_config.add_facet_field :facet_key, field: "facet_name" - expect(subject.send(:facet_value_to_fq_string, "facet_key", "my value")).to eq "{!raw f=facet_name}my value" + expect(subject.send(:facet_value_to_fq_string, "facet_key", "my value")).to eq "{!term f=facet_name}my value" end it "should use the raw handler for strings" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name}my value" + expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!term f=facet_name}my value" end it "should pass booleans through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "facet_name:true" + expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq '{!term f=facet_name}true' end it "should pass boolean-like strings through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", "true")).to eq "facet_name:true" + expect(subject.send(:facet_value_to_fq_string, "facet_name", "true")).to eq '{!term f=facet_name}true' end it "should pass integers through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", 1)).to eq "facet_name:1" + expect(subject.send(:facet_value_to_fq_string, "facet_name", 1)).to eq '{!term f=facet_name}1' end it "should pass integer-like strings through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", "1")).to eq "facet_name:1" + expect(subject.send(:facet_value_to_fq_string, "facet_name", "1")).to eq '{!term f=facet_name}1' end it "should pass floats through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", 1.11)).to eq "facet_name:1.11" + expect(subject.send(:facet_value_to_fq_string, "facet_name", 1.11)).to eq '{!term f=facet_name}1.11' end it "should pass floats through" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", "1.11")).to eq "facet_name:1.11" + expect(subject.send(:facet_value_to_fq_string, "facet_name", "1.11")).to eq '{!term f=facet_name}1.11' end it "should escape negative integers" do - expect(subject.send(:facet_value_to_fq_string, "facet_name", -1)).to eq "facet_name:\\-1" + expect(subject.send(:facet_value_to_fq_string, "facet_name", -1)).to eq '{!term f=facet_name}-1' end it "should pass date-type fields through" do allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil, :field => 'facet_name')) - expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq "facet_name:2012\\-01\\-01" + expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq '{!term f=facet_name}2012-01-01' end it "should escape datetime-type fields" do allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil, :field => 'facet_name')) - expect(subject.send(:facet_value_to_fq_string, "facet_name", "2003-04-09T00:00:00Z")).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z" + expect(subject.send(:facet_value_to_fq_string, "facet_name", "2003-04-09T00:00:00Z")).to eq '{!term f=facet_name}2003-04-09T00:00:00Z' end - + it "should format Date objects correctly" do allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => nil, :query => nil, :tag => nil, :field => 'facet_name')) d = DateTime.parse("2003-04-09T00:00:00") - expect(subject.send(:facet_value_to_fq_string, "facet_name", d)).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z" + expect(subject.send(:facet_value_to_fq_string, "facet_name", d)).to eq '{!term f=facet_name}2003-04-09T00:00:00Z' end it "should handle range requests" do expect(subject.send(:facet_value_to_fq_string, "facet_name", 1..5)).to eq "facet_name:[1 TO 5]" end it "should add tag local parameters" do allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:query => nil, :tag => 'asdf', :date => nil, :field => 'facet_name')) - expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "{!tag=asdf}facet_name:true" - expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name tag=asdf}my value" + expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "{!term f=facet_name tag=asdf}true" + expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!term f=facet_name tag=asdf}my value" end end describe "#add_facet_fq_to_solr" do it "converts a String fq into an Array" do @@ -581,10 +565,11 @@ describe "#add_facet_paging_to_solr" do let(:facet_field) { 'format' } let(:sort_key) { Blacklight::Solr::FacetPaginator.request_keys[:sort] } let(:page_key) { Blacklight::Solr::FacetPaginator.request_keys[:page] } + let(:prefix_key) { Blacklight::Solr::FacetPaginator.request_keys[:prefix] } let(:blacklight_config) do Blacklight::Configuration.new do |config| config.add_facet_fields_to_solr_request! config.add_facet_field 'format' @@ -637,9 +622,16 @@ context 'when sort is provided' do let(:user_params) { { sort_key => 'index' } } it 'uses sort provided in the parameters' do expect(solr_parameters[:"f.#{facet_field}.facet.sort"]).to eq 'index' + end + end + + context 'when a prefix is provided' do + let(:user_params) { { prefix_key => 'A' } } + it 'includes the prefix in the query' do + expect(solr_parameters[:"f.#{facet_field}.facet.prefix"]).to eq 'A' end end end describe "#with_tag_ex" do