Fix spec failure in jruby and add support for Rails 3.0
This commit is contained in:
parent
68e65b2aba
commit
d85503f86e
|
@ -43,12 +43,10 @@ module Shoulda # :nodoc:
|
|||
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
|
||||
|
@ -92,16 +90,16 @@ module Shoulda # :nodoc:
|
|||
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
|
||||
|
|
|
@ -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)
|
||||
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
|
||||
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")
|
||||
@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
|
||||
|
|
Loading…
Reference in New Issue