<commit_before>module ThinkingSphinx
  module ActiveRecord
    module Callbacks; end
  end
end

require 'active_support/core_ext/string/inflections'
require 'thinking_sphinx/callbacks'
require 'thinking_sphinx/errors'
require 'thinking_sphinx/active_record/callbacks/update_callbacks'

describe ThinkingSphinx::ActiveRecord::Callbacks::UpdateCallbacks do
  describe '#after_update' do
    let(:callbacks)     {
      ThinkingSphinx::ActiveRecord::Callbacks::UpdateCallbacks.new instance }
    let(:instance)      { double('instance', :class => klass, :id => 2) }
    let(:klass)         { double(:name => 'Article') }
    let(:configuration) { double('configuration',
      :settings => {'attribute_updates' => true},
      :indices_for_references => [index]) }
    let(:connection)    { double('connection', :execute => '') }
    let(:index)         { double 'index', :name => 'article_core',
      :sources => [source], :document_id_for_key => 3, :distributed? => false }
    let(:source)        { double('source', :attributes => []) }

    before :each do
      stub_const 'ThinkingSphinx::Configuration',
        double(:instance => configuration)
      stub_const 'ThinkingSphinx::Connection', double
      stub_const 'Riddle::Query', double(:update => 'SphinxQL')

      ThinkingSphinx::Connection.stub(:take).and_yield(connection)

      source.attributes.replace([
        double(:name => 'foo', :updateable? => true,
          :columns => [double(:__name => 'foo_column')]),
        double(:name => 'bar', :updateable? => true, :value_for => 7,
          :columns => [double(:__name => 'bar_column')]),
        double(:name => 'baz', :updateable? => false)
      ])

      instance.stub :changed => ['bar_column', 'baz'], :bar_column => 7
    end

    it "does not send any updates to Sphinx if updates are disabled" do
      configuration.settings['attribute_updates'] = false

      connection.should_not_receive(:execute)

      callbacks.after_update
    end

    it "builds an update query with only updateable attributes that have changed" do
      Riddle::Query.should_receive(:update).
        with('article_core', 3, 'bar' => 7).and_return('SphinxQL')

      callbacks.after_update
    end

    it "sends the update query through to Sphinx" do
      connection.should_receive(:execute).with('SphinxQL')

      callbacks.after_update
    end

    it "doesn't care if the update fails at Sphinx's end" do
      connection.stub(:execute).
        and_raise(ThinkingSphinx::ConnectionError.new(''))

      lambda { callbacks.after_update }.should_not raise_error
    end
  end
end
<commit_msg>Spec fix for real-time index exclusion in attribute updates.<commit_after>module ThinkingSphinx
  module ActiveRecord
    module Callbacks; end
  end
end

require 'active_support/core_ext/string/inflections'
require 'thinking_sphinx/callbacks'
require 'thinking_sphinx/errors'
require 'thinking_sphinx/active_record/callbacks/update_callbacks'

describe ThinkingSphinx::ActiveRecord::Callbacks::UpdateCallbacks do
  describe '#after_update' do
    let(:callbacks)     {
      ThinkingSphinx::ActiveRecord::Callbacks::UpdateCallbacks.new instance }
    let(:instance)      { double('instance', :class => klass, :id => 2) }
    let(:klass)         { double(:name => 'Article') }
    let(:configuration) { double('configuration',
      :settings => {'attribute_updates' => true},
      :indices_for_references => [index]) }
    let(:connection)    { double('connection', :execute => '') }
    let(:index)         { double 'index', :name => 'article_core',
      :sources => [source], :document_id_for_key => 3, :distributed? => false,
      :type => 'plain'}
    let(:source)        { double('source', :attributes => []) }

    before :each do
      stub_const 'ThinkingSphinx::Configuration',
        double(:instance => configuration)
      stub_const 'ThinkingSphinx::Connection', double
      stub_const 'Riddle::Query', double(:update => 'SphinxQL')

      ThinkingSphinx::Connection.stub(:take).and_yield(connection)

      source.attributes.replace([
        double(:name => 'foo', :updateable? => true,
          :columns => [double(:__name => 'foo_column')]),
        double(:name => 'bar', :updateable? => true, :value_for => 7,
          :columns => [double(:__name => 'bar_column')]),
        double(:name => 'baz', :updateable? => false)
      ])

      instance.stub :changed => ['bar_column', 'baz'], :bar_column => 7
    end

    it "does not send any updates to Sphinx if updates are disabled" do
      configuration.settings['attribute_updates'] = false

      connection.should_not_receive(:execute)

      callbacks.after_update
    end

    it "builds an update query with only updateable attributes that have changed" do
      Riddle::Query.should_receive(:update).
        with('article_core', 3, 'bar' => 7).and_return('SphinxQL')

      callbacks.after_update
    end

    it "sends the update query through to Sphinx" do
      connection.should_receive(:execute).with('SphinxQL')

      callbacks.after_update
    end

    it "doesn't care if the update fails at Sphinx's end" do
      connection.stub(:execute).
        and_raise(ThinkingSphinx::ConnectionError.new(''))

      lambda { callbacks.after_update }.should_not raise_error
    end
  end
end

{'fraction_non_alphanumeric': 0.1343679031037093, 'fraction_numerical': 0.002271006813020439, 'mean_word_length': 3.591659426585578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5108065', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1542, 'n_words': 354}