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:
parent
27841b7b4c
commit
d56d4fed0b
|
@ -1,5 +1,5 @@
|
||||||
PATH
|
PATH
|
||||||
remote: /Users/joshuaclayton/dev/gems/shoulda-matchers
|
remote: /home/mike/thoughtbot/shoulda-matchers
|
||||||
specs:
|
specs:
|
||||||
shoulda-matchers (1.3.0)
|
shoulda-matchers (1.3.0)
|
||||||
activesupport (>= 3.0.0)
|
activesupport (>= 3.0.0)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
PATH
|
PATH
|
||||||
remote: /Users/joshuaclayton/dev/gems/shoulda-matchers
|
remote: /home/mike/thoughtbot/shoulda-matchers
|
||||||
specs:
|
specs:
|
||||||
shoulda-matchers (1.3.0)
|
shoulda-matchers (1.3.0)
|
||||||
activesupport (>= 3.0.0)
|
activesupport (>= 3.0.0)
|
||||||
|
@ -46,7 +46,7 @@ GEM
|
||||||
rspec (>= 2.7.0)
|
rspec (>= 2.7.0)
|
||||||
bourne (1.1.2)
|
bourne (1.1.2)
|
||||||
mocha (= 0.10.5)
|
mocha (= 0.10.5)
|
||||||
builder (3.0.0)
|
builder (3.0.3)
|
||||||
childprocess (0.3.5)
|
childprocess (0.3.5)
|
||||||
ffi (~> 1.0, >= 1.0.6)
|
ffi (~> 1.0, >= 1.0.6)
|
||||||
cucumber (1.1.9)
|
cucumber (1.1.9)
|
||||||
|
@ -61,8 +61,8 @@ GEM
|
||||||
gherkin (2.9.3)
|
gherkin (2.9.3)
|
||||||
json (>= 1.4.6)
|
json (>= 1.4.6)
|
||||||
hike (1.2.1)
|
hike (1.2.1)
|
||||||
i18n (0.6.0)
|
i18n (0.6.1)
|
||||||
jquery-rails (2.1.1)
|
jquery-rails (2.1.2)
|
||||||
railties (>= 3.1.0, < 5.0)
|
railties (>= 3.1.0, < 5.0)
|
||||||
thor (~> 0.14)
|
thor (~> 0.14)
|
||||||
json (1.7.5)
|
json (1.7.5)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
PATH
|
PATH
|
||||||
remote: /Users/joshuaclayton/dev/gems/shoulda-matchers
|
remote: /home/mike/thoughtbot/shoulda-matchers
|
||||||
specs:
|
specs:
|
||||||
shoulda-matchers (1.3.0)
|
shoulda-matchers (1.3.0)
|
||||||
activesupport (>= 3.0.0)
|
activesupport (>= 3.0.0)
|
||||||
|
@ -45,7 +45,7 @@ GEM
|
||||||
rspec (>= 2.7.0)
|
rspec (>= 2.7.0)
|
||||||
bourne (1.1.2)
|
bourne (1.1.2)
|
||||||
mocha (= 0.10.5)
|
mocha (= 0.10.5)
|
||||||
builder (3.0.0)
|
builder (3.0.3)
|
||||||
childprocess (0.3.5)
|
childprocess (0.3.5)
|
||||||
ffi (~> 1.0, >= 1.0.6)
|
ffi (~> 1.0, >= 1.0.6)
|
||||||
cucumber (1.1.9)
|
cucumber (1.1.9)
|
||||||
|
@ -60,9 +60,9 @@ GEM
|
||||||
gherkin (2.9.3)
|
gherkin (2.9.3)
|
||||||
json (>= 1.4.6)
|
json (>= 1.4.6)
|
||||||
hike (1.2.1)
|
hike (1.2.1)
|
||||||
i18n (0.6.0)
|
i18n (0.6.1)
|
||||||
journey (1.0.4)
|
journey (1.0.4)
|
||||||
jquery-rails (2.1.1)
|
jquery-rails (2.1.2)
|
||||||
railties (>= 3.1.0, < 5.0)
|
railties (>= 3.1.0, < 5.0)
|
||||||
thor (~> 0.14)
|
thor (~> 0.14)
|
||||||
json (1.7.5)
|
json (1.7.5)
|
||||||
|
|
|
@ -62,13 +62,16 @@ module Shoulda # :nodoc:
|
||||||
def correct_unique?
|
def correct_unique?
|
||||||
return true unless @options.key?(:unique)
|
return true unless @options.key?(:unique)
|
||||||
|
|
||||||
if matched_index.unique == @options[:unique]
|
is_unique = matched_index.unique
|
||||||
true
|
|
||||||
else
|
is_unique = !is_unique unless @options[:unique]
|
||||||
|
|
||||||
|
unless is_unique
|
||||||
@missing = "#{table_name} has an index named #{matched_index.name} " <<
|
@missing = "#{table_name} has an index named #{matched_index.name} " <<
|
||||||
"of unique #{matched_index.unique}, not #{@options[:unique]}."
|
"of unique #{matched_index.unique}, not #{@options[:unique]}."
|
||||||
false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
is_unique
|
||||||
end
|
end
|
||||||
|
|
||||||
def matched_index
|
def matched_index
|
||||||
|
|
|
@ -85,4 +85,21 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher do
|
||||||
it "should not context an index's uniqueness when it isn't important" do
|
it "should not context an index's uniqueness when it isn't important" do
|
||||||
have_db_index(:user_id).description.should_not =~ /unique/
|
have_db_index(:user_id).description.should_not =~ /unique/
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue