Permalink
Browse files

Remove 'query_the_database' matcher

* See issue #252
  • Loading branch information...
1 parent 5324653 commit c4413a4acab9376a20f85b62b4926a0186cb2983 @mxie mxie committed with mxie Mar 15, 2013
@@ -3,7 +3,6 @@
require 'shoulda/matchers/active_record/have_db_index_matcher'
require 'shoulda/matchers/active_record/have_readonly_attribute_matcher'
require 'shoulda/matchers/active_record/serialize_matcher'
-require 'shoulda/matchers/active_record/query_the_database_matcher'
require 'shoulda/matchers/active_record/accept_nested_attributes_for_matcher'
module Shoulda
@@ -1,111 +0,0 @@
-require 'active_support/deprecation'
-
-module Shoulda # :nodoc:
- module Matchers
- module ActiveRecord # :nodoc:
-
- # Ensures that the number of database queries is known. Rails 3.1 or greater is required.
- #
- # Options:
- # * <tt>when_calling</tt> - Required, the name of the method to examine.
- # * <tt>with</tt> - Used in conjunction with <tt>when_calling</tt> to pass parameters to the method to examine.
- # * <tt>or_less</tt> - Pass if the database is queried no more than the number of times specified, as opposed to exactly that number of times.
- #
- # Examples:
- # it { should query_the_database(4.times).when_calling(:complicated_counting_method)
- # it { should query_the_database(4.times).or_less.when_calling(:generate_big_report)
- # it { should_not query_the_database.when_calling(:cached_count)
- #
- def query_the_database(times = nil)
- QueryTheDatabaseMatcher.new(times)
- end
-
- class QueryTheDatabaseMatcher # :nodoc:
- def initialize(times)
- ActiveSupport::Deprecation.warn 'The query_the_database matcher is deprecated and will be removed in 2.0'
- @queries = []
- @options = {}
-
- if times.respond_to?(:count)
- @options[:expected_query_count] = times.count
- else
- @options[:expected_query_count] = times
- end
- end
-
- def when_calling(method_name)
- @options[:method_name] = method_name
- self
- end
-
- def with(*method_arguments)
- @options[:method_arguments] = method_arguments
- self
- end
-
- def or_less
- @options[:expected_count_is_maximum] = true
- self
- end
-
- def matches?(subject)
- subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |name, started, finished, id, payload|
- @queries << payload unless filter_query(payload)
- end
-
- if @options[:method_arguments]
- subject.send(@options[:method_name], *@options[:method_arguments])
- else
- subject.send(@options[:method_name])
- end
-
- ActiveSupport::Notifications.unsubscribe(subscriber)
-
- if @options[:expected_count_is_maximum]
- @queries.length <= @options[:expected_query_count]
- elsif @options[:expected_query_count].present?
- @queries.length == @options[:expected_query_count]
- else
- @queries.length > 0
- end
- end
-
- def failure_message_for_should
- if @options.key?(:expected_query_count)
- "Expected ##{@options[:method_name]} to cause #{@options[:expected_query_count]} database queries but it actually caused #{@queries.length} queries:" + friendly_queries
- else
- "Expected ##{@options[:method_name]} to query the database but it actually caused #{@queries.length} queries:" + friendly_queries
- end
- end
-
- def failure_message_for_should_not
- if @options[:expected_query_count]
- "Expected ##{@options[:method_name]} to not cause #{@options[:expected_query_count]} database queries but it actually caused #{@queries.length} queries:" + friendly_queries
- else
- "Expected ##{@options[:method_name]} to not query the database but it actually caused #{@queries.length} queries:" + friendly_queries
- end
- end
-
- private
-
- def friendly_queries
- @queries.map do |query|
- "\n (#{query[:name]}) #{query[:sql]}"
- end.join
- end
-
- def filter_query(query)
- query[:name] == 'SCHEMA' || looks_like_schema?(query[:sql])
- end
-
- def schema_terms
- ['FROM sqlite_master', 'PRAGMA', 'SHOW TABLES', 'SHOW KEYS FROM', 'SHOW FIELDS FROM', 'begin transaction', 'commit transaction']
- end
-
- def looks_like_schema?(sql)
- schema_terms.any? { |term| sql.include?(term) }
- end
- end
- end
- end
-end
@@ -1,45 +0,0 @@
-require 'spec_helper'
-
-describe Shoulda::Matchers::ActiveRecord::QueryTheDatabaseMatcher do
- before do
- @parent = define_model :litter do
- has_many :kittens
- end
- @child = define_model :kitten, :litter_id => :integer do
- belongs_to :litter
- end
- end
-
- it 'accepts the correct number of queries when there is a single query' do
- @parent.should query_the_database(1.times).when_calling(:count)
- end
-
- it 'accepts any number of queries when no number is specified' do
- @parent.should query_the_database.when_calling(:count)
- end
-
- it 'rejects any number of queries when no number is specified' do
- @parent.should_not query_the_database.when_calling(:to_s)
- end
-
- it 'accepts the correct number of queries when there are two queries' do
- nonsense = lambda do
- @parent.create.kittens.create
- end
- nonsense.should query_the_database(2.times).when_calling(:call)
- end
-
- it 'rejects the wrong number of queries' do
- @parent.should_not query_the_database(10.times).when_calling(:count)
- end
-
- it 'accepts fewer than the specified maximum' do
- @parent.should query_the_database(5.times).or_less.when_calling(:count)
- end
-
- it 'passes arguments to the method to examine' do
- model = stub('Model', :count => nil)
- model.should_not query_the_database.when_calling(:count).with('arguments')
- model.should have_received(:count).with('arguments')
- end
-end

0 comments on commit c4413a4

Please sign in to comment.