mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Check primality of DB columns. Fixes #35.
This commit is contained in:
parent
2b98e497f8
commit
68e65b2aba
2 changed files with 36 additions and 1 deletions
|
@ -35,6 +35,7 @@ module Shoulda # :nodoc:
|
||||||
@default = opts[:default]
|
@default = opts[:default]
|
||||||
@null = opts[:null]
|
@null = opts[:null]
|
||||||
@scale = opts[:scale]
|
@scale = opts[:scale]
|
||||||
|
@primary = opts[:primary]
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -46,7 +47,8 @@ module Shoulda # :nodoc:
|
||||||
correct_limit? &&
|
correct_limit? &&
|
||||||
correct_default? &&
|
correct_default? &&
|
||||||
correct_null? &&
|
correct_null? &&
|
||||||
correct_scale?
|
correct_scale? &&
|
||||||
|
correct_primary?
|
||||||
end
|
end
|
||||||
|
|
||||||
def failure_message
|
def failure_message
|
||||||
|
@ -150,6 +152,21 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def correct_primary?
|
||||||
|
return true if @primary.nil?
|
||||||
|
if matched_column.primary == @primary
|
||||||
|
true
|
||||||
|
else
|
||||||
|
@missing = "#{model_class} has a db column named #{@column} "
|
||||||
|
if @primary
|
||||||
|
@missing << "that is not primary, but should be"
|
||||||
|
else
|
||||||
|
@missing << "that is primary, but should not be"
|
||||||
|
end
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def matched_column
|
def matched_column
|
||||||
model_class.columns.detect { |each| each.name == @column.to_s }
|
model_class.columns.detect { |each| each.name == @column.to_s }
|
||||||
end
|
end
|
||||||
|
|
|
@ -164,4 +164,22 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbColumnMatcher do
|
||||||
Superhero.new.should_not @matcher
|
Superhero.new.should_not @matcher
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "have_db_column with primary option" do
|
||||||
|
it "should accept a column that is primary" do
|
||||||
|
create_table 'superheros' do |table|
|
||||||
|
table.integer :id, :primary => true
|
||||||
|
end
|
||||||
|
define_model_class 'Superhero'
|
||||||
|
Superhero.new.should have_db_column(:id).with_options(:primary => true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should reject a column that is not primary" do
|
||||||
|
create_table 'superheros' do |table|
|
||||||
|
table.integer :primary
|
||||||
|
end
|
||||||
|
define_model_class 'Superhero'
|
||||||
|
Superhero.new.should_not have_db_column(:primary).with_options(:primary => true)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue