Class: SchemaExpectations::RSpecMatchers::ValidateSchemaUniquenessMatcher

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

Instance Method Summary collapse

Instance Method Details

#descriptionObject



80
81
82
# File 'lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb', line 80

def description
  'validate unique indexes have uniqueness validation'
end

#except(*args) ⇒ ValidateSchemaUniquenessMatcher

Specifies a list of columns for matcher to ignore

Any unique scope which includes one of these columns will be ignored

Returns:



101
102
103
104
105
106
# File 'lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb', line 101

def except(*args)
  fail 'cannot use only and except' if @only
  @except = Array(args)
  fail 'empty except list' if @except.empty?
  self
end

#failure_messageObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb', line 56

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).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



76
77
78
# File 'lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb', line 76

def failure_message_when_negated
  "#{@model.name} should not match unique indexes with its uniqueness validation but does"
end

#matches?(model) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb', line 47

def matches?(model)
  @model = cast_model model
  @validation_reflector = ActiveRecord::ValidationReflector.new(@model)
  @column_reflector = ActiveRecord::ColumnReflector.new(@model)
  @validator_unique_scopes = filter_scopes(validator_unique_scopes).map(&:sort).sort
  @schema_unique_scopes = filter_scopes(schema_unique_scopes).map(&:sort).sort
  @validator_unique_scopes == @schema_unique_scopes
end

#only(*args) ⇒ ValidateSchemaUniquenessMatcher

Specifies a list of columns to restrict matcher

Any unique scope which includes a column not in this list will be ignored

Returns:



89
90
91
92
93
94
# File 'lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb', line 89

def only(*args)
  fail 'cannot use only and except' if @except
  @only = Array(args)
  fail 'empty only list' if @only.empty?
  self
end