Skip to content

Commit

Permalink
Fix spec failure in jruby and add support for Rails 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MDaubs authored and Gabe Berke-Williams committed Mar 30, 2012
1 parent 68e65b2 commit d85503f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
16 changes: 7 additions & 9 deletions lib/shoulda/matchers/active_record/query_the_database_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ def or_less
end

def matches?(subject)
ensure_at_least_rails_3_1

@queries = []

subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |name, started, finished, id, payload|
@queries << payload unless filter_query(payload[:name])
@queries << payload unless filter_query(payload)
end

if @method_arguments
Expand Down Expand Up @@ -92,16 +90,16 @@ def friendly_queries
end.join
end

def filter_query(query_name)
query_name == 'SCHEMA'
def filter_query(query)
query[:name] == 'SCHEMA' || looks_like_schema(query[:sql])
end

def ensure_at_least_rails_3_1
raise "Rails 3.1 or greater is required" unless rails_3_1?
def schema_terms
['FROM sqlite_master', 'PRAGMA', 'SHOW TABLES', 'SHOW KEYS FROM', 'SHOW FIELDS FROM']
end

def rails_3_1?
::ActiveRecord::VERSION::MAJOR == 3 && ::ActiveRecord::VERSION::MINOR >= 1
def looks_like_schema(sql)
schema_terms.any? { |term| sql.include?(term) }
end
end
end
Expand Down
71 changes: 31 additions & 40 deletions spec/shoulda/active_record/query_the_database_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,45 @@
require 'spec_helper'

describe Shoulda::Matchers::ActiveRecord::QueryTheDatabaseMatcher do
if ::ActiveRecord::VERSION::MAJOR == 3 && ::ActiveRecord::VERSION::MINOR >= 1
before do
@parent = define_model :litter do
has_many :kittens
end
@child = define_model :kitten, :litter_id => :integer do
belongs_to :litter
end
before do
@parent = define_model :litter do
has_many :kittens
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)
@child = define_model :kitten, :litter_id => :integer do
belongs_to :litter
end
end

it "accepts any number of queries when no number is specified" do
@parent.should query_the_database.when_calling(:count)
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 "rejects any number of queries when no number is specified" do
@parent.should_not query_the_database.when_calling(:to_s)
end
it "accepts any number of queries when no number is specified" do
@parent.should query_the_database.when_calling(:count)
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 any number of queries when no number is specified" do
@parent.should_not query_the_database.when_calling(:to_s)
end

it "rejects the wrong number of queries" do
@parent.should_not query_the_database(10.times).when_calling(:count)
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 "accepts fewer than the specified maximum" do
@parent.should query_the_database(5.times).or_less.when_calling(:count)
end
it "rejects the wrong number of queries" do
@parent.should_not query_the_database(10.times).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
else
it "should raise an exception on Rails < 3.1" do
model = define_model(:litter)
lambda do
model.should query_the_database(1.times).when_calling(:count)
end.should raise_exception(RuntimeError, "Rails 3.1 or greater is required")
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 d85503f

Please sign in to comment.