<commit_before>require 'spec_helper'

describe ActiveRecord::ConnectionAdapters::MakaraAdapter do

  class Model < ActiveRecord::Base
  end

  before do
    connect!(config)
  end

  context "a config with a makara adapter and a db_adapter provided" do

    let(:config){ dry_single_slave_config }

    before do
      connect!(config)
    end

    ActiveRecord::ConnectionAdapters::MakaraAdapter::MASS_DELEGATION_METHODS.each do |meth|
      it "should delegate #{meth} to all connections" do
        adapter.mcon.should_receive(meth).once
        adapter.scon(1).should_receive(meth).once
        adapter.send(meth)
      end
    end

    ActiveRecord::ConnectionAdapters::MakaraAdapter::MASS_ANY_DELEGATION_METHODS.each do |meth|
      it "should delegate and evaluate an any? on #{meth}" do
        adapter.mcon.should_receive(meth).once
        adapter.scon(1).should_receive(meth).once
        adapter.send(meth)
      end
    end

    it 'should use the correct wrapper' do

      adapter.mcon.should_receive(:execute).with('insert into dogs...', nil).once
      adapter.mcon.should_receive(:execute).with('insert into cats (select * from felines)', nil).once
      adapter.scon.should_receive(:execute).with('select * from felines', nil).once
      adapter.scon.should_receive(:execute).with('select * from dogs', nil).once

      adapter.execute('select * from dogs')
      adapter.execute('insert into dogs...')
      adapter.execute('select * from felines')
      adapter.execute('insert into cats (select * from felines)')

    end
  end

  context "a config with a makara-* adapter and a db_adapter provided" do

    let(:config){ simple_config.merge(:adapter => 'makara_mysql2') }

    it 'should invoke the makara connection with the appropriate db_adapter' do
      require 'mysql2'
      require 'active_record/connection_adapters/mysql2_adapter'

      abstract_master = Makara::ConfigParser.master_config(config)
      ActiveRecord::Base.should_receive(:mysql2_connection).and_return(ActiveRecord::Base.abstract_connection(abstract_master))
      
      connect!(config)
      ActiveRecord::Base.connection.should be_a(ActiveRecord::ConnectionAdapters::MakaraAdapter)
    end

  end

  it 'should register with the top level Makara' do
    adapter
    Makara.adapters.should eql([adapter])
  end

  it 'should not allow multiple adapters with the same id' do
    lambda{
      ActiveRecord::ConnectionAdapters::MakaraAdapter.new([adapter.mcon])
    }.should raise_error('[Makara] all adapters must be given a unique id. "default" has already been used.')
  end

  it 'should allow multiple adapters as long as they have different id' do
    lambda{
      ActiveRecord::ConnectionAdapters::MakaraAdapter.new([adapter.mcon], :id => 'secondary')
    }.should_not raise_error
  end

end<commit_msg>fix adapter naming spec<commit_after>require 'spec_helper'

describe ActiveRecord::ConnectionAdapters::MakaraAdapter do

  class Model < ActiveRecord::Base
  end

  context "a config with a makara adapter and a db_adapter provided" do

    let(:config){ dry_single_slave_config }

    before do
      connect!(config)
    end

    ActiveRecord::ConnectionAdapters::MakaraAdapter::MASS_DELEGATION_METHODS.each do |meth|
      it "should delegate #{meth} to all connections" do
        adapter.mcon.should_receive(meth).once
        adapter.scon(1).should_receive(meth).once
        adapter.send(meth)
      end
    end

    ActiveRecord::ConnectionAdapters::MakaraAdapter::MASS_ANY_DELEGATION_METHODS.each do |meth|
      it "should delegate and evaluate an any? on #{meth}" do
        adapter.mcon.should_receive(meth).once
        adapter.scon(1).should_receive(meth).once
        adapter.send(meth)
      end
    end

    it 'should use the correct wrapper' do

      adapter.mcon.should_receive(:execute).with('insert into dogs...', nil).once
      adapter.mcon.should_receive(:execute).with('insert into cats (select * from felines)', nil).once
      adapter.scon.should_receive(:execute).with('select * from felines', nil).once
      adapter.scon.should_receive(:execute).with('select * from dogs', nil).once

      adapter.execute('select * from dogs')
      adapter.execute('insert into dogs...')
      adapter.execute('select * from felines')
      adapter.execute('insert into cats (select * from felines)')

    end
  end

  context "a config with a makara-* adapter and a db_adapter provided" do

    let(:config){ simple_config.merge(:adapter => 'makara_mysql2') }

    it 'should invoke the makara connection with the appropriate db_adapter' do
      require 'mysql2'
      require 'active_record/connection_adapters/mysql2_adapter'

      abstract_master = Makara::ConfigParser.master_config(config)
      ActiveRecord::Base.should_receive(:mysql2_connection).and_return(ActiveRecord::Base.abstract_connection(abstract_master))
      
      connect!(config)
      ActiveRecord::Base.connection.should be_a(ActiveRecord::ConnectionAdapters::MakaraAdapter)
    end

  end

  describe "registration" do

    let(:config){ simple_config }

    before do
      connect!(config)
    end

    it 'should register with the top level Makara' do
      adapter
      Makara.adapters.should eql([adapter])
    end

    it 'should not allow multiple adapters with the same id' do
      lambda{
        ActiveRecord::ConnectionAdapters::MakaraAdapter.new([adapter.mcon])
      }.should raise_error('[Makara] all adapters must be given a unique id. "default" has already been used.')
    end

    it 'should allow multiple adapters as long as they have different id' do
      lambda{
        ActiveRecord::ConnectionAdapters::MakaraAdapter.new([adapter.mcon], :id => 'secondary')
      }.should_not raise_error
    end
  end
end
{'fraction_non_alphanumeric': 0.0994773519163763, 'fraction_numerical': 0.0020905923344947735, 'mean_word_length': 3.667479674796748, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4186342', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1616, 'n_words': 426}