Skip to content
Browse files

first passing tests for have_index matcher

  • Loading branch information...
1 parent ee89e55 commit 114df857bd55aa229792284d501997059b4b7856 @croaky croaky committed with jferris
View
1 lib/shoulda/active_record/matchers.rb
@@ -9,6 +9,7 @@
require 'shoulda/active_record/matchers/only_allow_numeric_values_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_readonly_attribute_matcher'
require 'shoulda/active_record/matchers/protect_attribute_matcher'
require 'shoulda/active_record/matchers/have_named_scope_matcher'
View
32 lib/shoulda/active_record/matchers/association_matcher.rb
@@ -2,6 +2,22 @@ module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
module Matchers
+ def belong_to(name)
+ AssociationMatcher.new(:belongs_to, name)
+ end
+
+ def have_many(name)
+ AssociationMatcher.new(:has_many, name)
+ end
+
+ def have_one(name)
+ AssociationMatcher.new(:has_one, name)
+ end
+
+ def have_and_belong_to_many(name)
+ AssociationMatcher.new(:has_and_belongs_to_many, name)
+ end
+
class AssociationMatcher # :nodoc:
def initialize(macro, name)
@macro = macro
@@ -171,22 +187,6 @@ def macro_description
end
end
- def belong_to(name)
- AssociationMatcher.new(:belongs_to, name)
- end
-
- def have_many(name)
- AssociationMatcher.new(:has_many, name)
- end
-
- def have_one(name)
- AssociationMatcher.new(:has_one, name)
- end
-
- def have_and_belong_to_many(name)
- AssociationMatcher.new(:has_and_belongs_to_many, name)
- end
-
end
end
end
View
4 lib/shoulda/active_record/matchers/have_db_column_matcher.rb
@@ -21,8 +21,8 @@ def have_db_column(column)
class HaveDbColumnMatcher # :nodoc:
def initialize(macro, column)
- @macro = macro
- @column = column
+ @macro = macro
+ @column = column
end
def of_type(column_type)
View
65 lib/shoulda/active_record/matchers/have_index_matcher.rb
@@ -0,0 +1,65 @@
+module Shoulda # :nodoc:
+ module ActiveRecord # :nodoc:
+ module Matchers
+
+ # Ensures the database column has specified index.
+ #
+ # Options:
+ # * <tt>unique</tt> -
+ #
+ # Example:
+ # it { should have_index(:ssn).unique(true) }
+ #
+ def have_index(index)
+ HaveIndexMatcher.new(:have_index, index)
+ end
+
+ class HaveIndexMatcher # :nodoc:
+ def initialize(macro, index)
+ @macro = macro
+ @index = index
+ end
+
+ def matches?(subject)
+ @subject = subject
+ index_exists?
+ end
+
+ def failure_message
+ "Expected #{expectation} (#{@missing})"
+ end
+
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
+
+ def description
+ "have index named #{@column}"
+ end
+
+ protected
+
+ def index_exists?
+ ! matched_index.nil?
+ end
+
+ def matched_index
+ indexes.detect { |each| each.columns.include?(@index.to_s) }
+ end
+
+ def model_class
+ @subject.class
+ end
+
+ def indexes
+ ::ActiveRecord::Base.connection.indexes(model_class.table_name)
+ end
+
+ def expectation
+ expected = "#{model_class.name} to #{description}"
+ end
+ end
+
+ end
+ end
+end
View
25 test/matchers/have_index_matcher_test.rb
@@ -0,0 +1,25 @@
+require File.join(File.dirname(__FILE__), '..', 'test_helper')
+
+class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
+
+ context "have_index" do
+ setup do
+ @matcher = have_index(:age)
+ end
+
+ should "accept an existing index" do
+ db_connection = create_table 'superheros' do |table|
+ table.integer :age
+ end
+ db_connection.add_index :superheros, :age
+ define_model_class 'Superhero'
+ assert_accepts @matcher, Superhero.new
+ end
+
+ should "reject a nonexistent index" do
+ define_model :superhero
+ assert_rejects @matcher, Superhero.new
+ end
+ end
+
+end
View
1 test/model_builder.rb
@@ -7,6 +7,7 @@ def create_table(table_name, &block)
connection.create_table(table_name, &block)
@created_tables ||= []
@created_tables << table_name
+ connection
rescue Exception => e
connection.execute("DROP TABLE IF EXISTS #{table_name}")
raise e

0 comments on commit 114df85

Please sign in to comment.
Something went wrong with that request. Please try again.