class SchemaExpectations::RSpecMatchers::ValidateSchemaUniquenessMatcher

Public Instance Methods

description() click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 78
def description
  'validate unique indexes have uniqueness validation'
end
failure_message() click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 54
def failure_message
  errors = []

  (@validator_unique_scopes - @schema_unique_scopes).each do |scope|
    errors << "#{@model.name} scope #{scope.inspect} has unconditional uniqueness validation but is missing a unique database index"
  end

  (@schema_unique_scopes - @validator_unique_scopes - absent_scopes).each do |scope|
    conditions = validator_conditions_for_scope(scope) ||
      validator_allow_empty_conditions_for_scope(scope)
    if conditions
      errors << "#{@model.name} scope #{scope.inspect} has a unique index but its uniqueness validator was conditional: #{conditions.inspect}"
    else
      errors << "#{@model.name} scope #{scope.inspect} has a unique index but no uniqueness validation"
    end
  end

  errors.join(', ')
end
failure_message_when_negated() click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 74
def failure_message_when_negated
  "#{@model.name} should not match unique indexes with its uniqueness validation but does"
end
matches?(model) click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 48
def matches?(model)
  setup(model)
  (@validator_unique_scopes - @schema_unique_scopes).empty? &&
    (@schema_unique_scopes - @validator_unique_scopes - absent_scopes).empty?
end

Private Instance Methods

absent_scopes() click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 132
def absent_scopes
  scopes = @validator_unique_scopes + @schema_unique_scopes
  absent_attributes = @validation_reflector.
    absence.unconditional.disallow_empty.attributes
  absent_columns = @column_reflector.for_attributes(*absent_attributes).column_names

  scopes.reject do |scope|
    (scope & absent_columns).empty?
  end
end
deep_sort(scopes) click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 108
def deep_sort(scopes)
  scopes.map(&:sort).sort
end
filter_scopes(scopes) click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 98
def filter_scopes(scopes)
  if @only
    scopes.select { |scope| (scope - @only).empty? }
  elsif @except
    scopes.select { |scope| (scope & @except).empty? }
  else
    scopes
  end
end
schema_unique_scopes() click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 94
def schema_unique_scopes
  @column_reflector.unique_scopes
end
setup(model) click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 84
def setup(model)
  super
  @validator_unique_scopes = deep_sort(filter_scopes(validator_unique_scopes))
  @schema_unique_scopes = deep_sort(filter_scopes(schema_unique_scopes))
end
validator_allow_empty_conditions_for_scope(scope) click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 118
def validator_allow_empty_conditions_for_scope(scope)
  validator_conditions(scope) do |reflector, attribute|
    reflector.allow_empty_conditions_for_attribute attribute
  end
end
validator_conditions(scope) { |reflector, attribute| ... } click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 124
def validator_conditions(scope)
  reflector = @validation_reflector.for_unique_scope(scope)
  conditions = reflector.attributes.map do |attribute|
    yield reflector, attribute
  end
  conditions.compact.first
end
validator_conditions_for_scope(scope) click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 112
def validator_conditions_for_scope(scope)
  validator_conditions(scope) do |reflector, attribute|
    reflector.conditions_for_attribute attribute
  end
end
validator_unique_scopes() click to toggle source
# File lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb, line 90
def validator_unique_scopes
  @validation_reflector.unconditional.disallow_empty.unique_scopes
end