spec/supernova/solr_indexer_spec.rb in supernova-0.4.13 vs spec/supernova/solr_indexer_spec.rb in supernova-0.4.14

- old
+ new

@@ -23,10 +23,11 @@ Supernova::Solr.stub!(:connection).and_return solr indexer_clazz.has(:title, :type => :text) indexer_clazz.has(:artist_id, :type => :integer) indexer_clazz.has(:description, :type => :text) indexer_clazz.has(:created_at, :type => :date) + indexer_clazz.has(:indexed, :type => :boolean, :virtual => true) end before(:each) do File.stub!(:open).and_return file_stub Kernel.stub!(:`).and_return true @@ -67,20 +68,10 @@ query = "some query" indexer.stub!(:query_to_index).and_return query indexer.should_receive(:index_query).with(query) indexer.index! end - - it "calls map_for_solr with all returned rows from sql" do - row1 = double("row1") - row2 = double("row2") - indexer.stub!(:query).and_return [row1, row2] - indexer.stub!(:query_to_index).and_return "some query" - indexer.should_receive(:map_for_solr).with(row1) - indexer.stub!(:index_query).and_yield(row1) - indexer.index! - end end describe "#map_for_solr" do let(:row) { { "a" => 1 } } @@ -209,12 +200,21 @@ has :offer_id, :type => :integer has :lat, :type => :float has :lng, :type => :float has :created_at, :type => :date has :checkin_date, :type => :date + has :indexed, :type => :boolean, :virtual => true end + it "maps virtual fields" do + CustomSolrIndex.new.map_hash_keys_to_solr("indexed" => true)["indexed_b"].should == true + end + + it "maps fields with false as value" do + CustomSolrIndex.new.map_hash_keys_to_solr("indexed" => false)["indexed_b"].should == false + end + it "maps float fields" do index = CustomSolrIndex.new index.map_hash_keys_to_solr("lat" => 49.0)["lat_f"].should == 49.0 end @@ -252,13 +252,41 @@ clazz.clazz Offer clazz.new.map_hash_keys_to_solr({})["type"].should == "Offer" end end + describe "#solr_rows_to_index_for_query" do + let(:result) { + [ + { "title" => "Some Title", "artist_id" => 10 } + ] + } + + { "title_t" => "Some Title", "artist_id_i" => 10 }.each do |key, value| + it "sets #{key} to #{value}" do + custom_indexer.should_receive(:query_db).with("some query").and_return(result) + custom_indexer.solr_rows_to_index_for_query("some query").first[key].should == value + end + end + + it "also maps virtual attributes" do + hash = { "indexed" => true } + query = "some query" + custom_indexer.should_receive(:query_db).with(query).and_return([hash]) + custom_indexer.solr_rows_to_index_for_query(query).first["indexed_b"].should == true + end + end + describe "#index_query" do let(:query) { %(SELECT CONCAT("user_", id) AS id, title FROM people WHERE type = 'User') } + it "calls solr_rows_to_index_for_query with query" do + result = [] + indexer.should_receive(:solr_rows_to_index_for_query).with(query).and_return(result) + indexer.index_query(query) + end + it "calls index_with_json_file when rows > max_rows_to_direct_index" do indexer.max_rows_to_direct_index = 0 rows = [to_index] indexer.should_receive(:query_db).with(query).and_return rows indexer.should_receive(:index_with_json_file).with(rows) @@ -287,11 +315,11 @@ indexer.should_receive(:query_db).with(query).and_return [to_index] indexer.index_query(query) end it "calls write_to_file on all rows" do - rows = [double("1"), double("2")] + rows = [{ "b" => 2 }, { "a" => 1 }] indexer.stub(:query_db).and_return rows indexer.should_receive(:write_to_file).with(rows.first) indexer.should_receive(:write_to_file).with(rows.at(1)) indexer.stub!(:finish) indexer.index_query(query) @@ -325,12 +353,10 @@ it "does not call commit when rows is empty" do solr.should_not_receive(:commit) indexer.index_directly([]) end - - it "calls a block given given" end describe "#index_file_path" do it "returns the set file_path" do indexer.index_file_path = "/some/path" @@ -623,21 +649,23 @@ indexer_clazz.where(:a => 1).clazz.should == String end it "adds the attribute_mapping" do indexer_clazz.where(:a => 1).search_options[:attribute_mapping].should == { - :artist_id=>{:type=>:integer}, :title=>{:type=>:text}, :created_at=>{:type=>:date}, :description=>{:type=>:text} + :artist_id=>{:type=>:integer}, :title=>{:type=>:text}, :created_at=>{:type=>:date}, :description=>{:type=>:text}, + :indexed => {:type => :boolean, :virtual => true } } end end describe "#named_search_scope" do it "returns the correct scope" do indexer_clazz.named_search_scope :published do where(:public => true) end indexer_clazz.published.search_options[:attribute_mapping].should == { - :artist_id=>{:type=>:integer}, :title=>{:type=>:text}, :created_at=>{:type=>:date}, :description=>{:type=>:text} + :artist_id=>{:type=>:integer}, :title=>{:type=>:text}, :created_at=>{:type=>:date}, :description=>{:type=>:text}, + :indexed => {:type => :boolean, :virtual => true } } end it "works with attribute mappings" do indexer_clazz.named_search_scope :with_title do