Skip to content

Commit

Permalink
Changed the description for have_db_index to correctly describe multi…
Browse files Browse the repository at this point in the history
…ple columns and index where uniqueness is not specified [#159 state:resolved]
  • Loading branch information
jferris committed May 6, 2009
1 parent 9f24c1d commit f03757e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/shoulda/active_record/matchers/have_db_index_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def negative_failure_message
end

def description
"have a #{index_type} index on columns #{@columns}"
"have a #{index_type} index on columns #{@columns.join(' and ')}"
end

protected
Expand Down Expand Up @@ -88,7 +88,14 @@ def expectation
end

def index_type
@unique ? "unique" : "non-unique"
case @unique
when nil
''
when false
'non-unique'
else
'unique'
end
end

def normalize_columns_to_array(columns)
Expand Down
17 changes: 17 additions & 0 deletions test/matchers/active_record/have_db_index_matcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,22 @@ class HaveDbIndexMatcherTest < ActiveSupport::TestCase # :nodoc:
assert_rejects @matcher, Geocoding.new
end
end

should "join columns with and describing multiple columns" do
assert_match /on columns user_id and post_id/,
have_db_index([:user_id, :post_id]).description
end

should "describe a unique index as unique" do
assert_match /a unique index/, have_db_index(:user_id).unique(true).description
end

should "describe a non-unique index as non-unique" do
assert_match /a non-unique index/, have_db_index(:user_id).unique(false).description
end

should "not describe an index's uniqueness when it isn't important" do
assert_no_match /unique/, have_db_index(:user_id).description
end

end

0 comments on commit f03757e

Please sign in to comment.