benhoskings-ambitious-activerecord 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/test/benchmark.rb ADDED
@@ -0,0 +1,69 @@
1
+ unless defined? Test
2
+ $:.unshift File.dirname(__FILE__) + '/../../../lib'
3
+ %w( rubygems ambition/adapters/active_record benchmark ).each { |f| require f }
4
+
5
+ class User < ActiveRecord::Base
6
+ def self.reflections
7
+ return @reflections if @reflections
8
+ @reflections = {}
9
+ @reflections[:ideas] = Reflection.new(:has_many, 'user_id', :ideas, 'ideas')
10
+ @reflections[:invites] = Reflection.new(:has_many, 'referrer_id', :invites, 'invites')
11
+ @reflections[:profile] = Reflection.new(:has_one, 'user_id', :profile, 'profiles')
12
+ @reflections[:account] = Reflection.new(:belongs_to, 'account_id', :account, 'accounts')
13
+ @reflections
14
+ end
15
+
16
+ def self.table_name
17
+ 'users'
18
+ end
19
+ end
20
+
21
+ class Reflection < Struct.new(:macro, :primary_key_name, :name, :table_name)
22
+ end
23
+
24
+ Times = 10000
25
+
26
+ Benchmark.bm(30) do |x|
27
+ x.report 'simple select' do
28
+ Times.times do
29
+ User.select { |u| u.id == 20 }.to_hash
30
+ end
31
+ end
32
+
33
+ x.report 'simple select w/ eval' do
34
+ Times.times do
35
+ User.select { |u| u.created_at == Time.now }.to_hash
36
+ end
37
+ end
38
+
39
+ x.report 'dual select' do
40
+ Times.times do
41
+ User.select { |u| u.id == 20 && u.age > 20 }.to_hash
42
+ end
43
+ end
44
+
45
+ x.report 'join select' do
46
+ Times.times do
47
+ User.select { |u| u.id == 20 && u.ideas.name =~ /stuff/ }.to_hash
48
+ end
49
+ end
50
+
51
+ x.report 'dual select w/ sort' do
52
+ Times.times do
53
+ User.select { |u| u.id == 20 && u.age > 20 }.sort_by { |u| u.id }.to_hash
54
+ end
55
+ end
56
+
57
+ x.report 'dual select w/ sort & first' do
58
+ Times.times do
59
+ User.select { |u| u.id == 20 && u.age > 20 }.sort_by { |u| u.id }.first(20).to_hash
60
+ end
61
+ end
62
+
63
+ x.report "it's complicated" do
64
+ Times.times do
65
+ User.select { |u| (u.id == 20 && u.age > 20) || u.profile.name == 'Jon' }.sort_by { |u| [u.id, -u.name] }.first(20).to_hash
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "ActiveRecord Adapter" do
4
+ context "Chaining" do
5
+ specify "should join selects with AND" do
6
+ sql = User.select { |m| m.name == 'jon' }
7
+ sql = sql.select { |m| m.age == 22 }
8
+ sql.to_s.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22"
9
+ end
10
+
11
+ specify "should join sort_bys with a comma" do
12
+ sql = User.select { |m| m.name == 'jon' }
13
+ sql = sql.sort_by { |m| m.name }
14
+ sql = sql.sort_by { |m| m.age }
15
+ sql.to_s.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
16
+ end
17
+
18
+ specify "should join selects and sorts intelligently" do
19
+ sql = User.select { |m| m.name == 'jon' }
20
+ sql = sql.select { |m| m.age == 22 }
21
+ sql = sql.sort_by { |m| -m.name }
22
+ sql = sql.sort_by { |m| m.age }
23
+ sql.to_s.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22 ORDER BY users.name DESC, users.age"
24
+ end
25
+
26
+ specify "should join lots of selects and sorts intelligently" do
27
+ sql = User.select { |m| m.name == 'jon' }
28
+ sql = sql.select { |m| m.age == 22 }
29
+ sql = sql.sort_by { |m| m.name }
30
+ sql = sql.select { |m| m.power == true }
31
+ sql = sql.sort_by { |m| m.email }
32
+ sql = sql.select { |m| m.admin == true && m.email == 'chris@ozmm.org' }
33
+ sql.to_s.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22 AND users.power = 1 AND (users.admin = 1 AND users.email = 'chris@ozmm.org') ORDER BY users.name, users.email"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "ActiveRecord Adapter" do
4
+ context "Count" do
5
+ setup do
6
+ hash = { :conditions => "users.name = 'jon'" }
7
+ User.expects(:count).with(hash)
8
+ @sql = User.select { |m| m.name == 'jon' }
9
+ end
10
+
11
+ specify "size" do
12
+ @sql.size
13
+ end
14
+
15
+ specify "length" do
16
+ @sql.length
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "ActiveRecord Adapter" do
4
+ context "Each" do
5
+ specify "simple ==" do
6
+ hash = { :conditions => "users.age = 21" }
7
+ User.expects(:find).with(:all, hash).returns([])
8
+ User.select { |m| m.age == 21 }.each do |user|
9
+ puts user.name
10
+ end
11
+ end
12
+
13
+ specify "limit and conditions" do
14
+ hash = { :limit => 5, :conditions => "users.age = 21" }
15
+ User.expects(:find).with(:all, hash).returns([])
16
+ User.select { |m| m.age == 21 }.first(5).each do |user|
17
+ puts user.name
18
+ end
19
+ end
20
+
21
+ specify "limit and conditions and order" do
22
+ hash = { :limit => 5, :conditions => "users.age = 21", :order => 'users.name' }
23
+ User.expects(:find).with(:all, hash).returns([])
24
+ User.select { |m| m.age == 21 }.sort_by { |m| m.name }.first(5).each do |user|
25
+ puts user.name
26
+ end
27
+ end
28
+
29
+ specify "limit and order" do
30
+ hash = { :limit => 5, :order => 'users.name' }
31
+ User.expects(:find).with(:all, hash).returns([])
32
+ User.sort_by { |m| m.name }.first(5).each do |user|
33
+ puts user.name
34
+ end
35
+ end
36
+ end
37
+
38
+ context "Enumerable Methods" do
39
+ specify "map" do
40
+ hash = { :conditions => "users.age = 21" }
41
+ User.expects(:find).with(:all, hash).returns([])
42
+ User.select { |m| m.age == 21 }.map { |u| u.name }
43
+ end
44
+
45
+ specify "each_with_index" do
46
+ hash = { :conditions => "users.age = 21" }
47
+ User.expects(:find).with(:all, hash).returns([])
48
+ User.select { |m| m.age == 21 }.each_with_index do |user, i|
49
+ puts "#{i}: #{user.name}"
50
+ end
51
+ end
52
+
53
+ specify "any?" do
54
+ User.expects(:count).with(:conditions => "users.age > 21").returns(1)
55
+ User.any? { |u| u.age > 21 }.should == true
56
+ end
57
+
58
+ specify "all?" do
59
+ User.expects(:count).at_least_once.returns(10, 20)
60
+ User.all? { |u| u.age > 21 }.should == false
61
+
62
+ User.expects(:count).at_least_once.returns(10, 10)
63
+ User.all? { |u| u.age > 21 }.should == true
64
+ end
65
+
66
+ specify "empty?" do
67
+ User.expects(:count).with(:conditions => "users.age > 21").returns(1)
68
+ User.select { |u| u.age > 21 }.empty?.should.equal false
69
+
70
+ User.expects(:count).with(:conditions => "users.age > 21").returns(0)
71
+ User.select { |u| u.age > 21 }.empty?.should.equal true
72
+ end
73
+
74
+ specify "entries" do
75
+ User.expects(:find).with(:all, {})
76
+ User.entries
77
+
78
+ hash = { :conditions => "users.age = 21" }
79
+ User.expects(:find).with(:all, hash).returns([])
80
+ User.select { |m| m.age == 21 }.entries
81
+ end
82
+
83
+ specify "to_a" do
84
+ User.expects(:find).with(:all, {})
85
+ User.to_a
86
+ end
87
+ end
88
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,47 @@
1
+ %w( rubygems test/spec mocha redgreen English ).each { |f| require f }
2
+
3
+ $LOAD_PATH.unshift *[ File.dirname(__FILE__) + '/../lib', File.dirname(__FILE__) + '/../../../lib' ]
4
+ require 'ambition/adapters/active_record'
5
+
6
+ class User < ActiveRecord::Base
7
+ def self.reflections
8
+ return @reflections if @reflections
9
+ @reflections = {}
10
+ @reflections[:ideas] = Reflection.new(:has_many, 'user_id', :ideas, 'ideas')
11
+ @reflections[:invites] = Reflection.new(:has_many, 'referrer_id', :invites, 'invites')
12
+ @reflections[:profile] = Reflection.new(:has_one, 'user_id', :profile, 'profiles')
13
+ @reflections[:account] = Reflection.new(:belongs_to, 'account_id', :account, 'accounts')
14
+ @reflections
15
+ end
16
+
17
+ def self.table_name
18
+ 'users'
19
+ end
20
+ end
21
+
22
+ class Reflection < Struct.new(:macro, :primary_key_name, :name, :table_name)
23
+ end
24
+
25
+ module ActiveRecord
26
+ module ConnectionAdapters
27
+ class MysqlAdapter
28
+ def initialize(*args)
29
+ super
30
+ end
31
+
32
+ def connect(*args)
33
+ true
34
+ end
35
+ end
36
+
37
+ class PostgreSQLAdapter
38
+ def connect(*args)
39
+ true
40
+ end
41
+ class PGError; end
42
+ end
43
+
44
+ class FakeAdapter < AbstractAdapter
45
+ end
46
+ end
47
+ end
data/test/join_test.rb ADDED
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "ActiveRecord Adapter" do
4
+ context "Joins" do
5
+ specify "simple == on an association" do
6
+ sql = User.select { |m| m.account.email == 'chris@ozmm.org' }
7
+ sql.to_hash.should == {
8
+ :conditions => "accounts.email = 'chris@ozmm.org'",
9
+ :include => [:account]
10
+ }
11
+ end
12
+
13
+ specify "simple mixed == on an association" do
14
+ sql = User.select { |m| m.name == 'chris' && m.account.email == 'chris@ozmm.org' }
15
+ sql.to_hash.should == {
16
+ :conditions => "(users.name = 'chris' AND accounts.email = 'chris@ozmm.org')",
17
+ :include => [:account]
18
+ }
19
+ end
20
+
21
+ specify "multiple associations" do
22
+ sql = User.select { |m| m.ideas.title == 'New Freezer' || m.invites.email == 'pj@hyett.com' }
23
+ sql.to_hash.should == {
24
+ :conditions => "(ideas.title = 'New Freezer' OR invites.email = 'pj@hyett.com')",
25
+ :include => [:ideas, :invites]
26
+ }
27
+ end
28
+
29
+ specify "belongs_to" do
30
+ sql = User.select { |m| m.account.id > 20 }
31
+ sql.to_hash.should == {
32
+ :conditions => "accounts.id > 20",
33
+ :include => [:account]
34
+ }
35
+ end
36
+
37
+ specify "complex joins have no to_s" do
38
+ sql = User.select { |m| m.account.id > 20 }
39
+ should.raise { sql.to_s }
40
+ end
41
+
42
+ specify "non-existant associations" do
43
+ should.raise { User.select { |m| m.liquor.brand == 'Jack' } }
44
+ end
45
+
46
+ specify "in order" do
47
+ sql = User.sort_by { |m| m.ideas.title }
48
+ sql.to_hash.should == {
49
+ :order => "ideas.title",
50
+ :include => [:ideas]
51
+ }
52
+ end
53
+
54
+ specify "in a more complex order" do
55
+ sql = User.sort_by { |m| [ m.ideas.title, -m.invites.email ] }
56
+ sql.to_hash.should == {
57
+ :order => "ideas.title, invites.email DESC",
58
+ :include => [:ideas, :invites]
59
+ }
60
+ end
61
+ end
62
+ end
data/test/profiler.rb ADDED
@@ -0,0 +1,36 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../../../lib'
2
+ require 'rubygems'
3
+ require 'ambition'
4
+ require 'ambition/adapters/active_record'
5
+ require 'ruby-prof'
6
+
7
+ class User < ActiveRecord::Base
8
+ def self.reflections
9
+ return @reflections if @reflections
10
+ @reflections = {}
11
+ @reflections[:ideas] = Reflection.new(:has_many, 'user_id', :ideas, 'ideas')
12
+ @reflections[:invites] = Reflection.new(:has_many, 'referrer_id', :invites, 'invites')
13
+ @reflections[:profile] = Reflection.new(:has_one, 'user_id', :profile, 'profiles')
14
+ @reflections[:account] = Reflection.new(:belongs_to, 'account_id', :account, 'accounts')
15
+ @reflections
16
+ end
17
+
18
+ def self.table_name
19
+ 'users'
20
+ end
21
+ end
22
+
23
+ class Reflection < Struct.new(:macro, :primary_key_name, :name, :table_name)
24
+ end
25
+
26
+ result = RubyProf.profile do
27
+ 1000.times do
28
+ User.select { |u| (u.id == 20 && u.age > 20) || u.profile.name == 'Jon' }.sort_by { |u| [u.id, -u.name] }.first(20).to_hash
29
+ end
30
+ end
31
+
32
+ printer = RubyProf::FlatPrinter.new(result)
33
+ #printer = RubyProf::GraphPrinter.new(result)
34
+ printer.print(STDOUT, 0)
35
+
36
+ puts User.select { |u| (u.id == 20 && u.age > 20) || u.profile.name == 'Jon' }.sort_by { |u| [u.id, -u.name] }.first(20).to_hash.inspect
data/test/ruby_test.rb ADDED
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "ActiveRecord Adapter" do
4
+ context "Inline Ruby" do
5
+ xspecify "should know what to return" do
6
+ name = 'David'
7
+ sql = User.select { |u| name.nil? || u.name == name }.to_s
8
+ sql.should == "SELECT * FROM users WHERE (users.name = 'David')"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,259 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "ActiveRecord Adapter" do
4
+ context "Select" do
5
+ specify "simple ==" do
6
+ sql = User.select { |m| m.name == 'jon' }.to_s
7
+ sql.should == "SELECT * FROM users WHERE users.name = 'jon'"
8
+ end
9
+
10
+ specify "simple !=" do
11
+ sql = User.select { |m| m.name != 'jon' }.to_s
12
+ sql.should == "SELECT * FROM users WHERE users.name <> 'jon'"
13
+ end
14
+
15
+ specify "simple == && ==" do
16
+ sql = User.select { |m| m.name == 'jon' && m.age == 21 }.to_s
17
+ sql.should == "SELECT * FROM users WHERE (users.name = 'jon' AND users.age = 21)"
18
+ end
19
+
20
+ specify "simple == || ==" do
21
+ sql = User.select { |m| m.name == 'jon' || m.age == 21 }.to_s
22
+ sql.should == "SELECT * FROM users WHERE (users.name = 'jon' OR users.age = 21)"
23
+ end
24
+
25
+ specify "mixed && and ||" do
26
+ sql = User.select { |m| m.name == 'jon' || m.age == 21 && m.password == 'pass' }.to_s
27
+ sql.should == "SELECT * FROM users WHERE (users.name = 'jon' OR (users.age = 21 AND users.password = 'pass'))"
28
+ end
29
+
30
+ specify "grouped && and ||" do
31
+ sql = User.select { |m| (m.name == 'jon' || m.name == 'rick') && m.age == 21 }.to_s
32
+ sql.should == "SELECT * FROM users WHERE ((users.name = 'jon' OR users.name = 'rick') AND users.age = 21)"
33
+ end
34
+
35
+ specify "simple >/<" do
36
+ sql = User.select { |m| m.age > 21 }.to_s
37
+ sql.should == "SELECT * FROM users WHERE users.age > 21"
38
+
39
+ sql = User.select { |m| m.age >= 21 }.to_s
40
+ sql.should == "SELECT * FROM users WHERE users.age >= 21"
41
+
42
+ sql = User.select { |m| m.age < 21 }.to_s
43
+ sql.should == "SELECT * FROM users WHERE users.age < 21"
44
+ end
45
+
46
+ specify "array.include? item" do
47
+ sql = User.select { |m| [1, 2, 3, 4].include? m.id }.to_s
48
+ sql.should == "SELECT * FROM users WHERE users.id IN (1, 2, 3, 4)"
49
+ end
50
+
51
+ specify "variable'd array.include? item" do
52
+ array = [1, 2, 3, 4]
53
+ sql = User.select { |m| array.include? m.id }.to_s
54
+ sql.should == "SELECT * FROM users WHERE users.id IN (1, 2, 3, 4)"
55
+ end
56
+
57
+ specify "simple == with variables" do
58
+ me = 'chris'
59
+ sql = User.select { |m| m.name == me }.to_s
60
+ sql.should == "SELECT * FROM users WHERE users.name = '#{me}'"
61
+ end
62
+
63
+ specify "simple == with method arguments" do
64
+ def test_it(name)
65
+ sql = User.select { |m| m.name == name }.to_s
66
+ sql.should == "SELECT * FROM users WHERE users.name = '#{name}'"
67
+ end
68
+
69
+ test_it('chris')
70
+ end
71
+
72
+ specify "simple == with instance variables" do
73
+ @me = 'chris'
74
+ sql = User.select { |m| m.name == @me }.to_s
75
+ sql.should == "SELECT * FROM users WHERE users.name = '#{@me}'"
76
+ end
77
+
78
+ specify "simple == with instance variable method call" do
79
+ require 'ostruct'
80
+ @person = OpenStruct.new(:name => 'chris')
81
+
82
+ sql = User.select { |m| m.name == @person.name }.to_s
83
+ sql.should == "SELECT * FROM users WHERE users.name = '#{@person.name}'"
84
+ end
85
+
86
+ specify "simple == with global variables" do
87
+ $my_name = 'boston'
88
+ sql = User.select { |m| m.name == $my_name }.to_s
89
+ sql.should == "SELECT * FROM users WHERE users.name = '#{$my_name}'"
90
+ end
91
+
92
+ specify "simple == with method call" do
93
+ def band
94
+ 'megadeth'
95
+ end
96
+
97
+ sql = User.select { |m| m.name == band }.to_s
98
+ sql.should == "SELECT * FROM users WHERE users.name = '#{band}'"
99
+ end
100
+
101
+ specify "simple =~ with string" do
102
+ sql = User.select { |m| m.name =~ 'chris' }.to_s
103
+ sql.should == "SELECT * FROM users WHERE users.name LIKE 'chris'"
104
+
105
+ sql = User.select { |m| m.name =~ 'chri%' }.to_s
106
+ sql.should == "SELECT * FROM users WHERE users.name LIKE 'chri%'"
107
+ end
108
+
109
+ specify "simple !~ with string" do
110
+ sql = User.select { |m| m.name !~ 'chris' }.to_s
111
+ sql.should == "SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
112
+
113
+ sql = User.select { |m| !(m.name =~ 'chris') }.to_s
114
+ sql.should == "SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
115
+ end
116
+
117
+ specify "simple =~ with regexp" do
118
+ sql = User.select { |m| m.name =~ /chris/ }.to_s
119
+ sql.should == "SELECT * FROM users WHERE users.name REGEXP 'chris'"
120
+ end
121
+
122
+ specify "simple =~ with regexp flags" do
123
+ sql = User.select { |m| m.name =~ /chris/i }.to_s
124
+ sql.should == "SELECT * FROM users WHERE users.name REGEXP 'chris'"
125
+ end
126
+
127
+ specify "simple LOWER()" do
128
+ sql = User.select { |m| m.name.downcase =~ 'chris%' }.to_s
129
+ sql.should == "SELECT * FROM users WHERE LOWER(users.name) LIKE 'chris%'"
130
+ end
131
+
132
+ specify "simple UPPER()" do
133
+ sql = User.select { |m| m.name.upcase =~ 'chris%' }.to_s
134
+ sql.should == "SELECT * FROM users WHERE UPPER(users.name) LIKE 'chris%'"
135
+ end
136
+
137
+ specify "undefined equality symbol" do
138
+ should.raise { User.select { |m| m.name =* /chris/ }.to_s }
139
+ end
140
+
141
+ specify "block variable / assigning variable conflict" do
142
+ m = User.select { |m| m.name == 'chris' }.to_s
143
+ m.should == "SELECT * FROM users WHERE users.name = 'chris'"
144
+ end
145
+
146
+ specify "simple == with inline ruby" do
147
+ sql = User.select { |m| m.created_at == 2.days.ago.to_s(:db) }.to_s
148
+ sql.should == "SELECT * FROM users WHERE users.created_at = '#{2.days.ago.to_s(:db)}'"
149
+ end
150
+
151
+ specify "deep chains" do
152
+ should.raise do
153
+ User.select { |m| m.created_at.something.else.perhaps }.to_s
154
+ end
155
+ end
156
+
157
+ specify "inspect" do
158
+ User.select { |u| u.name }.inspect.should.match %r(call #to_s or #to_hash)
159
+ end
160
+ end
161
+
162
+ context "Detect" do
163
+ specify "simple ==" do
164
+ User.expects(:select).returns(mock(:first => true))
165
+ User.detect { |m| m.name == 'chris' }
166
+ end
167
+
168
+ specify "nothing found" do
169
+ User.expects(:select).returns(mock(:first => nil))
170
+ User.detect { |m| m.name == 'chris' }.should.be.nil
171
+ end
172
+ end
173
+
174
+ xcontext "[]" do
175
+ specify "finds a single row" do
176
+ User.expects(:find).with(1)
177
+ User[1]
178
+ end
179
+ end
180
+
181
+ context "PostgreSQL specific" do
182
+ setup do
183
+ ActiveRecord::Base.connection = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new 'fake_connection', 'fake_logger'
184
+ end
185
+
186
+ teardown do
187
+ ActiveRecord::Base.remove_connection
188
+ end
189
+
190
+ specify "quoting of column name" do
191
+ me = 'chris'
192
+ sql = User.select { |m| m.name == me }.to_s
193
+ sql.should == %(SELECT * FROM users WHERE users."name" = '#{me}')
194
+ end
195
+
196
+ specify "simple =~ with regexp" do
197
+ sql = User.select { |m| m.name =~ /chris/ }.to_s
198
+ sql.should == %(SELECT * FROM users WHERE users."name" ~ 'chris')
199
+ end
200
+
201
+ specify "insensitive =~" do
202
+ sql = User.select { |m| m.name =~ /chris/i }.to_s
203
+ sql.should == %(SELECT * FROM users WHERE users."name" ~* 'chris')
204
+ end
205
+
206
+ specify "negated =~" do
207
+ sql = User.select { |m| m.name !~ /chris/ }.to_s
208
+ sql.should == %(SELECT * FROM users WHERE users."name" !~ 'chris')
209
+ end
210
+
211
+ specify "negated insensitive =~" do
212
+ sql = User.select { |m| m.name !~ /chris/i }.to_s
213
+ sql.should == %(SELECT * FROM users WHERE users."name" !~* 'chris')
214
+ end
215
+ end
216
+
217
+ context "MySQL specific" do
218
+ setup do
219
+ ActiveRecord::Base.connection = ActiveRecord::ConnectionAdapters::MysqlAdapter.new('connection', 'logger')
220
+ end
221
+
222
+ teardown do
223
+ ActiveRecord::Base.connection = ActiveRecord::ConnectionAdapters::FakeAdapter.new('connection', 'logger')
224
+ end
225
+
226
+ specify "quoting of column name" do
227
+ me = 'chris'
228
+ sql = User.select { |m| m.name == me }.to_s
229
+ sql.should == "SELECT * FROM users WHERE users.`name` = '#{me}'"
230
+ end
231
+
232
+ specify "simple =~ with regexp" do
233
+ sql = User.select { |m| m.name =~ /chris/ }.to_s
234
+ sql.should == "SELECT * FROM users WHERE users.`name` REGEXP 'chris'"
235
+ end
236
+
237
+ specify "negated =~" do
238
+ sql = User.select { |m| m.name !~ 'chris' }.to_s
239
+ sql.should == "SELECT * FROM users WHERE users.`name` NOT LIKE 'chris'"
240
+ end
241
+
242
+ specify "negated =~ with regexp" do
243
+ sql = User.select { |m| m.name !~ /chris/ }.to_s
244
+ sql.should == "SELECT * FROM users WHERE users.`name` NOT REGEXP 'chris'"
245
+ end
246
+ end
247
+
248
+ context "Adapter without overrides" do
249
+ setup do
250
+ ActiveRecord::Base.connection = ActiveRecord::ConnectionAdapters::FakeAdapter.new('connection', 'logger')
251
+ end
252
+
253
+ specify "quoting of column name" do
254
+ me = 'chris'
255
+ sql = User.select { |m| m.name == me }.to_s
256
+ sql.should == "SELECT * FROM users WHERE users.name = '#{me}'"
257
+ end
258
+ end
259
+ end