This works
This commit is contained in:
parent
dccdbc0806
commit
e716a5b421
|
@ -3,8 +3,20 @@ require 'unit_spec_helper'
|
||||||
describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do
|
describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do
|
||||||
context 'have_db_index' do
|
context 'have_db_index' do
|
||||||
it 'accepts an existing index' do
|
it 'accepts an existing index' do
|
||||||
expect(with_index_on(:age1, parent_class: DevelopmentRecord)).to have_db_index(:age1)
|
model_connected_to_development = with_index_on(
|
||||||
expect(with_index_on(:age2, parent_class: ProductionRecord)).to have_db_index(:age2)
|
:age1,
|
||||||
|
model_name: 'EmployeeInDevelopment',
|
||||||
|
table_name: 'employees_in_development',
|
||||||
|
parent_class: DevelopmentRecord,
|
||||||
|
)
|
||||||
|
model_connected_to_production = with_index_on(
|
||||||
|
:age2,
|
||||||
|
model_name: 'EmployeeInProduction',
|
||||||
|
table_name: 'employees_in_production',
|
||||||
|
parent_class: ProductionRecord,
|
||||||
|
)
|
||||||
|
expect(model_connected_to_development).to have_db_index(:age1)
|
||||||
|
expect(model_connected_to_production).to have_db_index(:age2)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'rejects a nonexistent index' do
|
it 'rejects a nonexistent index' do
|
||||||
|
@ -78,11 +90,15 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_index_on(column_name, index_options = {})
|
def with_index_on(column_name, index_options = {})
|
||||||
|
model_name = index_options.delete(:model_name) || 'Employee'
|
||||||
|
table_name = index_options.delete(:table_name) || 'employees'
|
||||||
parent_class = index_options.delete(:parent_class) || ActiveRecord::Base
|
parent_class = index_options.delete(:parent_class) || ActiveRecord::Base
|
||||||
|
|
||||||
create_table('employees', connection: parent_class.connection) do |table|
|
create_table(table_name, connection: parent_class.connection) do |table|
|
||||||
table.integer column_name
|
table.integer column_name
|
||||||
end.add_index(:employees, column_name, index_options)
|
end.add_index(table_name.to_sym, column_name, index_options)
|
||||||
define_model_class('Employee', parent_class: parent_class).new
|
define_model_class(model_name, parent_class: parent_class) do |model|
|
||||||
|
model.table_name = table_name
|
||||||
|
end.new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue