Renamed have_index to have_db_index [#153 state:resolved]
This commit is contained in:
parent
62078846bb
commit
712a02615a
|
@ -448,7 +448,7 @@ module Shoulda # :nodoc:
|
|||
end
|
||||
|
||||
# Ensure that the given columns are defined on the models backing SQL table.
|
||||
# Also aliased to should_have_index for readability.
|
||||
# Also aliased to should_have_db_column for readability.
|
||||
# Takes the same options available in migrations:
|
||||
# :type, :precision, :limit, :default, :null, and :scale
|
||||
#
|
||||
|
@ -480,7 +480,7 @@ module Shoulda # :nodoc:
|
|||
alias_method :should_have_db_column, :should_have_db_columns
|
||||
|
||||
# Ensures that there are DB indices on the given columns or tuples of columns.
|
||||
# Also aliased to should_have_index for readability
|
||||
# Also aliased to should_have_db_index for readability
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:unique</tt> - whether or not the index has a unique
|
||||
|
@ -491,23 +491,37 @@ module Shoulda # :nodoc:
|
|||
#
|
||||
# Examples:
|
||||
#
|
||||
# should_have_indices :email, :name, [:commentable_type, :commentable_id]
|
||||
# should_have_index :age
|
||||
# should_have_index :ssn, :unique => true
|
||||
# should_have_db_indices :email, :name, [:commentable_type, :commentable_id]
|
||||
# should_have_db_index :age
|
||||
# should_have_db_index :ssn, :unique => true
|
||||
#
|
||||
def should_have_indices(*columns)
|
||||
def should_have_db_indices(*columns)
|
||||
unique = get_options!(columns, :unique)
|
||||
klass = model_class
|
||||
|
||||
columns.each do |column|
|
||||
matcher = have_index(column).unique(unique)
|
||||
matcher = have_db_index(column).unique(unique)
|
||||
should matcher.description do
|
||||
assert_accepts(matcher, klass.new)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
alias_method :should_have_index, :should_have_indices
|
||||
alias_method :should_have_db_index, :should_have_db_indices
|
||||
|
||||
# Deprecated. See should_have_db_index
|
||||
def should_have_index(*args)
|
||||
warn "[DEPRECATION] should_have_index is deprecated. " <<
|
||||
"Use should_have_db_index instead."
|
||||
should_have_db_index(*args)
|
||||
end
|
||||
|
||||
# Deprecated. See should_have_db_indices
|
||||
def should_have_indices(*args)
|
||||
warn "[DEPRECATION] should_have_indices is deprecated. " <<
|
||||
"Use should_have_db_indices instead."
|
||||
should_have_db_indices(*args)
|
||||
end
|
||||
|
||||
# Ensures that the model cannot be saved if one of the attributes listed is not accepted.
|
||||
#
|
||||
|
|
|
@ -9,7 +9,7 @@ require 'shoulda/active_record/matchers/validate_acceptance_of_matcher'
|
|||
require 'shoulda/active_record/matchers/validate_numericality_of_matcher'
|
||||
require 'shoulda/active_record/matchers/association_matcher'
|
||||
require 'shoulda/active_record/matchers/have_db_column_matcher'
|
||||
require 'shoulda/active_record/matchers/have_index_matcher'
|
||||
require 'shoulda/active_record/matchers/have_db_index_matcher'
|
||||
require 'shoulda/active_record/matchers/have_readonly_attribute_matcher'
|
||||
require 'shoulda/active_record/matchers/allow_mass_assignment_of_matcher'
|
||||
require 'shoulda/active_record/matchers/have_named_scope_matcher'
|
||||
|
|
|
@ -14,15 +14,15 @@ module Shoulda # :nodoc:
|
|||
#
|
||||
# Examples:
|
||||
#
|
||||
# it { should have_index(:age) }
|
||||
# it { should have_index([:commentable_type, :commentable_id]) }
|
||||
# it { should have_index(:ssn).unique(true) }
|
||||
# it { should have_db_index(:age) }
|
||||
# it { should have_db_index([:commentable_type, :commentable_id]) }
|
||||
# it { should have_db_index(:ssn).unique(true) }
|
||||
#
|
||||
def have_index(columns)
|
||||
HaveIndexMatcher.new(:have_index, columns)
|
||||
def have_db_index(columns)
|
||||
HaveDbIndexMatcher.new(:have_index, columns)
|
||||
end
|
||||
|
||||
class HaveIndexMatcher # :nodoc:
|
||||
class HaveDbIndexMatcher # :nodoc:
|
||||
def initialize(macro, columns)
|
||||
@macro = macro
|
||||
@columns = normalize_columns_to_array(columns)
|
|
@ -1,10 +1,10 @@
|
|||
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
|
||||
|
||||
class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
|
||||
class HaveDbIndexMatcherTest < Test::Unit::TestCase # :nodoc:
|
||||
|
||||
context "have_index" do
|
||||
context "have_db_index" do
|
||||
setup do
|
||||
@matcher = have_index(:age)
|
||||
@matcher = have_db_index(:age)
|
||||
end
|
||||
|
||||
should "accept an existing index" do
|
||||
|
@ -22,9 +22,9 @@ class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
context "have_index with unique option" do
|
||||
context "have_db_index with unique option" do
|
||||
setup do
|
||||
@matcher = have_index(:ssn).unique(true)
|
||||
@matcher = have_db_index(:ssn).unique(true)
|
||||
end
|
||||
|
||||
should "accept an index of correct unique" do
|
||||
|
@ -46,9 +46,9 @@ class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
context "have_index on multiple columns" do
|
||||
context "have_db_index on multiple columns" do
|
||||
setup do
|
||||
@matcher = have_index([:geocodable_type, :geocodable_id])
|
||||
@matcher = have_db_index([:geocodable_type, :geocodable_id])
|
||||
end
|
||||
|
||||
should "accept an existing index" do
|
|
@ -12,15 +12,15 @@ class UserTest < Test::Unit::TestCase
|
|||
should_have_one :address
|
||||
should_have_one :address, :dependent => :destroy
|
||||
|
||||
should_have_indices :email, :name
|
||||
should_have_db_indices :email, :name
|
||||
should_have_index :age
|
||||
should_have_index [:email, :name], :unique => true
|
||||
should_have_index :age, :unique => false
|
||||
should_have_db_index [:email, :name], :unique => true
|
||||
should_have_db_index :age, :unique => false
|
||||
|
||||
should_fail do
|
||||
should_have_index :phone
|
||||
should_have_index :email, :unique => false
|
||||
should_have_index :age, :unique => true
|
||||
should_have_db_index :phone
|
||||
should_have_db_index :email, :unique => false
|
||||
should_have_db_index :age, :unique => true
|
||||
end
|
||||
|
||||
should_have_named_scope :old, :conditions => "age > 50"
|
||||
|
|
Loading…
Reference in New Issue