Update have_db_index_matcher to test for custom db connection

This commit is contained in:
Joey Cheng 2019-04-13 20:01:19 +08:00 committed by Elliot Winkler
parent b53433f96d
commit dccdbc0806
2 changed files with 7 additions and 4 deletions

View File

@ -160,7 +160,7 @@ module Shoulda
end
def indexes
::ActiveRecord::Base.connection.indexes(table_name)
model_class.connection.indexes(table_name)
end
def expectation

View File

@ -3,7 +3,8 @@ require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do
context 'have_db_index' do
it 'accepts an existing index' do
expect(with_index_on(:age)).to have_db_index(:age)
expect(with_index_on(:age1, parent_class: DevelopmentRecord)).to have_db_index(:age1)
expect(with_index_on(:age2, parent_class: ProductionRecord)).to have_db_index(:age2)
end
it 'rejects a nonexistent index' do
@ -77,9 +78,11 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do
end
def with_index_on(column_name, index_options = {})
create_table 'employees' do |table|
parent_class = index_options.delete(:parent_class) || ActiveRecord::Base
create_table('employees', connection: parent_class.connection) do |table|
table.integer column_name
end.add_index(:employees, column_name, index_options)
define_model_class('Employee').new
define_model_class('Employee', parent_class: parent_class).new
end
end