require 'spec_helper' describe "Base-Searcher-Modul" do subject { Class.new { include MultiSolr::BaseSearcher } } context "core_name" do it "should the defined core-name if set" do subject.set_core_name 'core-set' subject.core_name.should == 'core-set' end it "should use class-name if ends with Searcher" do sample_searcher = Class.new Object.const_set 'SampleSearcher', sample_searcher sample_searcher.send :include, MultiSolr::BaseSearcher sample_searcher.core_name.should == 'core-sample' end it "should use local (instance) core-name if set" do searcher = subject.new searcher.core_name = 'test-core' searcher.core_name.should == 'test-core' searcher.class.core_name == 'core-default' end end context "cached_list_possible_values" do it "should get cached value if exist" do fieldname = :test; context = :test_context1 value = "test-value" MultiSolr.cache.write("solr-#{subject.core_name}-#{context}-#{fieldname}-values", value) subject.cached_list_possible_values(fieldname, context).should == value end it "should call 'list_possible_values' if no cached value exist" do fieldname = :test; context = :test_context2 value = "test-value" subject.should_receive(:list_possible_values).with(fieldname, context, nil) do value end subject.cached_list_possible_values(fieldname, context).should == value end end context "list_possible_values" do it "should for field p1 an Array with right sortert Strings" do facet_searcher_mock = double :searcher result = double :solr_result result.stub(:facet_counts).and_return('p1' => [['schuhe', 11], ['hosen', 2]]) facet_searcher_mock.stub(:execute).and_return(result) subject.should_receive(:new).and_return(facet_searcher_mock) subject.list_possible_values(:p1).should == ['hosen', 'schuhe'] end it "should for field p2 an Array with sortet Integers" do facet_searcher_mock = double :searcher result = double :solr_result result.stub(:facet_counts).and_return('p2' => [['2233', 2], ['11', 3]]) facet_searcher_mock.stub(:execute).and_return(result) subject.should_receive(:new).and_return(facet_searcher_mock) subject.list_possible_values(:p2).should == [11, 2233] # Int-Strings sollten nach int konvertiert sein end end context "solr_connection" do it "should raise exception if no solr_url set" do expect{ subject.solr_connection }.to raise_error end it "should connect with solr-url + core-name" do subject.set_solr_url 'http://localhost/solr' RSolr.should_receive(:connect) do |options| options # zum Test die options selbst wieder zurück liefern end subject.solr_connection[:url].should == 'http://localhost/solr/core-default' end it "should use local (instance) core-name if set" do subject.set_solr_url 'http://localhost/solr' RSolr.should_receive(:connect) do |options| options # zum Test die options selbst wieder zurück liefern end searcher = subject.new searcher.core_name = 'test_11' searcher.solr_connection[:url].should == 'http://localhost/solr/test_11' end end context "execute" do before do @request = MultiSolr::SearchRequest.new @searcher = subject.new end it "should parse result if result-format is 'ruby'" do rsolr_mock = double('rsolr') rsolr_mock.should_receive(:get).and_return({}) @searcher.should_receive(:solr_connection).and_return(rsolr_mock) result = @searcher.execute @request result.should be_a(MultiSolr::SearchResult) end it "should not parse result if result-format is not 'ruby'" do @searcher.result_format = 'json' result_mock = "RAW" rsolr_mock = double('rsolr') rsolr_mock.should_receive(:get).and_return(result_mock) @searcher.should_receive(:solr_connection).and_return(rsolr_mock) result = @searcher.execute @request result.should == result_mock end end context "build_solr_params" do before do @request = MultiSolr::SearchRequest.new @searcher = subject.new end it "should add score force-query if context given" do solr_params = @searcher.build_solr_params @request, :whs_id => 8 solr_params[:fq].should == "whs_id:8" end it "should have query for all if no query specified" do solr_params = @searcher.build_solr_params @request solr_params[:q].should == "*:*" end it "should use query-params builded by search request" do @request.should_receive(:build_query).and_return('lkz:1234') solr_params = @searcher.build_solr_params @request solr_params[:q].should == "lkz:1234" end it "should add score field to fieldlist if '_val_' in query (function query)" do @request.should_receive(:build_query).and_return('lkz:1234 _val_:"product(menge,volumen)') solr_params = @searcher.build_solr_params @request @searcher.fieldlist.should == "*,score" solr_params[:fl].should == "*,score" end it "should use sort if specified" do @request.sorts = ['s1', 's2 desc'] solr_params = @searcher.build_solr_params @request solr_params[:sort].should == 's1 asc,s2 desc' end context "should generate facet-params if simple facet fields specified" do before do @request.facets = ['f1', 'f2'] @solr_params = @searcher.build_solr_params @request end it{ @solr_params[:facet].should be_true } it{ @solr_params['facet.field'].should == [:f1, :f2] } end context "should generate facet-params if facet fields specified" do before do @request.facets = ['f1', 'f2', 'f3'] @request.facet_params = {:f2 => {:limit => 1000, :sort => :index}, :f3 => {:range => true, :start => 'NOW/DAY-2DAY'}} @solr_params = @searcher.build_solr_params @request end it{ @solr_params[:facet].should be_true } it{ @solr_params['facet.field'].should == [:f1, :f2] } it{ @solr_params['f.f2.facet.limit'].should == 1000 } it{ @solr_params['f.f2.facet.sort'].to_s.should == 'index' } it{ @solr_params['facet.range'].should == [:f3] } it{ @solr_params['f.f3.facet.range.start'].should == 'NOW/DAY-2DAY' } it{ @solr_params['f.f3.facet.range.end'].should == 'NOW/DAY' } it{ @solr_params['f.f3.facet.range.gap'].should == '+1DAY' } it{ @solr_params['json.nl'].should == 'arrarr' } end context "should generate stats-facets if facet-params have stats-field specified" do before do @request.facets = ['sf1', 'sf2'] @request.stats_fields = [:menge] @request.facet_params = {:sf1 => {:stats_field => 'menge'}, :sf2 => {:stats_field => ['volumen', 'gewicht', 'menge']}} @solr_params = @searcher.build_solr_params @request # puts @solr_params.inspect end it{ @solr_params[:facet].should be_nil } it{ @solr_params[:stats].should be_true } it{ @solr_params['stats.field'].should == ['menge', 'volumen', 'gewicht'] } it{ @solr_params['f.menge.stats.facet'].should == [:sf1, :sf2] } it{ @solr_params['f.volumen.stats.facet'].should == [:sf2] } it{ @solr_params['f.gewicht.stats.facet'].should == [:sf2] } end it "should set result-format to in searcher specified format" do @searcher.result_format = 'json' solr_params = @searcher.build_solr_params @request solr_params[:wt].should == 'json' end end end