No commit activity in last 3 years
No release in over 3 years
Allows you to test whether your database schema matches the validations in your ActiveRecord models.'
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
~> 0.3.18
~> 0.18
>= 0
~> 10.4
< 3.3, >= 3.0
~> 1.3
>= 0
~> 0.8.7

Runtime

< 4.3, >= 3.1
< 4.3, >= 3.1
 Project Readme

Gem Version Build Status Code Climate Test Coverage Dependency Status

Schema Expectations

Allows you to test whether your database schema matches the validations in your ActiveRecord models.

Documentation

You can find documentation at http://www.rubydoc.info/gems/schema_expectations

Installation

Add schema_expectations to your Gemfile:

group :test do
  gem 'schema_expectations'
end

Usage with RSpec

Validating uniqueness constraints

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.

Validating presence constraints

The validate_schema_nullable matcher tests that an ActiveRecord model has unconditional presence validation on columns with NOT NULL constraints, and vice versa.

For example, we can assert that the model and database are consistent on whether Record#name should be present:

create_table :records do |t|
  t.string :name, null: false
end

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.

License

MIT License