Sha256: f942148b594fcd17235a172024967d2ce6aeba0bd10e01a91bf7410720d2b318

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

require 'active_record'
require 'mysql2'
require 'active_record/connection_adapters/mysql2_adapter'
require 'activerecord-mysql-index-hint'

module Mysql2
  class Client
    def initialize(opts={})
      @query_options = @@default_query_options.dup.merge opts
    end
  end
end

ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
  def quote_string(string)
    string
  end
  def configure_connection
  end
end

Arel::Visitors::ToSql.class_eval do
  def column_for o
    nil
  end
end

class Product < ActiveRecord::Base
  establish_connection 'mysql2://user@host/db'
end

describe 'ActiveRecord Index Hint' do
  context 'with AR::Relation' do
    it do
      expect(
        Product.limit(1).use_index(:idx1).to_sql
      ).to eq('SELECT  `products`.* FROM `products` USE INDEX(`idx1`)  LIMIT 1')
    end

    it do
      expect(
        Product.limit(1).force_index(:idx1).to_sql
      ).to eq('SELECT  `products`.* FROM `products` FORCE INDEX(`idx1`)  LIMIT 1')
    end

    it do
      expect(
        Product.limit(1).ignore_index(:idx1).to_sql
      ).to eq('SELECT  `products`.* FROM `products` IGNORE INDEX(`idx1`)  LIMIT 1')
    end
  end

  context 'with AR::Base' do
    it do
      expect(
        Product.use_index(:idx1).limit(1).to_sql
      ).to eq('SELECT  `products`.* FROM `products` USE INDEX(`idx1`)  LIMIT 1')
    end
  end

  context 'with multiple indexes' do
    it do
      expect(
        Product.limit(1).use_index(:idx1, :idx2).to_sql
      ).to eq('SELECT  `products`.* FROM `products` USE INDEX(`idx1`, `idx2`)  LIMIT 1')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-mysql-index-hint-0.0.2 spec/activerecord-mysql-index-hint_spec.rb
activerecord-mysql-index-hint-0.0.1 spec/activerecord-mysql-index-hint_spec.rb