Module: SchemaExpectations::RSpecMatchers
- Defined in:
- lib/schema_expectations/rspec_matchers/base.rb,
lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb,
lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb
Defined Under Namespace
Classes: Base, ValidateSchemaNullableMatcher, ValidateSchemaUniquenessMatcher
Instance Method Summary collapse
-
#validate_schema_nullable ⇒ ValidateSchemaNullableMatcher
class Record < ActiveRecord::Base validates :name, presence: true end.
-
#validate_schema_uniqueness ⇒ ValidateSchemaUniquenessMatcher
The ‘validate_schema_uniqueness` matcher tests that an ActiveRecord model has uniqueness validation on columns with database uniqueness constraints, and vice versa.
Instance Method Details
#validate_schema_nullable ⇒ ValidateSchemaNullableMatcher
class Record < ActiveRecord::Base
validates :name, presence: true
end
# RSpec
describe Record do
it { should validate_schema_nullable }
end
You can restrict the columns tested:
# RSpec
describe Record do
it { should validate_schema_nullable.only(:name) }
it { should validate_schema_nullable.except(:name) }
end
The primary key and timestamp columns are automatically skipped.
37 38 39 |
# File 'lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb', line 37 def validate_schema_nullable ValidateSchemaNullableMatcher.new end |
#validate_schema_uniqueness ⇒ ValidateSchemaUniquenessMatcher
The ‘validate_schema_uniqueness` matcher tests that an ActiveRecord model has uniqueness validation on columns with database uniqueness constraints, and vice versa.
For example, we can assert that the model and database are consistent on whether ‘record_type` and `record_id` should be unique:
create_table :records do |t|
t.integer :record_type
t.integer :record_id
t.index [:record_type, :record_id], unique: true
end
class Record < ActiveRecord::Base
validates :record_type, uniqueness: { scope: :record_id }
end
# RSpec
describe Record do
it { should validate_schema_uniqueness }
end
You can restrict the columns tested:
# RSpec
describe Record do
it { should validate_schema_uniqueness.only(:record_id, :record_type) }
it { should validate_schema_uniqueness.except(:record_id, :record_type) }
end
note: if you exclude a column, then every unique scope which includes it will be completely ignored, regardless of whether that scope includes other non-excluded columns. Only works similarly, in that it will ignore any scope which contains columns not in the list
Absence validation on any attribute in a scope absolves requiring uniqueness validation.
43 44 45 |
# File 'lib/schema_expectations/rspec_matchers/validate_schema_uniqueness.rb', line 43 def validate_schema_uniqueness ValidateSchemaUniquenessMatcher.new end |