Permalink
Browse files

added unique option to have_index matcher

  • Loading branch information...
1 parent 114df85 commit ecb6bdc2eace69101d0022ecd4afde79d14da7d9 @croaky croaky committed with jferris Jan 25, 2009
@@ -19,10 +19,15 @@ def initialize(macro, index)
@macro = macro
@index = index
end
+
+ def unique(unique)
+ @unique = unique
+ self
+ end
def matches?(subject)
@subject = subject
- index_exists?
+ index_exists? && correct_unique?
end
def failure_message
@@ -43,6 +48,17 @@ def index_exists?
! matched_index.nil?
end
+ def correct_unique?
+ return true if @unique.nil?
+ if matched_index.unique == @unique
+ true
+ else
+ @missing = "#{model_class} has an index named #{matched_index.name} " <<
+ "of unique #{matched_index.unique}, not #{@unique}."
+ false
+ end
+ end
+
def matched_index
indexes.detect { |each| each.columns.include?(@index.to_s) }
end
@@ -22,4 +22,28 @@ class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
end
end
+ context "have_index with unique option" do
+ setup do
+ @matcher = have_index(:ssn).unique(true)
+ end
+
+ should "accept an index of correct unique" do
+ db_connection = create_table 'superheros' do |table|
+ table.integer :ssn
+ end
+ db_connection.add_index :superheros, :ssn, :unique => true
+ define_model_class 'Superhero'
+ assert_accepts @matcher, Superhero.new
+ end
+
+ should "reject an index of wrong unique" do
+ db_connection = create_table 'superheros' do |table|
+ table.integer :ssn
+ end
+ db_connection.add_index :superheros, :ssn, :unique => false
+ define_model_class 'Superhero'
+ assert_rejects @matcher, Superhero.new
+ end
+ end
+
end

0 comments on commit ecb6bdc

Please sign in to comment.