From d56d4fed0be2456043e74a0cda2c8cd9d90e56bb Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 19 Sep 2012 10:40:57 +0300 Subject: [PATCH] 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. --- gemfiles/3.0.gemfile.lock | 2 +- gemfiles/3.1.gemfile.lock | 8 ++++---- gemfiles/3.2.gemfile.lock | 8 ++++---- .../active_record/have_db_index_matcher.rb | 13 ++++++++----- .../active_record/have_db_index_matcher_spec.rb | 17 +++++++++++++++++ 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/gemfiles/3.0.gemfile.lock b/gemfiles/3.0.gemfile.lock index 5d62c7aa..a2281365 100644 --- a/gemfiles/3.0.gemfile.lock +++ b/gemfiles/3.0.gemfile.lock @@ -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) diff --git a/gemfiles/3.1.gemfile.lock b/gemfiles/3.1.gemfile.lock index 0c2efdf6..aeaa99b0 100644 --- a/gemfiles/3.1.gemfile.lock +++ b/gemfiles/3.1.gemfile.lock @@ -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) diff --git a/gemfiles/3.2.gemfile.lock b/gemfiles/3.2.gemfile.lock index 77d50b5b..4b98501d 100644 --- a/gemfiles/3.2.gemfile.lock +++ b/gemfiles/3.2.gemfile.lock @@ -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) diff --git a/lib/shoulda/matchers/active_record/have_db_index_matcher.rb b/lib/shoulda/matchers/active_record/have_db_index_matcher.rb index 250d3ac4..457ddbfe 100644 --- a/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +++ b/lib/shoulda/matchers/active_record/have_db_index_matcher.rb @@ -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 diff --git a/spec/shoulda/active_record/have_db_index_matcher_spec.rb b/spec/shoulda/active_record/have_db_index_matcher_spec.rb index 85da9617..6b6dc515 100644 --- a/spec/shoulda/active_record/have_db_index_matcher_spec.rb +++ b/spec/shoulda/active_record/have_db_index_matcher_spec.rb @@ -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