Class: SchemaExpectations::RSpecMatchers::ValidateSchemaUniquenessMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb

Instance Method Summary collapse

Methods inherited from Base

#except, #only

Instance Method Details

#descriptionObject



78
79
80
# File 'lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb', line 78

def description
  'validate unique indexes have uniqueness validation'
end

#failure_messageObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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_negatedObject



74
75
76
# 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) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# 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