public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
added should_have_indices to test for database indices
tsaleh (author)
Sat Apr 26 14:32:16 -0700 2008
commit  102fe37adc9a9b2c23132791bf16c0ff13553b3b
tree    378bbc14fb925c3b1c535be2425e762f39b168d7
parent  2c975a161cb36e3b04d56a48c6341f54706758ba
...
522
523
524
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
525
526
527
...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
0
@@ -522,6 +522,26 @@ module ThoughtBot # :nodoc:
0
           end
0
         end
0
       end
0
+
0
+ # Ensures that there are DB indices on the given columns or tuples of columns.
0
+ # Also aliased to should_have_index for readability
0
+ #
0
+ # should_have_indices :email, :name, [:commentable_type, :commentable_id]
0
+ # should_have_index :age
0
+ #
0
+ def should_have_indices(*columns)
0
+ table = model_class.name.tableize
0
+ indices = ::ActiveRecord::Base.connection.indexes(table).map(&:columns)
0
+
0
+ columns.each do |column|
0
+ should "have index on #{table} for #{column.inspect}" do
0
+ columns = [column].flatten.map(&:to_s)
0
+ assert_contains(indices, columns)
0
+ end
0
+ end
0
+ end
0
+
0
+ alias_method :should_have_index, :should_have_indices
0
       
0
       # Ensures that the model cannot be saved if one of the attributes listed is not accepted.
0
       #
...
5
6
7
 
 
 
 
8
9
10
...
5
6
7
8
9
10
11
12
13
14
0
@@ -5,6 +5,10 @@ class CreateUsers < ActiveRecord::Migration
0
       t.column :email, :string
0
       t.column :age, :integer
0
     end
0
+ add_index :users, :email
0
+ add_index :users, :name
0
+ add_index :users, :age
0
+ add_index :users, [:email, :name]
0
   end
0
 
0
   def self.down
...
8
9
10
 
 
 
11
12
13
...
8
9
10
11
12
13
14
15
16
0
@@ -8,6 +8,9 @@ class UserTest < Test::Unit::TestCase
0
 
0
   should_have_one :address
0
 
0
+ should_have_indices :email, :name, [:email, :name]
0
+ should_have_index :age
0
+
0
   should_not_allow_values_for :email, "blah", "b lah"
0
   should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
0
   should_ensure_length_in_range :email, 1..100

Comments

    No one has commented yet.