Skip to content
This repository
Browse code

Fix spec failure in jruby and add support for Rails 3.0

  • Loading branch information...
commit d85503f86ef224bca2b54fc6b9ad96866fe12f2d 1 parent 68e65b2
Matthew Daubert authored March 25, 2012 gabebw committed March 30, 2012
16  lib/shoulda/matchers/active_record/query_the_database_matcher.rb
@@ -43,12 +43,10 @@ def or_less
43 43
         end
44 44
 
45 45
         def matches?(subject)
46  
-          ensure_at_least_rails_3_1
47  
-
48 46
           @queries = []
49 47
 
50 48
           subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |name, started, finished, id, payload|
51  
-            @queries << payload unless filter_query(payload[:name])
  49
+            @queries << payload unless filter_query(payload)
52 50
           end
53 51
 
54 52
           if @method_arguments
@@ -92,16 +90,16 @@ def friendly_queries
92 90
           end.join
93 91
         end
94 92
 
95  
-        def filter_query(query_name)
96  
-          query_name == 'SCHEMA'
  93
+        def filter_query(query)
  94
+          query[:name] == 'SCHEMA' || looks_like_schema(query[:sql])
97 95
         end
98 96
 
99  
-        def ensure_at_least_rails_3_1
100  
-          raise "Rails 3.1 or greater is required" unless rails_3_1?
  97
+        def schema_terms
  98
+          ['FROM sqlite_master', 'PRAGMA', 'SHOW TABLES', 'SHOW KEYS FROM', 'SHOW FIELDS FROM']
101 99
         end
102 100
 
103  
-        def rails_3_1?
104  
-          ::ActiveRecord::VERSION::MAJOR == 3 && ::ActiveRecord::VERSION::MINOR >= 1
  101
+        def looks_like_schema(sql)
  102
+          schema_terms.any? { |term| sql.include?(term) }
105 103
         end
106 104
       end
107 105
     end
71  spec/shoulda/active_record/query_the_database_matcher_spec.rb
... ...
@@ -1,54 +1,45 @@
1 1
 require 'spec_helper'
2 2
 
3 3
 describe Shoulda::Matchers::ActiveRecord::QueryTheDatabaseMatcher do
4  
-  if ::ActiveRecord::VERSION::MAJOR == 3 && ::ActiveRecord::VERSION::MINOR >= 1
5  
-    before do
6  
-      @parent = define_model :litter do
7  
-        has_many :kittens
8  
-      end
9  
-      @child = define_model :kitten, :litter_id => :integer do
10  
-        belongs_to :litter
11  
-      end
  4
+  before do
  5
+    @parent = define_model :litter do
  6
+      has_many :kittens
12 7
     end
13  
-
14  
-    it "accepts the correct number of queries when there is a single query" do
15  
-      @parent.should query_the_database(1.times).when_calling(:count)
  8
+    @child = define_model :kitten, :litter_id => :integer do
  9
+      belongs_to :litter
16 10
     end
  11
+  end
17 12
 
18  
-    it "accepts any number of queries when no number is specified" do
19  
-      @parent.should query_the_database.when_calling(:count)
20  
-    end
  13
+  it "accepts the correct number of queries when there is a single query" do
  14
+    @parent.should query_the_database(1.times).when_calling(:count)
  15
+  end
21 16
 
22  
-    it "rejects any number of queries when no number is specified" do
23  
-      @parent.should_not query_the_database.when_calling(:to_s)
24  
-    end
  17
+  it "accepts any number of queries when no number is specified" do
  18
+    @parent.should query_the_database.when_calling(:count)
  19
+  end
25 20
 
26  
-    it "accepts the correct number of queries when there are two queries" do
27  
-      nonsense = lambda do
28  
-        @parent.create.kittens.create
29  
-      end
30  
-      nonsense.should query_the_database(2.times).when_calling(:call)
31  
-    end
  21
+  it "rejects any number of queries when no number is specified" do
  22
+    @parent.should_not query_the_database.when_calling(:to_s)
  23
+  end
32 24
 
33  
-    it "rejects the wrong number of queries" do
34  
-      @parent.should_not query_the_database(10.times).when_calling(:count)
  25
+  it "accepts the correct number of queries when there are two queries" do
  26
+    nonsense = lambda do
  27
+      @parent.create.kittens.create
35 28
     end
  29
+    nonsense.should query_the_database(2.times).when_calling(:call)
  30
+  end
36 31
 
37  
-    it "accepts fewer than the specified maximum" do
38  
-      @parent.should query_the_database(5.times).or_less.when_calling(:count)
39  
-    end
  32
+  it "rejects the wrong number of queries" do
  33
+    @parent.should_not query_the_database(10.times).when_calling(:count)
  34
+  end
40 35
 
41  
-    it "passes arguments to the method to examine" do
42  
-      model = stub("Model", :count => nil)
43  
-      model.should_not query_the_database.when_calling(:count).with("arguments")
44  
-      model.should have_received(:count).with("arguments")
45  
-    end
46  
-  else
47  
-    it "should raise an exception on Rails < 3.1" do
48  
-      model = define_model(:litter)
49  
-      lambda do
50  
-        model.should query_the_database(1.times).when_calling(:count)
51  
-      end.should raise_exception(RuntimeError, "Rails 3.1 or greater is required")
52  
-    end
  36
+  it "accepts fewer than the specified maximum" do
  37
+    @parent.should query_the_database(5.times).or_less.when_calling(:count)
  38
+  end
  39
+
  40
+  it "passes arguments to the method to examine" do
  41
+    model = stub("Model", :count => nil)
  42
+    model.should_not query_the_database.when_calling(:count).with("arguments")
  43
+    model.should have_received(:count).with("arguments")
53 44
   end
54 45
 end

0 notes on commit d85503f

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