require 'spec_helper' describe MultiSolr::SingleCoreHandler do subject{ MultiSolr::SingleCoreHandler.new SolrTestdataProvider::SOLR_TEST_INSTANCE_URL, "core-01" } 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) 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 sorted Strings" do result = double :solr_result result.stub(:facet_counts).and_return('p1' => [['schuhe', 11], ['hosen', 2]]) subject.stub(:search).and_return(result) subject.list_possible_values(:p1).should == ['hosen', 'schuhe'] end it "should for field p2 an Array with sortet Integers" do result = double :solr_result result.stub(:facet_counts).and_return('p2' => [['2233', 2], ['11', 3]]) subject.stub(:search).and_return(result) 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{ MultiSolr::SingleCoreHandler.new nil, nil }.to raise_error end it "should connect with solr-url + core-name" do RSolr.should_receive(:connect) do |options| options # zum Test die options selbst wieder zurück liefern end subject.solr_connection[:url].should == "#{SolrTestdataProvider::SOLR_TEST_INSTANCE_URL}/core-01" end end context "search" do before do @request = MultiSolr::SearchRequest.new end it "should parse result if result-format is 'ruby'" do rsolr_mock = double('rsolr') rsolr_mock.should_receive(:get).and_return({}) subject.should_receive(:solr_connection).and_return(rsolr_mock) result = subject.search @request result.should be_a(MultiSolr::SearchResult) end it "should not parse result if result-format is not 'ruby'" do result_mock = "RAW" rsolr_mock = double('rsolr') rsolr_mock.should_receive(:get).and_return(result_mock) subject.should_receive(:solr_connection).and_return(rsolr_mock) result = subject.search @request, :result_format => 'json' result.should == result_mock end end context "build_solr_params" do before do @request = MultiSolr::SearchRequest.new end it "should add force-query if context as hash given" do solr_params = subject.build_solr_params @request, :context => {:whs_id => 8} solr_params[:fq].should == "whs_id:8" end it "should add force-query if context as string given" do solr_params = subject.build_solr_params @request, :context => "(whs_id:8 OR whs_id:9)" solr_params[:fq].should == "(whs_id:8 OR whs_id:9)" end it "should have query for all if no query specified" do solr_params = subject.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 = subject.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 = subject.build_solr_params @request, {} solr_params[:fl].should == "*,score" end it "should use sort if specified" do @request.sorts = ['s1', 's2 desc'] solr_params = subject.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 = subject.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 = subject.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 = subject.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 context "should generate facet-params with facet.prefix if facet prefix is specified" do before do @request.facets = ['f1'] @request.facet_prefix = 'pre' @solr_params = subject.build_solr_params @request, {} end it{ @solr_params[:facet].should be_true } it{ @solr_params['facet.field'].should == [:f1] } it{ @solr_params['facet.prefix'].should == 'pre' } end it "should set result-format if specified" do solr_params = subject.build_solr_params @request, :result_format => 'json' solr_params[:wt].should == 'json' end end end