spec/supernova/solr_indexer_spec.rb in supernova-0.3.12 vs spec/supernova/solr_indexer_spec.rb in supernova-0.3.13
- old
+ new
@@ -4,15 +4,17 @@
let(:indexer_clazz) { Class.new(Supernova::SolrIndexer) }
let(:db) { double("db", :query => [to_index]) }
let(:to_index) { { :id => 1, :title => "Some Title"} }
let(:file_stub) { double("file").as_null_object }
let(:solr) { double("solr").as_null_object }
+ let(:solr_index_response) { %(<int name="status">0</int>) }
let(:indexer) do
indexer = Supernova::SolrIndexer.new
indexer.db = db
indexer.stub!(:system).and_return true
+ Kernel.stub!(:`).and_return solr_index_response
indexer
end
let(:custom_indexer) { indexer_clazz.new }
@@ -66,21 +68,85 @@
indexer.stub!(:query_to_index).and_return query
indexer.should_receive(:index_query).with(query)
indexer.index!
end
- it "calls row_to_solr with all returned rows from sql" do
+ 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(:row_to_solr).with(row1)
+ 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 } }
+
+ before(:each) do
+ indexer.stub!(:puts)
+ end
+
+ it "calls row_to_solr" do
+ indexer.should_not_receive(:before_index)
+ indexer.should_receive(:row_to_solr).with(row).and_return row
+ indexer.map_for_solr(row)
+ end
+
+ it "prints a deprecation warning when using row_to_solr" do
+ indexer.stub!(:row_to_solr).with(row).and_return row
+ indexer.should_receive(:puts).with(/DEPRECATION WARNING: use before_index instead of row_to_solr! in/)
+ indexer.map_for_solr(row)
+ end
+
+ it "calls before_index when row_to_solr is not defined" do
+ row = { "a" => 1 }
+ indexer.should_receive(:before_index).with(row).and_return row
+ indexer.map_for_solr(row)
+ end
+
+ it "calls map_hash_keys_to_solr with result of row_to_solr" do
+ dummy_row = double("dummy row")
+ indexer.stub!(:row_to_solr).and_return dummy_row
+ indexer.should_receive(:map_hash_keys_to_solr).with(dummy_row)
+ indexer.map_for_solr({ "a" => 1 })
+ end
+
+ describe "with the index defining extra_attributes_from_record" do
+ let(:index) { SolrOfferIndex.new }
+ let(:offer_double) { double("Solr Offer", :id => 88).as_null_object }
+
+ class SolrOfferIndex < Supernova::SolrIndexer
+ clazz Offer
+ has :created_at, :type => :date
+ has :offer_id, :type => :integer
+
+ def extra_attributes_from_record(doc)
+ { :offer_code => "OFFER_#{doc.id}" }
+ end
+ end
+
+ it "calls Supernova.build_ar_like_record with correct parameters" do
+ Supernova.should_receive(:build_ar_like_record).and_return offer_double
+ SolrOfferIndex.new("offer_id" => 77, "type" => "Offer").map_for_solr(row)
+ end
+
+ it "includes the original attributes" do
+ index = SolrOfferIndex.new
+ index.map_for_solr({ "a" => 2 })["a"].should == 2
+ end
+
+ it "includes the attributes from extra_attributes_from_record" do
+ index = SolrOfferIndex.new
+ index.map_for_solr({ "a" => 2, "id" => "88" })["offer_code"].should == "OFFER_88"
+ hash = { :a => 1, "a" => 2 }
+ end
+ end
+ end
+
describe "validate_lat" do
{ nil => nil, 10 => 10.0, 90.1 => nil, 90 => 90, -90.1 => nil, -90 => -90 }.each do |from, to|
it "converts #{from} to #{to}" do
indexer.validate_lat(from).should == to
end
@@ -116,13 +182,13 @@
indexer.sql_column_from_field_and_type(:text, :rgne)
}.should raise_error
end
end
- describe "#row_to_solr" do
+ describe "#before_index" do
it "returns the db row by default" do
- indexer.row_to_solr("id" => 1).should == { "id" => 1 }
+ indexer.before_index("id" => 1).should == { "id" => 1 }
end
end
describe "#query_db" do
it "executes the query" do
@@ -136,10 +202,60 @@
old_mysql_double.should_receive(:select_all).with("query").and_return [to_index]
indexer.query_db("query")
end
end
+ describe "#map_hash_keys_to_solr" do
+ class CustomSolrIndex < Supernova::SolrIndexer
+ has :offer_id, :type => :integer
+ has :lat, :type => :float
+ has :lng, :type => :float
+ has :created_at, :type => :date
+ has :checkin_date, :type => :date
+ end
+
+ it "maps float fields" do
+ index = CustomSolrIndex.new
+ index.map_hash_keys_to_solr("lat" => 49.0)["lat_f"].should == 49.0
+ end
+
+ it "maps time fields to iso8601" do
+ index = CustomSolrIndex.new
+ time = Time.parse("2011-02-03 11:20:30")
+ index.map_hash_keys_to_solr("created_at" => time)["created_at_dt"].should == "2011-02-03T10:20:30Z"
+ end
+
+ it "maps date fields to iso8601" do
+ date = Date.new(2011, 1, 2)
+ CustomSolrIndex.new.map_hash_keys_to_solr("checkin_date" => date)["checkin_date_dt"].should == "2011-01-02T00:00:00Z"
+ end
+
+ it "sets the indexed_at time" do
+ Time.stub!(:now).and_return Time.parse("2011-02-03T11:20:30Z")
+ CustomSolrIndex.new.map_hash_keys_to_solr({})["indexed_at_dt"].should == "2011-02-03T11:20:30Z"
+ Time.unstub!(:now)
+ end
+
+ it "adds the class as type when class set" do
+ clazz = Class.new(Supernova::SolrIndexer)
+ clazz.clazz Offer
+ clazz.new.map_hash_keys_to_solr({})["type"].should == "Offer"
+ end
+
+ it "uses the table_name as prefix for ids" do
+ clazz = Class.new(Supernova::SolrIndexer)
+ clazz.table_name :people
+ clazz.new.map_hash_keys_to_solr({ "id" => 88 })["id_s"].should == "people/88"
+ end
+
+ it "adds the sets the cla" do
+ clazz = Class.new(Supernova::SolrIndexer)
+ clazz.clazz Offer
+ clazz.new.map_hash_keys_to_solr({})["type"].should == "Offer"
+ end
+ end
+
describe "#index_query" do
let(:query) { %(SELECT CONCAT("user_", id) AS id, title FROM people WHERE type = 'User') }
it "calls index_with_json_file when rows > max_rows_to_direct_index" do
indexer.max_rows_to_direct_index = 0
@@ -298,32 +414,34 @@
}.should raise_error("solr not configured")
end
it "calls the correct curl command" do
indexer = Supernova::SolrIndexer.new(:index_file_path => "/tmp/some_path.json", :local_solr => true)
- Kernel.should_receive(:`).with("curl -s 'http://solr.xx:9333/solr/update/json?commit=true\\&stream.file=/tmp/some_path.json'").and_return "<\"status\">0"
+ Kernel.should_receive(:`).with("curl -s 'http://solr.xx:9333/solr/update/json?commit=true\\&stream.file=/tmp/some_path.json'").and_return solr_index_response
indexer.do_index_file(:local => true)
end
it "calls rm on file" do
indexer = Supernova::SolrIndexer.new(:index_file_path => "/tmp/some_path.json", :local_solr => true)
- Kernel.should_receive(:`).with("curl -s 'http://solr.xx:9333/solr/update/json?commit=true\\&stream.file=/tmp/some_path.json'").and_return %(<int name="status">0</int>)
+ Kernel.should_receive(:`).with("curl -s 'http://solr.xx:9333/solr/update/json?commit=true\\&stream.file=/tmp/some_path.json'").and_return solr_index_response
FileUtils.should_receive(:rm_f).with("/tmp/some_path.json")
indexer.do_index_file(:local => true)
end
it "does not call rm when not successful" do
indexer = Supernova::SolrIndexer.new(:index_file_path => "/tmp/some_path.json", :local_solr => true)
Kernel.should_receive(:`).with("curl -s 'http://solr.xx:9333/solr/update/json?commit=true\\&stream.file=/tmp/some_path.json'").and_return %(<int name="status">1</int>)
FileUtils.should_not_receive(:rm_f).with("/tmp/some_path.json")
- indexer.do_index_file(:local => true)
+ lambda {
+ indexer.do_index_file(:local => true)
+ }.should raise_error
end
it "executes the correct curl call when not local" do
# curl 'http://localhost:8983/solr/update/json?commit=true' --data-binary @books.json -H 'Content-type:application/json'
indexer.index_file_path = "/tmp/some_path.json"
- Kernel.should_receive(:`).with("cd /tmp && curl -s 'http://solr.xx:9333/solr/update/json?commit=true' --data-binary @some_path.json -H 'Content-type:application/json'")
+ Kernel.should_receive(:`).with("cd /tmp && curl -s 'http://solr.xx:9333/solr/update/json?commit=true' --data-binary @some_path.json -H 'Content-type:application/json'").and_return solr_index_response
indexer.do_index_file
end
end
describe "define mappings" do
@@ -392,22 +510,20 @@
it "calls field_definitions" do
indexer_clazz.should_receive(:field_definitions).and_return field_definitions
custom_indexer.defined_fields
end
- ["title AS title_t", "artist_id AS artist_id_i", "description AS description_t",
- %(IF(created_at IS NULL, NULL, CONCAT(REPLACE(created_at, " ", "T"), "Z")) AS created_at_dt)
- ].each do |field|
+ ["title", "artist_id", "description", "created_at"].each do |field|
it "includes field #{field.inspect}" do
custom_indexer.defined_fields.should include(field)
end
end
it "does not include virtual fields" do
clazz = Class.new(Supernova::SolrIndexer)
clazz.has :location, :type => :location, :virtual => true
clazz.has :title, :type => :string
- clazz.new.defined_fields.should == ["title AS title_s"]
+ clazz.new.defined_fields.should == ["title"]
end
end
describe "#table_name" do
it "returns nil when no table_name defined on indexer class and no class defined" do