Skip to content

Commit

Permalink
source specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Mar 12, 2013
1 parent 3c6f899 commit 5a501a6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 38 deletions.
39 changes: 3 additions & 36 deletions lib/sphinx_integration/extensions/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,10 @@ module SphinxIntegration::Extensions::Search
end

def populate_with_logging
return if @populated
@populated = true
retries = hard_retries

begin
retry_on_stale_index do
begin
log "#{query}, #{options.dup.tap{|o| o[:classes] = o[:classes].map(&:name) if o[:classes] }.inspect}" do
@results = client.query query, indexes, comment
end
total = @results[:total_found].to_i
log "Found #{total} result#{'s' unless total == 1}"

log "Sphinx Daemon returned warning: #{warning}" if warning?

if error?
log "Sphinx Daemon returned error: #{error}"
raise SphinxError.new(error, @results) unless options[:ignore_errors]
end
rescue Errno::ECONNREFUSED => err
raise ThinkingSphinx::ConnectionError,
'Connection to Sphinx Daemon (searchd) failed.'
end

compose_results
end
rescue => e
log 'Caught Sphinx exception: %s (%s %s left)' % [
e.message, retries, (retries == 1 ? 'try' : 'tries')
]
retries -= 1
if retries >= 0
retry
else
raise e
end
unless @populated
log "#{query}, #{options.dup.tap{|o| o[:classes] = o[:classes].map(&:name) if o[:classes] }.inspect}"
end
populate_without_logging
end

end
6 changes: 4 additions & 2 deletions lib/sphinx_integration/extensions/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def set_source_database_settings_with_slave(source)
config = nil

if slave_db_key
db_config = YAML.load(IO.read("#{Rails.root}/config/database.yml")).with_indifferent_access

if slave_db_key.is_a?(String)
slave_db_key = "#{Rails.env}_#{slave_db_key}"
else
Expand All @@ -40,4 +38,8 @@ def set_source_database_settings_with_slave(source)
source.mysql_ssl_key = config[:sslkey] if config[:sslkey]
end

def db_config
@db_config ||= YAML.load(IO.read("#{Rails.root}/config/database.yml")).with_indifferent_access
end

end
77 changes: 77 additions & 0 deletions spec/sphinx_integration/extensions/source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# coding: utf-8
require 'spec_helper'

describe ThinkingSphinx::Source do

describe '#set_source_database_settings' do
let(:db_config) do
{
:test_first_slave => {
:host => 'first-slave',
:username => 'first-slave-root',
:database => 'first-slave-db'
},
:test_slave => {
:host => 'default-slave',
:username => 'default-slave-root',
:database => 'default-slave-db'
},
:test => {
:host => 'test',
:username => 'test-root',
:database => 'test-db'
}
}.with_indifferent_access
end

let(:index_source){ index.sources.first }

before do
index_source.stub(:db_config).and_return(db_config)
index_source.instance_variable_set(:@database_configuration, db_config[:test])
end

subject { index_source.to_riddle_for_core(0, 0) }

context 'when slave' do
context 'when slave property is string' do
let(:index) do
ThinkingSphinx::Index::Builder.generate(ModelWithDisk, nil) do
indexes 'content', :as => :content
set_property :use_slave_db => 'first_slave'
end
end

its(:sql_host){ should eq db_config[:test_first_slave][:host] }
its(:sql_user){ should eq db_config[:test_first_slave][:username] }
its(:sql_db){ should eq db_config[:test_first_slave][:database] }
end

context 'when slave property is true' do
let(:index) do
ThinkingSphinx::Index::Builder.generate(ModelWithDisk, nil) do
indexes 'content', :as => :content
set_property :use_slave_db => true
end
end

its(:sql_host){ should eq db_config[:test_slave][:host] }
its(:sql_user){ should eq db_config[:test_slave][:username] }
its(:sql_db){ should eq db_config[:test_slave][:database] }
end

context 'when without slave property' do
let(:index) do
ThinkingSphinx::Index::Builder.generate(ModelWithDisk, nil) do
indexes 'content', :as => :content
end
end

its(:sql_host){ should eq db_config[:test][:host] }
its(:sql_user){ should eq db_config[:test][:username] }
its(:sql_db){ should eq db_config[:test][:database] }
end
end
end

end

0 comments on commit 5a501a6

Please sign in to comment.