Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix spec failure in jruby and add support for Rails 3.0

  • Loading branch information...
commit d85503f86ef224bca2b54fc6b9ad96866fe12f2d 1 parent 68e65b2
@MDaubs MDaubs authored gabebw committed
View
16 lib/shoulda/matchers/active_record/query_the_database_matcher.rb
@@ -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
@@ -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
View
71 spec/shoulda/active_record/query_the_database_matcher_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.