Refactor have_db_index_matcher#correct_unique?

Currently, there is an assumption that `matched_index.unique` will
be `true` and not truthy. This is not always the case. This can
cause tests to fail, even though they should pass. This allows
`matched_index.unique` to be truthy.
This commit is contained in:
Steve Klabnik 2012-09-19 10:40:57 +03:00 committed by Mike Burns
parent 27841b7b4c
commit d56d4fed0b
5 changed files with 34 additions and 14 deletions

View File

@ -1,5 +1,5 @@
PATH
remote: /Users/joshuaclayton/dev/gems/shoulda-matchers
remote: /home/mike/thoughtbot/shoulda-matchers
specs:
shoulda-matchers (1.3.0)
activesupport (>= 3.0.0)

View File

@ -1,5 +1,5 @@
PATH
remote: /Users/joshuaclayton/dev/gems/shoulda-matchers
remote: /home/mike/thoughtbot/shoulda-matchers
specs:
shoulda-matchers (1.3.0)
activesupport (>= 3.0.0)
@ -46,7 +46,7 @@ GEM
rspec (>= 2.7.0)
bourne (1.1.2)
mocha (= 0.10.5)
builder (3.0.0)
builder (3.0.3)
childprocess (0.3.5)
ffi (~> 1.0, >= 1.0.6)
cucumber (1.1.9)
@ -61,8 +61,8 @@ GEM
gherkin (2.9.3)
json (>= 1.4.6)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (2.1.1)
i18n (0.6.1)
jquery-rails (2.1.2)
railties (>= 3.1.0, < 5.0)
thor (~> 0.14)
json (1.7.5)

View File

@ -1,5 +1,5 @@
PATH
remote: /Users/joshuaclayton/dev/gems/shoulda-matchers
remote: /home/mike/thoughtbot/shoulda-matchers
specs:
shoulda-matchers (1.3.0)
activesupport (>= 3.0.0)
@ -45,7 +45,7 @@ GEM
rspec (>= 2.7.0)
bourne (1.1.2)
mocha (= 0.10.5)
builder (3.0.0)
builder (3.0.3)
childprocess (0.3.5)
ffi (~> 1.0, >= 1.0.6)
cucumber (1.1.9)
@ -60,9 +60,9 @@ GEM
gherkin (2.9.3)
json (>= 1.4.6)
hike (1.2.1)
i18n (0.6.0)
i18n (0.6.1)
journey (1.0.4)
jquery-rails (2.1.1)
jquery-rails (2.1.2)
railties (>= 3.1.0, < 5.0)
thor (~> 0.14)
json (1.7.5)

View File

@ -62,13 +62,16 @@ module Shoulda # :nodoc:
def correct_unique?
return true unless @options.key?(:unique)
if matched_index.unique == @options[:unique]
true
else
is_unique = matched_index.unique
is_unique = !is_unique unless @options[:unique]
unless is_unique
@missing = "#{table_name} has an index named #{matched_index.name} " <<
"of unique #{matched_index.unique}, not #{@options[:unique]}."
false
"of unique #{matched_index.unique}, not #{@options[:unique]}."
end
is_unique
end
def matched_index

View File

@ -85,4 +85,21 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher do
it "should not context an index's uniqueness when it isn't important" do
have_db_index(:user_id).description.should_not =~ /unique/
end
it "allows an IndexDefinition to have a truthy value for unique" do
db_connection = create_table 'superheros' do |table|
table.integer :age
end
db_connection.add_index :superheros, :age
define_model_class 'Superhero'
@matcher = have_db_index(:age).unique(true)
index_definition = stub("ActiveRecord::ConnectionAdapters::IndexDefinition",
:unique => 7,
:name => :age)
@matcher.stubs(:matched_index => index_definition)
Superhero.new.should @matcher
end
end