From 5a501a61a99142ac23a9ee81bbe1206dd64e282c Mon Sep 17 00:00:00 2001 From: Merkushin Date: Tue, 12 Mar 2013 17:12:40 +0600 Subject: [PATCH] source specs --- lib/sphinx_integration/extensions/search.rb | 39 +--------- lib/sphinx_integration/extensions/source.rb | 6 +- .../extensions/source_spec.rb | 77 +++++++++++++++++++ 3 files changed, 84 insertions(+), 38 deletions(-) create mode 100644 spec/sphinx_integration/extensions/source_spec.rb diff --git a/lib/sphinx_integration/extensions/search.rb b/lib/sphinx_integration/extensions/search.rb index 5c30f54..b750595 100644 --- a/lib/sphinx_integration/extensions/search.rb +++ b/lib/sphinx_integration/extensions/search.rb @@ -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 \ No newline at end of file diff --git a/lib/sphinx_integration/extensions/source.rb b/lib/sphinx_integration/extensions/source.rb index 2b24f77..6f74964 100644 --- a/lib/sphinx_integration/extensions/source.rb +++ b/lib/sphinx_integration/extensions/source.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/spec/sphinx_integration/extensions/source_spec.rb b/spec/sphinx_integration/extensions/source_spec.rb new file mode 100644 index 0000000..cc95adf --- /dev/null +++ b/spec/sphinx_integration/extensions/source_spec.rb @@ -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 \ No newline at end of file