This works

This commit is contained in:
Elliot Winkler 2019-04-26 20:43:08 -04:00
parent dccdbc0806
commit e716a5b421
1 changed files with 21 additions and 5 deletions

View File

@ -3,8 +3,20 @@ 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(:age1, parent_class: DevelopmentRecord)).to have_db_index(:age1)
expect(with_index_on(:age2, parent_class: ProductionRecord)).to have_db_index(:age2)
model_connected_to_development = with_index_on(
: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
it 'rejects a nonexistent index' do
@ -78,11 +90,15 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do
end
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
create_table('employees', connection: parent_class.connection) do |table|
create_table(table_name, connection: parent_class.connection) do |table|
table.integer column_name
end.add_index(:employees, column_name, index_options)
define_model_class('Employee', parent_class: parent_class).new
end.add_index(table_name.to_sym, column_name, index_options)
define_model_class(model_name, parent_class: parent_class) do |model|
model.table_name = table_name
end.new
end
end